From stephen@cauchy.math.missouri.edu  Sat Jul 14 13:35:17 2001
Return-Path: <stephen@cauchy.math.missouri.edu>
Received: from cauchy.math.missouri.edu (cauchy.math.missouri.edu [128.206.49.166])
	by hub.freebsd.org (Postfix) with ESMTP id 1A03F37B405
	for <FreeBSD-gnats-submit@freebsd.org>; Sat, 14 Jul 2001 13:35:04 -0700 (PDT)
	(envelope-from stephen@cauchy.math.missouri.edu)
Received: (from stephen@localhost)
	by cauchy.math.missouri.edu (8.11.4/8.11.4) id f6EKZ4p85660;
	Sat, 14 Jul 2001 15:35:04 -0500 (CDT)
	(envelope-from stephen)
Message-Id: <200107142035.f6EKZ4p85660@cauchy.math.missouri.edu>
Date: Sat, 14 Jul 2001 15:35:04 -0500 (CDT)
From: Stephen Montgomery-Smith <stephen@cauchy.math.missouri.edu>
Reply-To: Stephen Montgomery-Smith <stephen@math.missouri.edu>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: gamma returns same result as lgamma
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         28972
>Category:       bin
>Synopsis:       gamma(3) returns same result as lgamma
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    dwmalone
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sat Jul 14 13:40:27 PDT 2001
>Closed-Date:    Sat Aug 16 14:05:28 UTC 2008
>Last-Modified:  Sat Aug 16 14:05:28 UTC 2008
>Originator:     Stephen Montgomery-Smith
>Release:        FreeBSD 4.3-STABLE i386
>Organization:
University of Missouri
>Environment:
System: FreeBSD cauchy 4.3-STABLE FreeBSD 4.3-STABLE #0: Tue Jun 26 20:41:11 CDT 2001 stephen@cauchy:/usr/obj/usr/src/sys/cauchy i386


>Description:

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)));
}

>Fix:

>Release-Note:
>Audit-Trail:

From: Bruce Evans <bde@zeta.org.au>
To: Stephen Montgomery-Smith <stephen@math.missouri.edu>
Cc: FreeBSD-gnats-submit@FreeBSD.ORG
Subject: Re: bin/28972: gamma returns same result as lgamma
Date: Mon, 16 Jul 2001 00:02:58 +1000 (EST)

 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
 

From: Stephen Montgomery-Smith <stephen@math.missouri.edu>
To: Bruce Evans <bde@zeta.org.au>
Cc: FreeBSD-gnats-submit@FreeBSD.ORG
Subject: Re: bin/28972: gamma returns same result as lgamma
Date: Sun, 15 Jul 2001 09:13:40 -0500

 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

From: Stephen Montgomery-Smith <stephen@math.missouri.edu>
To: freebsd-gnats-submit@FreeBSD.org, stephen@math.missouri.edu,
	Bruce Evans <bde@zeta.org.au>
Cc:  
Subject: Re: bin/28972: gamma returns same result as lgamma
Date: Sun, 15 Jul 2001 11:06:37 -0500

 This is a multi-part message in MIME format.
 --------------9846E66BA761B8D0C3555D7D
 Content-Type: text/plain; charset=us-ascii
 Content-Transfer-Encoding: 7bit
 
 How about this change to lgamma.3 ?
 
 
 
 -- 
 Stephen Montgomery-Smith
 stephen@math.missouri.edu
 http://www.math.missouri.edu/~stephen
 --------------9846E66BA761B8D0C3555D7D
 Content-Type: text/plain; charset=us-ascii;
  name="ddd"
 Content-Transfer-Encoding: 7bit
 Content-Disposition: inline;
  filename="ddd"
 
 --- lgamma.3-orig	Sun Jul 15 10:59:56 2001
 +++ lgamma.3	Sun Jul 15 11:03:22 2001
 @@ -68,7 +68,7 @@
  returns the sign of \(*G(x).
  .Pp
  .Fn Gamma x
 -returns \(*G(x), with no effect on
 +returns ln\||\(*G(x)|, with no effect on
  .Fa signgam .
  .Sh IDIOSYNCRASIES
  Do not use the expression
 @@ -82,14 +82,14 @@
  Only after
  .Fn lgamma
  has returned can signgam be correct.
 -.Pp
 -For arguments in its range,
 -.Fn gamma
 -is preferred, as for positive arguments
 -it is accurate to within one unit in the last place.
 -Exponentiation of
 -.Fn lgamma
 -will lose up to 10 significant bits.
 +.\.Pp
 +.\For arguments in its range,
 +.\.Fn gamma
 +.\is preferred, as for positive arguments
 +.\it is accurate to within one unit in the last place.
 +.\Exponentiation of
 +.\.Fn lgamma
 +.\will lose up to 10 significant bits.
  .Sh RETURN VALUES
  .Fn Gamma
  and
 @@ -123,4 +123,12 @@
  .Fn gamma
  was originally dedicated to the
  .Fn lgamma
 -function, so some old code may no longer be compatible.
 +function, but then some old code was no longer compatible.
 +Then the name
 +.Fn gamma
 +came to mean a function that computed \(*G(x).  But this caused other
 +compatibility
 +problems, so now 
 +.Fn gamma
 +computes ln\||\(*G(x)|.  But it uses a different algorithm, and it doesn't set 
 +.Fa signgam .
 
 --------------9846E66BA761B8D0C3555D7D--
 
Responsible-Changed-From-To: freebsd-bugs->dwmalone 
Responsible-Changed-By: dwmalone 
Responsible-Changed-When: Mon Nov 5 02:18:56 PST 2001 
Responsible-Changed-Why:  
I've committed a patch based on Stephen's to -current. I'll close the 
PR once it is in -STABLE. 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=28972 
State-Changed-From-To: open->closed 
State-Changed-By: dwmalone 
State-Changed-When: Sat Aug 16 14:04:37 UTC 2008 
State-Changed-Why:  
This fix is now in all live versions of FreeBSD. Sorry for not closing 
it off sooner. 

David. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=28972 
>Unformatted:
