a3f203c174468f89c04061cf53cfbaf2efbcdb69
[musl] / src / math / tgamma.c
1 /*
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)
5
6 approximation method:
7
8                         (x - 0.5)         S(x)
9 Gamma(x) = (x + g - 0.5)         *  ----------------
10                                     exp(x + g - 0.5)
11
12 with
13                  a1      a2      a3            aN
14 S(x) ~= [ a0 + ----- + ----- + ----- + ... + ----- ]
15                x + 1   x + 2   x + 3         x + N
16
17 with a0, a1, a2, a3,.. aN constants which depend on g.
18
19 for x < 0 the following reflection formula is used:
20
21 Gamma(x)*Gamma(-x) = -pi/(x sin(pi x))
22
23 most ideas and constants are from boost and python
24 */
25 #include "libm.h"
26
27 static const double pi = 3.141592653589793238462643383279502884;
28
29 /* sin(pi x) with x > 0 && isnormal(x) assumption */
30 static double sinpi(double x)
31 {
32         int n;
33
34         /* argument reduction: x = |x| mod 2 */
35         /* spurious inexact when x is odd int */
36         x = x * 0.5;
37         x = 2 * (x - floor(x));
38
39         /* reduce x into [-.25,.25] */
40         n = 4 * x;
41         n = (n+1)/2;
42         x -= n * 0.5;
43
44         x *= pi;
45         switch (n) {
46         default: /* case 4 */
47         case 0:
48                 return __sin(x, 0, 0);
49         case 1:
50                 return __cos(x, 0);
51         case 2:
52                 /* sin(0-x) and -sin(x) have different sign at 0 */
53                 return __sin(0-x, 0, 0);
54         case 3:
55                 return -__cos(x, 0);
56         }
57 }
58
59 #define N 12
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,
76 };
77 static const double Sden[N+1] = {
78         0, 39916800, 120543840, 150917976, 105258076, 45995730, 13339535,
79         2637558, 357423, 32670, 1925, 66, 1,
80 };
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,
87 };
88
89 /* S(x) rational function for positive x */
90 static double S(double x)
91 {
92         double num = 0, den = 0;
93         int i;
94
95         /* to avoid overflow handle large x differently */
96         if (x < 8)
97                 for (i = N; i >= 0; i--) {
98                         num = num * x + Snum[i];
99                         den = den * x + Sden[i];
100                 }
101         else
102                 for (i = 0; i <= N; i++) {
103                         num = num / x + Snum[i];
104                         den = den / x + Sden[i];
105                 }
106         return num/den;
107 }
108
109 double tgamma(double x)
110 {
111         double absx, y, dy, z, r;
112
113         /* special cases */
114         if (!isfinite(x))
115                 /* tgamma(nan)=nan, tgamma(inf)=inf, tgamma(-inf)=nan with invalid */
116                 return x + INFINITY;
117
118         /* integer arguments */
119         /* raise inexact when non-integer */
120         if (x == floor(x)) {
121                 if (x == 0)
122                         /* tgamma(+-0)=+-inf with divide-by-zero */
123                         return 1/x;
124                 if (x < 0)
125                         return 0/0.0;
126                 if (x <= sizeof fact/sizeof *fact)
127                         return fact[(int)x - 1];
128         }
129
130         absx = fabs(x);
131
132         /* x ~ 0: tgamma(x) ~ 1/x */
133         if (absx < 0x1p-54)
134                 return 1/x;
135
136         /* x >= 172: tgamma(x)=inf with overflow */
137         /* x =< -184: tgamma(x)=+-0 with underflow */
138         if (absx >= 184) {
139                 if (x < 0) {
140                         if (floor(x) * 0.5 == floor(x * 0.5))
141                                 return 0;
142                         return -0.0;
143                 }
144                 x *= 0x1p1023;
145                 return x;
146         }
147
148         /* handle the error of x + g - 0.5 */
149         y = absx + gmhalf;
150         if (absx > gmhalf) {
151                 dy = y - absx;
152                 dy -= gmhalf;
153         } else {
154                 dy = y - gmhalf;
155                 dy -= absx;
156         }
157
158         z = absx - 0.5;
159         r = S(absx) * exp(-y);
160         if (x < 0) {
161                 /* reflection formula for negative x */
162                 r = -pi / (sinpi(absx) * absx * r);
163                 dy = -dy;
164                 z = -z;
165         }
166         r += dy * (gmhalf+0.5) * r / y;
167         z = pow(y, 0.5*z);
168         r = r * z * z;
169         return r;
170 }
171
172 #if 0
173 double __lgamma_r(double x, int *sign)
174 {
175         double r, absx, z, zz, w;
176
177         *sign = 1;
178
179         /* special cases */
180         if (!isfinite(x))
181                 /* lgamma(nan)=nan, lgamma(+-inf)=inf */
182                 return x*x;
183
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 */
188                 if (x <= 0)
189                         return 1/0.0;
190                 return 0;
191         }
192
193         absx = fabs(x);
194
195         /* lgamma(x) ~ -log(|x|) for tiny |x| */
196         if (absx < 0x1p-54) {
197                 *sign = 1 - 2*!!signbit(x);
198                 return -log(absx);
199         }
200
201         /* use tgamma for smaller |x| */
202         if (absx < 128) {
203                 x = tgamma(x);
204                 *sign = 1 - 2*!!signbit(x);
205                 return log(fabs(x));
206         }
207
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));
211         if (x < 0) {
212                 /* reflection formula for negative x */
213                 x = sinpi(absx);
214                 *sign = 2*!!signbit(x) - 1;
215                 r = log(pi/(fabs(x)*absx)) - r;
216         }
217         return r;
218 }
219
220 weak_alias(__lgamma_r, lgamma_r);
221 #endif