9 * float x, y, gammaf();
18 * Returns gamma function of the argument. The result is
19 * correctly signed, and the sign (+1 or -1) is also
20 * returned in a global (extern) variable named sgngamf.
21 * This same variable is also filled in by the logarithmic
22 * gamma function lgam().
24 * Arguments between 0 and 10 are reduced by recurrence and the
25 * function is approximated by a polynomial function covering
26 * the interval (2,3). Large arguments are handled by Stirling's
27 * formula. Negative arguments are made positive using
28 * a reflection formula.
34 * arithmetic domain # trials peak rms
35 * IEEE 0,-33 100,000 5.7e-7 1.0e-7
36 * IEEE -33,0 100,000 6.1e-7 1.2e-7
42 * Natural logarithm of gamma function
48 * float x, y, lgamf();
57 * Returns the base e (2.718...) logarithm of the absolute
58 * value of the gamma function of the argument.
59 * The sign (+1 or -1) of the gamma function is returned in a
60 * global (extern) variable named sgngamf.
62 * For arguments greater than 6.5, the logarithm of the gamma
63 * function is approximated by the logarithmic version of
64 * Stirling's formula. Arguments between 0 and +6.5 are reduced by
65 * by recurrence to the interval [.75,1.25] or [1.5,2.5] of a rational
66 * approximation. The cosecant reflection formula is employed for
67 * arguments less than zero.
69 * Arguments greater than MAXLGM = 2.035093e36 return MAXNUM and an
78 * arithmetic domain # trials peak rms
79 * IEEE -100,+100 500,000 7.4e-7 6.8e-8
80 * The error criterion was relative when the function magnitude
81 * was greater than one but absolute when it was less than one.
82 * The routine has low relative error for positive arguments.
84 * The following test used the relative error criterion.
85 * IEEE -2, +3 100000 4.0e-7 5.6e-8
93 Cephes Math Library Release 2.7: July, 1998
94 Copyright 1984, 1987, 1989, 1992, 1998 by Stephen L. Moshier
100 /* define MAXGAM 34.84425627277176174 */
102 /* Stirling's formula for the gamma function
103 * gamma(x) = sqrt(2 pi) x^(x-.5) exp(-x) ( 1 + 1/x P(1/x) )
105 * relative error < 1.9e-11
107 static float STIR[] = {
108 -2.705194986674176E-003,
109 3.473255786154910E-003,
110 8.333331788340907E-002,
112 static float MAXSTIR = 26.77;
113 static float SQTPIF = 2.50662827463100050242; /* sqrt( 2 pi ) */
117 extern float MAXLOGF, MAXNUMF, PIF;
122 float powf( float, float );
126 static float stirf(float);
127 float polevlf( float, float *, int );
128 float p1evlf( float, float *, int );
130 float expf(), logf(), powf(), sinf(), floorf();
131 float polevlf(), p1evlf();
132 static float stirf();
135 /* Gamma function computed by Stirling's formula,
136 * sqrt(2 pi) x^(x-.5) exp(-x) (1 + 1/x P(1/x))
137 * The polynomial STIR is valid for 33 <= x <= 172.
140 static float stirf( float xx )
142 static float stirf(xx)
150 w = 1.0 + w * polevlf( w, STIR, 2 );
153 { /* Avoid overflow in pow() */
154 v = powf( x, 0.5 * x - 0.25 );
160 y = powf( x, x - 0.5 ) * y;
167 /* gamma(x+2), 0 < x < 1 */
169 1.536830450601906E-003,
170 5.397581592950993E-003,
171 4.130370201859976E-003,
172 7.232307985516519E-002,
173 8.203960091619193E-002,
174 4.117857447645796E-001,
175 4.227867745131584E-001,
176 9.999999822945073E-001,
180 float gammaf( float xx )
186 float p, q, x, z, nz;
187 int i, direction, negative;
209 nz = q * sinf( PIF * nz );
213 mtherr( "gamma", OVERFLOW );
214 return( sgngamf * MAXNUMF);
258 p = z * polevlf( x, P, 7 );
264 p = sgngamf * PIF/(nz * p );
271 mtherr( "gamma", SING );
276 p = z / ((1.0 + 0.5772156649015329 * x) * x);
284 /* log gamma(x+2), -.5 < x < .5 */
286 6.055172732649237E-004,
287 -1.311620815545743E-003,
288 2.863437556468661E-003,
289 -7.366775108654962E-003,
290 2.058355474821512E-002,
291 -6.735323259371034E-002,
292 3.224669577325661E-001,
293 4.227843421859038E-001
296 /* log gamma(x+1), -.25 < x < .25 */
298 1.369488127325832E-001,
299 -1.590086327657347E-001,
300 1.692415923504637E-001,
301 -2.067882815621965E-001,
302 2.705806208275915E-001,
303 -4.006931650563372E-001,
304 8.224670749082976E-001,
305 -5.772156501719101E-001
308 /* log( sqrt( 2*pi ) ) */
309 static float LS2PI = 0.91893853320467274178;
310 #define MAXLGM 2.035093e36
311 static float PIINV = 0.318309886183790671538;
313 /* Logarithm of gamma function */
317 float lgamf( float xx )
333 w = lgamf(q); /* note this modifies sgngam! */
348 z = q * sinf( PIF * z );
351 z = -logf( PIINV*z ) - w;
371 p = x * polevlf( x, B, 7 );
377 x -= 1.0; /* x + 1 - 2 */
384 p = x * polevlf( x, C, 7 );
398 p = x * polevlf( x, B, 7 );
420 mtherr( "lgamf", OVERFLOW );
421 return( sgngamf * MAXNUMF );
424 /* Note, though an asymptotic formula could be used for x >= 3,
425 * there is cancellation error in the following if x < 6.5. */
427 q += ( x - 0.5 ) * logf(x);
433 q += (( 6.789774945028216E-004 * p
434 - 2.769887652139868E-003 ) * p
435 + 8.333316229807355E-002 ) * z;