Bug 33683 - calculating lgamma instead of gamma
Summary: calculating lgamma instead of gamma
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libfortran (show other bugs)
Version: 4.3.0
: P3 critical
Target Milestone: ---
Assignee: Thomas Koenig
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2007-10-06 22:22 UTC by Thomas Koenig
Modified: 2007-10-07 21:45 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2007-10-07 16:48:03


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Koenig 2007-10-06 22:22:55 UTC
$ cat g.f90 
program main
  real :: x
  x = 10.
  print *,gamma(x)
end program main
$ gfortran g.f90 
$ ./a.out
   12.801827    
$ gfortran -v
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ../../gcc/trunk/configure --prefix=/home/ig25 --enable-languages=c,fortran --enable-mainainer-mode
Thread model: posix
gcc version 4.3.0 20071006 (experimental) (GCC)
Comment 1 Dominique d'Humieres 2007-10-06 22:28:06 UTC
On Darwin I get:

/usr/bin/ld: Undefined symbols:
_gammaf
collect2: ld returned 1 exit status

Target: powerpc-apple-darwin8
Configured with: ../gcc-4.3-work/configure --prefix=/opt/gcc/gcc4.3w --mandir=/opt/gcc/gcc4.3w/share/man --infodir=/opt/gcc/gcc4.3w/share/info --build=powerpc-apple-darwin8 --enable-languages=c,fortran --with-gmp=/sw --with-libiconv-prefix=/sw --with-system-zlib --x-includes=/usr/X11R6/include --x-libraries=/usr/X11R6/lib
Thread model: posix
gcc version 4.3.0 20071006 (experimental) (GCC) 

revision 129055.

Comment 2 Francois-Xavier Coudert 2007-10-06 23:04:32 UTC
I checked and the simplification routines work correctly, which means there is no real testsuite coverage for these functions. We should always include testcases comparing values calculated at runtime with constants coming from our simplification routines.
Comment 3 Thomas Koenig 2007-10-07 10:22:11 UTC
(In reply to comment #2)
> I checked and the simplification routines work correctly, which means there is
> no real testsuite coverage for these functions. We should always include
> testcases comparing values calculated at runtime with constants coming from our
> simplification routines.

I concur.  PR 22307 strikes again...

The cause of the problem is outlined in the gamma(3) man page
of my Linux distro:

DESCRIPTION
       For the definition of the Gamma function, see tgamma(3).

   *BSD version
       4.4BSD and FreeBSD libm have a gamma() function that computes the Gamma
       function, as one would expect.

   glibc version
       Glibc has a gamma() function that is equivalent to  lgamma()  and  com-
       putes  the  natural logarithm of the Gamma function.  (This is for com-
       patibility reasons only.  Don't use this function.)

CONFORMING TO
       4.2BSD.  Compatible with previous mistakes.

I think we need to use tgamma() where available.  If it isn't, we
need a test to check whether gamma() is equivalent to tgamma() or
to lgamma().

Yuck.



Comment 4 Tobias Schlüter 2007-10-07 12:01:52 UTC
(In reply to comment #3)
> I think we need to use tgamma() where available.  If it isn't, we
> need a test to check whether gamma() is equivalent to tgamma() or
> to lgamma().
> 
> Yuck.

It may be simpler to calculate it ourselves, perhaps lifting code form glibc.

Comment 5 Thomas Koenig 2007-10-07 16:48:03 UTC
This fixes things at least on my system; this also
passes regression-testing.

Index: mathbuiltins.def
===================================================================
--- mathbuiltins.def    (revision 129050)
+++ mathbuiltins.def    (working copy)
@@ -30,5 +30,5 @@ DEFINE_MATH_BUILTIN   (Y1,    "y1",     
 DEFINE_MATH_BUILTIN   (YN,    "yn",     2)
 DEFINE_MATH_BUILTIN   (ERF,   "erf",    0)
 DEFINE_MATH_BUILTIN   (ERFC,  "erfc",   0)
-DEFINE_MATH_BUILTIN   (GAMMA, "gamma",  0)
+DEFINE_MATH_BUILTIN   (GAMMA, "tgamma",  0)
 DEFINE_MATH_BUILTIN   (LGAMMA,"lgamma", 0)
Comment 6 Jerry DeLisle 2007-10-07 18:43:07 UTC
I have tested this on x86-64-linux.  It works here as well.  After reading the man page myself, I think this is the answer and I would commit it as obvious.

The only question is what do other platforms do? AIX hp etc
Comment 7 Thomas Koenig 2007-10-07 21:36:20 UTC
Subject: Bug 33683

Author: tkoenig
Date: Sun Oct  7 21:36:09 2007
New Revision: 129116

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=129116
Log:
2007-10-07  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR libfortran/33683
	* mathbuiltins.def (GAMMA):  Change function name to
	"tgamma" instad of "gamma".

2007-10-07  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR libfortran/33683
	* gfortran.dg/gamma_5.f90:  New test case


Added:
    trunk/gcc/testsuite/gfortran.dg/gamma_5.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/mathbuiltins.def
    trunk/gcc/testsuite/ChangeLog

Comment 8 Thomas Koenig 2007-10-07 21:45:55 UTC
Fixed.

Let's close this and watch for occurences of
gamma_5.f90 failing, then open a new bug report
if that happens.
Comment 9 Thomas Koenig 2007-10-09 20:13:31 UTC
Subject: Bug 33683

Author: tkoenig
Date: Tue Oct  9 20:13:18 2007
New Revision: 129174

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=129174
Log:
2007-10-09  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR libfortran/33683
	* gfortran.dg/gamma_5.f90:  Adjust tolerance to avoid
	failure on some common systems.


Modified:
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/gamma_5.f90