1 /* $OpenBSD: e_tgammal.c,v 1.4 2013/11/12 20:35:19 martynas Exp $ */
2
3 /*
4 * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 /* tgammal.c
20 *
21 * Gamma function
22 *
23 *
24 *
25 * SYNOPSIS:
26 *
27 * long double x, y, tgammal();
28 *
29 * y = tgammal( x );
30 *
31 *
32 *
33 * DESCRIPTION:
34 *
35 * Returns gamma function of the argument. The result is correctly
36 * signed. This variable is also filled in by the logarithmic gamma
37 * function lgamma().
38 *
39 * Arguments |x| <= 13 are reduced by recurrence and the function
40 * approximated by a rational function of degree 7/8 in the
41 * interval (2,3). Large arguments are handled by Stirling's
42 * formula. Large negative arguments are made positive using
43 * a reflection formula.
44 *
45 *
46 * ACCURACY:
47 *
48 * Relative error:
49 * arithmetic domain # trials peak rms
50 * IEEE -40,+40 10000 3.6e-19 7.9e-20
51 * IEEE -1755,+1755 10000 4.8e-18 6.5e-19
52 *
53 * Accuracy for large arguments is dominated by error in powl().
54 *
55 */
56
57
58
59 /*
60 tgamma(x+2) = tgamma(x+2) P(x)/Q(x)
61 0 <= x <= 1
62 Relative error
63 n=7, d=8
64 Peak error = 1.83e-20
65 Relative error spread = 8.4e-23
66 */
67
68 static long double P[8] = {
69 4.212760487471622013093E-5L,
70 4.542931960608009155600E-4L,
71 4.092666828394035500949E-3L,
72 2.385363243461108252554E-2L,
73 1.113062816019361559013E-1L,
74 3.629515436640239168939E-1L,
75 8.378004301573126728826E-1L,
76 1.000000000000000000009E0L,
77 };
78 static long double Q[9] = {
79 -1.397148517476170440917E-5L,
80 2.346584059160635244282E-4L,
81 -1.237799246653152231188E-3L,
82 -7.955933682494738320586E-4L,
83 2.773706565840072979165E-2L,
84 -4.633887671244534213831E-2L,
85 -2.243510905670329164562E-1L,
86 4.150160950588455434583E-1L,
87 9.999999999999999999908E-1L,
88 };
89
90 /*
91 static long double P[] = {
92 -3.01525602666895735709e0L,
93 -3.25157411956062339893e1L,
94 -2.92929976820724030353e2L,
95 -1.70730828800510297666e3L,
96 -7.96667499622741999770e3L,
97 -2.59780216007146401957e4L,
98 -5.99650230220855581642e4L,
99 -7.15743521530849602425e4L
100 };
101 static long double Q[] = {
102 1.00000000000000000000e0L,
103 -1.67955233807178858919e1L,
104 8.85946791747759881659e1L,
105 5.69440799097468430177e1L,
106 -1.98526250512761318471e3L,
107 3.31667508019495079814e3L,
108 1.60577839621734713377e4L,
109 -2.97045081369399940529e4L,
110 -7.15743521530849602412e4L
111 };
112 */
113 #define MAXGAML 1755.455L
114 /*static const long double LOGPI = 1.14472988584940017414L;*/
115
116 /* Stirling's formula for the gamma function
117 tgamma(x) = sqrt(2 pi) x^(x-.5) exp(-x) (1 + 1/x P(1/x))
118 z(x) = x
119 13 <= x <= 1024
120 Relative error
121 n=8, d=0
122 Peak error = 9.44e-21
123 Relative error spread = 8.8e-4
124 */
125
126 static long double STIR[9] = {
127 7.147391378143610789273E-4L,
128 -2.363848809501759061727E-5L,
129 -5.950237554056330156018E-4L,
130 6.989332260623193171870E-5L,
131 7.840334842744753003862E-4L,
132 -2.294719747873185405699E-4L,
133 -2.681327161876304418288E-3L,
134 3.472222222230075327854E-3L,
135 8.333333333333331800504E-2L,
136 };
137
138 #define MAXSTIR 1024.0L
139 static const long double SQTPI = 2.50662827463100050242E0L;
140
141 /* 1/tgamma(x) = z P(z)
142 * z(x) = 1/x
143 * 0 < x < 0.03125
144 * Peak relative error 4.2e-23
145 */
146
147 static long double S[9] = {
148 -1.193945051381510095614E-3L,
149 7.220599478036909672331E-3L,
150 -9.622023360406271645744E-3L,
151 -4.219773360705915470089E-2L,
152 1.665386113720805206758E-1L,
153 -4.200263503403344054473E-2L,
154 -6.558780715202540684668E-1L,
155 5.772156649015328608253E-1L,
156 1.000000000000000000000E0L,
157 };
158
159 /* 1/tgamma(-x) = z P(z)
160 * z(x) = 1/x
161 * 0 < x < 0.03125
162 * Peak relative error 5.16e-23
163 * Relative error spread = 2.5e-24
164 */
165
166 static long double SN[9] = {
167 1.133374167243894382010E-3L,
168 7.220837261893170325704E-3L,
169 9.621911155035976733706E-3L,
170 -4.219773343731191721664E-2L,
171 -1.665386113944413519335E-1L,
172 -4.200263503402112910504E-2L,
173 6.558780715202536547116E-1L,
174 5.772156649015328608727E-1L,
175 -1.000000000000000000000E0L,
176 };
177
178 static const long double PIL = 3.1415926535897932384626L;
179
180 static long double stirf ( long double );
181
182 /* Gamma function computed by Stirling's formula.
183 */
stirf(long double x)184 static long double stirf(long double x)
185 {
186 long double y, w, v;
187
188 w = 1.0L/x;
189 /* For large x, use rational coefficients from the analytical expansion. */
190 if( x > 1024.0L )
191 w = (((((6.97281375836585777429E-5L * w
192 + 7.84039221720066627474E-4L) * w
193 - 2.29472093621399176955E-4L) * w
194 - 2.68132716049382716049E-3L) * w
195 + 3.47222222222222222222E-3L) * w
196 + 8.33333333333333333333E-2L) * w
197 + 1.0L;
198 else
199 w = 1.0L + w * __polevll( w, STIR, 8 );
200 y = expl(x);
201 if( x > MAXSTIR )
202 { /* Avoid overflow in pow() */
203 v = powl( x, 0.5L * x - 0.25L );
204 y = v * (v / y);
205 }
206 else
207 {
208 y = powl( x, x - 0.5L ) / y;
209 }
210 y = SQTPI * y * w;
211 return( y );
212 }
213
214 long double
tgammal(long double x)215 tgammal(long double x)
216 {
217 long double p, q, z;
218 int i;
219
220 if( isnan(x) )
221 return(x + x);
222 if(x == (long double) INFINITY)
223 return((long double) INFINITY);
224 if(x == -(long double) INFINITY)
225 return __math_invalidl(x);
226 if( x == 0.0L )
227 return __math_divzerol(__signbitl(x));
228 q = fabsl(x);
229
230 if( q > 13.0L )
231 {
232 int sign = 1;
233 if( q > MAXGAML ) {
234 if (x < 0.0L)
235 return __math_invalidl(x);
236 return __math_oflowl(0);
237 }
238 if( x < 0.0L )
239 {
240 p = floorl(q);
241 if( p == q )
242 return __math_invalidl(x);
243 i = p;
244 if( (i & 1) == 0 )
245 sign = -1;
246 z = q - p;
247 if( z > 0.5L )
248 {
249 p += 1.0L;
250 z = q - p;
251 }
252 z = q * sinl( PIL * z );
253 z = fabsl(z) * stirf(q);
254 if( z <= PIL/LDBL_MAX )
255 {
256 return __math_oflowl(sign < 0);
257 }
258 z = PIL/z;
259 }
260 else
261 {
262 z = stirf(x);
263 }
264 return( sign * z );
265 }
266
267 z = 1.0L;
268 while( x >= 3.0L )
269 {
270 x -= 1.0L;
271 z *= x;
272 }
273
274 while( x < -0.03125L )
275 {
276 z /= x;
277 x += 1.0L;
278 }
279
280 if( x <= 0.03125L )
281 goto small;
282
283 while( x < 2.0L )
284 {
285 z /= x;
286 x += 1.0L;
287 }
288
289 if( x == 2.0L )
290 return(z);
291
292 x -= 2.0L;
293 p = __polevll( x, P, 7 );
294 q = __polevll( x, Q, 8 );
295 z = z * p / q;
296 return z;
297
298 small:
299 if( x == 0.0L )
300 return __math_invalidl(x);
301 else
302 {
303 q = check_oflowl(1.0L/x);
304 if (!isinfl(q))
305 {
306 if( x < 0.0L )
307 {
308 x = -x;
309 q = z / __polevll( x, SN, 8 ) * (-q);
310 }
311 else
312 q = z / __polevll( x, S, 8 ) * q;
313 }
314 }
315 return q;
316 }
317