1 
2 /* @(#)w_gamma.c 5.1 93/09/24 */
3 /*
4  * ====================================================
5  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6  *
7  * Developed at SunPro, a Sun Microsystems, Inc. business.
8  * Permission to use, copy, modify, and distribute this
9  * software is freely granted, provided that this notice
10  * is preserved.
11  * ====================================================
12  *
13  */
14 
15 /* BUG:  FIXME?
16      According to Linux man pages for tgamma, lgamma, and gamma, the gamma
17 function was originally defined in BSD as implemented here--the log of the gamma
18 function.  BSD 4.3 changed the name to lgamma, apparently removing gamma.  BSD
19 4.4 re-introduced the gamma name with the more intuitive, without logarithm,
20 plain gamma function.  The C99 standard apparently wanted to avoid a problem
21 with the poorly-named earlier gamma and used tgamma when adding a plain
22 gamma function.
23      So the current gamma is matching an old, bad definition, and not
24 matching a newer, better definition.  */
25 /*
26 FUNCTION
27         <<gamma>>, <<gammaf>>, <<lgamma>>, <<lgammaf>>, <<gamma_r>>, <<gammaf_r>>, <<lgamma_r>>, <<lgammaf_r>>, <<tgamma>>, and <<tgammaf>>---logarithmic and plain gamma functions
28 
29 INDEX
30 gamma
31 INDEX
32 gammaf
33 INDEX
34 lgamma
35 INDEX
36 lgammaf
37 INDEX
38 gamma_r
39 INDEX
40 gammaf_r
41 INDEX
42 lgamma_r
43 INDEX
44 lgammaf_r
45 INDEX
46 tgamma
47 INDEX
48 tgammaf
49 
50 SYNOPSIS
51 #include <math.h>
52 double gamma(double <[x]>);
53 float gammaf(float <[x]>);
54 double lgamma(double <[x]>);
55 float lgammaf(float <[x]>);
56 double gamma_r(double <[x]>, int *<[signgamp]>);
57 float gammaf_r(float <[x]>, int *<[signgamp]>);
58 double lgamma_r(double <[x]>, int *<[signgamp]>);
59 float lgammaf_r(float <[x]>, int *<[signgamp]>);
60 double tgamma(double <[x]>);
61 float tgammaf(float <[x]>);
62 
63 DESCRIPTION
64 <<gamma>> calculates
65 @tex
66 $\mit ln\bigl(\Gamma(x)\bigr)$,
67 @end tex
68 the natural logarithm of the gamma function of <[x]>.  The gamma function
69 (<<exp(gamma(<[x]>))>>) is a generalization of factorial, and retains
70 the property that
71 @ifnottex
72 <<exp(gamma(N))>> is equivalent to <<N*exp(gamma(N-1))>>.
73 @end ifnottex
74 @tex
75 $\mit \Gamma(N)\equiv N\times\Gamma(N-1)$.
76 @end tex
77 Accordingly, the results of the gamma function itself grow very
78 quickly.  <<gamma>> is defined as
79 @tex
80 $\mit ln\bigl(\Gamma(x)\bigr)$ rather than simply $\mit \Gamma(x)$
81 @end tex
82 @ifnottex
83 the natural log of the gamma function, rather than the gamma function
84 itself,
85 @end ifnottex
86 to extend the useful range of results representable.
87 
88 The sign of the result is returned in the global variable <<signgam>>,
89 which is declared in math.h.
90 
91 <<gammaf>> performs the same calculation as <<gamma>>, but uses and
92 returns <<float>> values.
93 
94 <<lgamma>> and <<lgammaf>> are alternate names for <<gamma>> and
95 <<gammaf>>.  The use of <<lgamma>> instead of <<gamma>> is a reminder
96 that these functions compute the log of the gamma function, rather
97 than the gamma function itself.
98 
99 The functions <<gamma_r>>, <<gammaf_r>>, <<lgamma_r>>, and
100 <<lgammaf_r>> are just like <<gamma>>, <<gammaf>>, <<lgamma>>, and
101 <<lgammaf>>, respectively, but take an additional argument.  This
102 additional argument is a pointer to an integer.  This additional
103 argument is used to return the sign of the result, and the global
104 variable <<signgam>> is not used.  These functions may be used for
105 reentrant calls (but they will still set the global variable <<errno>>
106 if an error occurs).
107 
108 <<tgamma>> and <<tgammaf>> are the "true gamma" functions, returning
109 @tex
110 $\mit \Gamma(x)$,
111 @end tex
112 the gamma function of <[x]>--without a logarithm.
113 (They are apparently so named because of the prior existence of the old,
114 poorly-named <<gamma>> functions which returned the log of gamma up
115 through BSD 4.2.)
116 
117 RETURNS
118 Normally, the computed result is returned.
119 
120 When <[x]> is a nonpositive integer, <<gamma>> returns <<HUGE_VAL>>
121 and <<errno>> is set to <<EDOM>>.  If the result overflows, <<gamma>>
122 returns <<HUGE_VAL>> and <<errno>> is set to <<ERANGE>>.
123 
124 PORTABILITY
125 Neither <<gamma>> nor <<gammaf>> is ANSI C.  It is better not to use either
126 of these; use <<lgamma>> or <<tgamma>> instead.@*
127 <<lgamma>>, <<lgammaf>>, <<tgamma>>, and <<tgammaf>> are nominally C standard
128 in terms of the base return values, although the <[signgam]> global for
129 <<lgamma>> is not standard.
130 */
131 
132 /* double gamma(double x)
133  * Return the logarithm of the Gamma function of x.
134  *
135  * Method: call lgamma
136  */
137 
138 #include "fdlibm.h"
139 #include <errno.h>
140 
141 #ifndef _DOUBLE_IS_32BITS
142 
143 double
gamma(double x)144 gamma(double x)
145 {
146     return lgamma(x);
147 }
148 
149 #endif /* defined(_DOUBLE_IS_32BITS) */
150