Bug 28972 - gamma(3) returns same result as lgamma
Summary: gamma(3) returns same result as lgamma
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 4.3-STABLE
Hardware: Any Any
: Normal Affects Only Me
Assignee: dwmalone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2001-07-14 21:40 UTC by Stephen Montgomery-Smith
Modified: 2008-08-16 15:05 UTC (History)
0 users

See Also:


Attachments
ddd (1.24 KB, text/plain; charset=us-ascii)
2001-07-15 17:06 UTC, Stephen Montgomery-Smith
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Stephen Montgomery-Smith 2001-07-14 21:40:27 UTC
The function gamma in the math library gives the wrong answer - it gives the
same answer as lgamma.  For example gamma(6) should be 120.  The following
C program gives 4.78749 which is log(120).

How-To-Repeat: 
#include <stdio.h>
#include <math.h>

main() {
  double x=6;
  printf("%g %g %g %g\n",x,gamma(x),lgamma(x),exp(lgamma(x)));
}
Comment 1 Bruce Evans 2001-07-15 15:02:58 UTC
On Sat, 14 Jul 2001, Stephen Montgomery-Smith wrote:

> The function gamma in the math library gives the wrong answer - it gives the
> same answer as lgamma. ...

This is intentional (except for the broken man page).  gamma was actually
the log of gamma in many old Unix libraries.  I think these libraries had
no actual gamma function.  I'm not sure if they had the log of gamma named
as lgamma.

Net/2 and 4.4BSD attempted to fix this by implementing gamma and
actually naming it gamma.  This mainly confused everything which knew
that gamma was actually lgamma.  FreeBSD replaced the Net/2 libm by
the Sun fdlibm in FreeBSD-1 and kept using it in FreeBSD-[2-5].  gamma
is actually the log of gamma again in fdlibm.

C99 "fixed" this in a backwards-compatible by bogusly naming gamma as
tgamma (true gamma).  C99 has tgamma and lgamma, but no gamma.  Previous
C standards didn't specify any gamma functions.

C99 and tgamma aren't implemented under FreeBSD yet (especially the former).
libm/common_source/gamma.c can probably be ported easily to get a reasonably
good version of tgamma (I expect it would be better than exp(lgamma) but
not as good as lgamma).

Bruce
Comment 2 Stephen Montgomery-Smith 2001-07-15 15:13:40 UTC
So we should fix the man page then?  Does gamma set signgam?

I did a diff on the source code for gamma and lgamma and they did seem
very different, at least superficially.

The man page is very broken.  It includes comments like:

     For arguments in its range, gamma() and gammaf() is preferred, as
for
     positive arguments it is accurate to within one unit in the last
place.
     Exponentiation of lgamma() will lose up to 10 significant bits.


Bruce Evans wrote:
> 
> On Sat, 14 Jul 2001, Stephen Montgomery-Smith wrote:
> 
> > The function gamma in the math library gives the wrong answer - it gives the
> > same answer as lgamma. ...
> 
> This is intentional (except for the broken man page).  gamma was actually
> the log of gamma in many old Unix libraries.  I think these libraries had
> no actual gamma function.  I'm not sure if they had the log of gamma named
> as lgamma.

-- 
Stephen Montgomery-Smith
stephen@math.missouri.edu
http://www.math.missouri.edu/~stephen
Comment 3 Stephen Montgomery-Smith 2001-07-15 17:06:37 UTC
How about this change to lgamma.3 ?



-- 
Stephen Montgomery-Smith
stephen@math.missouri.edu
http://www.math.missouri.edu/~stephen
Comment 4 dwmalone freebsd_committer freebsd_triage 2001-11-05 10:18:56 UTC
Responsible Changed
From-To: freebsd-bugs->dwmalone

I've committed a patch based on Stephen's to -current. I'll close the 
PR once it is in -STABLE.
Comment 5 dwmalone freebsd_committer freebsd_triage 2008-08-16 15:04:37 UTC
State Changed
From-To: open->closed

This fix is now in all live versions of FreeBSD. Sorry for not closing 
it off sooner. 

David.