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))); }
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
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
How about this change to lgamma.3 ? -- Stephen Montgomery-Smith stephen@math.missouri.edu http://www.math.missouri.edu/~stephen
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.
State Changed From-To: open->closed This fix is now in all live versions of FreeBSD. Sorry for not closing it off sooner. David.