2 "A Precision Approximation of the Gamma Function" - Cornelius Lanczos (1964)
3 "Lanczos Implementation of the Gamma Function" - Paul Godfrey (2001)
4 "An Analysis of the Lanczos Gamma Approximation" - Glendon Ralph Pugh (2004)
9 Gamma(x) = (x + g - 0.5) * ----------------
14 S(x) ~= [ a0 + ----- + ----- + ----- + ... + ----- ]
15 x + 1 x + 2 x + 3 x + N
17 with a0, a1, a2, a3,.. aN constants which depend on g.
19 for x < 0 the following reflection formula is used:
21 Gamma(x)*Gamma(-x) = -pi/(x sin(pi x))
23 most ideas and constants are from boost and python
27 static const double pi = 3.141592653589793238462643383279502884;
29 /* sin(pi x) with x > 0 && isnormal(x) assumption */
30 static double sinpi(double x)
34 /* argument reduction: x = |x| mod 2 */
35 /* spurious inexact when x is odd int */
37 x = 2 * (x - floor(x));
39 /* reduce x into [-.25,.25] */
48 return __sin(x, 0, 0);
52 /* sin(0-x) and -sin(x) have different sign at 0 */
53 return __sin(0-x, 0, 0);
60 //static const double g = 6.024680040776729583740234375;
61 static const double gmhalf = 5.524680040776729583740234375;
62 static const double Snum[N+1] = {
63 23531376880.410759688572007674451636754734846804940,
64 42919803642.649098768957899047001988850926355848959,
65 35711959237.355668049440185451547166705960488635843,
66 17921034426.037209699919755754458931112671403265390,
67 6039542586.3520280050642916443072979210699388420708,
68 1439720407.3117216736632230727949123939715485786772,
69 248874557.86205415651146038641322942321632125127801,
70 31426415.585400194380614231628318205362874684987640,
71 2876370.6289353724412254090516208496135991145378768,
72 186056.26539522349504029498971604569928220784236328,
73 8071.6720023658162106380029022722506138218516325024,
74 210.82427775157934587250973392071336271166969580291,
75 2.5066282746310002701649081771338373386264310793408,
77 static const double Sden[N+1] = {
78 0, 39916800, 120543840, 150917976, 105258076, 45995730, 13339535,
79 2637558, 357423, 32670, 1925, 66, 1,
81 /* n! for small integer n */
82 static const double fact[] = {
83 1, 1, 2, 6, 24, 120, 720, 5040.0, 40320.0, 362880.0, 3628800.0, 39916800.0,
84 479001600.0, 6227020800.0, 87178291200.0, 1307674368000.0, 20922789888000.0,
85 355687428096000.0, 6402373705728000.0, 121645100408832000.0,
86 2432902008176640000.0, 51090942171709440000.0, 1124000727777607680000.0,
89 /* S(x) rational function for positive x */
90 static double S(double x)
92 double_t num = 0, den = 0;
95 /* to avoid overflow handle large x differently */
97 for (i = N; i >= 0; i--) {
98 num = num * x + Snum[i];
99 den = den * x + Sden[i];
102 for (i = 0; i <= N; i++) {
103 num = num / x + Snum[i];
104 den = den / x + Sden[i];
109 double tgamma(double x)
111 double absx, y, dy, z, r;
115 /* tgamma(nan)=nan, tgamma(inf)=inf, tgamma(-inf)=nan with invalid */
118 /* integer arguments */
119 /* raise inexact when non-integer */
122 /* tgamma(+-0)=+-inf with divide-by-zero */
126 if (x <= sizeof fact/sizeof *fact)
127 return fact[(int)x - 1];
132 /* x ~ 0: tgamma(x) ~ 1/x */
136 /* x >= 172: tgamma(x)=inf with overflow */
137 /* x =< -184: tgamma(x)=+-0 with underflow */
140 if (floor(x) * 0.5 == floor(x * 0.5))
148 /* handle the error of x + g - 0.5 */
159 r = S(absx) * exp(-y);
161 /* reflection formula for negative x */
162 r = -pi / (sinpi(absx) * absx * r);
166 r += dy * (gmhalf+0.5) * r / y;
173 double __lgamma_r(double x, int *sign)
175 double r, absx, z, zz, w;
181 /* lgamma(nan)=nan, lgamma(+-inf)=inf */
184 /* integer arguments */
185 if (x == floor(x) && x <= 2) {
186 /* n <= 0: lgamma(n)=inf with divbyzero */
187 /* n == 1,2: lgamma(n)=0 */
195 /* lgamma(x) ~ -log(|x|) for tiny |x| */
196 if (absx < 0x1p-54) {
197 *sign = 1 - 2*!!signbit(x);
201 /* use tgamma for smaller |x| */
204 *sign = 1 - 2*!!signbit(x);
208 /* second term (log(S)-g) could be more precise here.. */
209 /* or with stirling: (|x|-0.5)*(log(|x|)-1) + poly(1/|x|) */
210 r = (absx-0.5)*(log(absx+gmhalf)-1) + (log(S(absx)) - (gmhalf+0.5));
212 /* reflection formula for negative x */
214 *sign = 2*!!signbit(x) - 1;
215 r = log(pi/(fabs(x)*absx)) - r;
220 weak_alias(__lgamma_r, lgamma_r);