xref: /openbsd-src/lib/libm/src/ld80/e_tgammal.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: e_tgammal.c,v 1.2 2011/07/20 21:02:51 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  * extern int signgam;
29  *
30  * y = tgammal( x );
31  *
32  *
33  *
34  * DESCRIPTION:
35  *
36  * Returns gamma function of the argument.  The result is
37  * correctly signed, and the sign (+1 or -1) is also
38  * returned in a global (extern) variable named signgam.
39  * This variable is also filled in by the logarithmic gamma
40  * function lgamma().
41  *
42  * Arguments |x| <= 13 are reduced by recurrence and the function
43  * approximated by a rational function of degree 7/8 in the
44  * interval (2,3).  Large arguments are handled by Stirling's
45  * formula. Large negative arguments are made positive using
46  * a reflection formula.
47  *
48  *
49  * ACCURACY:
50  *
51  *                      Relative error:
52  * arithmetic   domain     # trials      peak         rms
53  *    IEEE     -40,+40      10000       3.6e-19     7.9e-20
54  *    IEEE    -1755,+1755   10000       4.8e-18     6.5e-19
55  *
56  * Accuracy for large arguments is dominated by error in powl().
57  *
58  */
59 
60 #include <float.h>
61 #include <math.h>
62 
63 /*
64 tgamma(x+2)  = tgamma(x+2) P(x)/Q(x)
65 0 <= x <= 1
66 Relative error
67 n=7, d=8
68 Peak error =  1.83e-20
69 Relative error spread =  8.4e-23
70 */
71 
72 static long double P[8] = {
73  4.212760487471622013093E-5L,
74  4.542931960608009155600E-4L,
75  4.092666828394035500949E-3L,
76  2.385363243461108252554E-2L,
77  1.113062816019361559013E-1L,
78  3.629515436640239168939E-1L,
79  8.378004301573126728826E-1L,
80  1.000000000000000000009E0L,
81 };
82 static long double Q[9] = {
83 -1.397148517476170440917E-5L,
84  2.346584059160635244282E-4L,
85 -1.237799246653152231188E-3L,
86 -7.955933682494738320586E-4L,
87  2.773706565840072979165E-2L,
88 -4.633887671244534213831E-2L,
89 -2.243510905670329164562E-1L,
90  4.150160950588455434583E-1L,
91  9.999999999999999999908E-1L,
92 };
93 
94 /*
95 static long double P[] = {
96 -3.01525602666895735709e0L,
97 -3.25157411956062339893e1L,
98 -2.92929976820724030353e2L,
99 -1.70730828800510297666e3L,
100 -7.96667499622741999770e3L,
101 -2.59780216007146401957e4L,
102 -5.99650230220855581642e4L,
103 -7.15743521530849602425e4L
104 };
105 static long double Q[] = {
106  1.00000000000000000000e0L,
107 -1.67955233807178858919e1L,
108  8.85946791747759881659e1L,
109  5.69440799097468430177e1L,
110 -1.98526250512761318471e3L,
111  3.31667508019495079814e3L,
112  1.60577839621734713377e4L,
113 -2.97045081369399940529e4L,
114 -7.15743521530849602412e4L
115 };
116 */
117 #define MAXGAML 1755.455L
118 /*static const long double LOGPI = 1.14472988584940017414L;*/
119 
120 /* Stirling's formula for the gamma function
121 tgamma(x) = sqrt(2 pi) x^(x-.5) exp(-x) (1 + 1/x P(1/x))
122 z(x) = x
123 13 <= x <= 1024
124 Relative error
125 n=8, d=0
126 Peak error =  9.44e-21
127 Relative error spread =  8.8e-4
128 */
129 
130 static long double STIR[9] = {
131  7.147391378143610789273E-4L,
132 -2.363848809501759061727E-5L,
133 -5.950237554056330156018E-4L,
134  6.989332260623193171870E-5L,
135  7.840334842744753003862E-4L,
136 -2.294719747873185405699E-4L,
137 -2.681327161876304418288E-3L,
138  3.472222222230075327854E-3L,
139  8.333333333333331800504E-2L,
140 };
141 
142 #define MAXSTIR 1024.0L
143 static const long double SQTPI = 2.50662827463100050242E0L;
144 
145 /* 1/tgamma(x) = z P(z)
146  * z(x) = 1/x
147  * 0 < x < 0.03125
148  * Peak relative error 4.2e-23
149  */
150 
151 static long double S[9] = {
152 -1.193945051381510095614E-3L,
153  7.220599478036909672331E-3L,
154 -9.622023360406271645744E-3L,
155 -4.219773360705915470089E-2L,
156  1.665386113720805206758E-1L,
157 -4.200263503403344054473E-2L,
158 -6.558780715202540684668E-1L,
159  5.772156649015328608253E-1L,
160  1.000000000000000000000E0L,
161 };
162 
163 /* 1/tgamma(-x) = z P(z)
164  * z(x) = 1/x
165  * 0 < x < 0.03125
166  * Peak relative error 5.16e-23
167  * Relative error spread =  2.5e-24
168  */
169 
170 static long double SN[9] = {
171  1.133374167243894382010E-3L,
172  7.220837261893170325704E-3L,
173  9.621911155035976733706E-3L,
174 -4.219773343731191721664E-2L,
175 -1.665386113944413519335E-1L,
176 -4.200263503402112910504E-2L,
177  6.558780715202536547116E-1L,
178  5.772156649015328608727E-1L,
179 -1.000000000000000000000E0L,
180 };
181 
182 static const long double PIL = 3.1415926535897932384626L;
183 
184 extern long double __polevll(long double, void *, int);
185 static long double stirf ( long double );
186 
187 /* Gamma function computed by Stirling's formula.
188  */
189 static long double stirf(long double x)
190 {
191 long double y, w, v;
192 
193 w = 1.0L/x;
194 /* For large x, use rational coefficients from the analytical expansion.  */
195 if( x > 1024.0L )
196 	w = (((((6.97281375836585777429E-5L * w
197 		+ 7.84039221720066627474E-4L) * w
198 		- 2.29472093621399176955E-4L) * w
199 		- 2.68132716049382716049E-3L) * w
200 		+ 3.47222222222222222222E-3L) * w
201 		+ 8.33333333333333333333E-2L) * w
202 		+ 1.0L;
203 else
204 	w = 1.0L + w * __polevll( w, STIR, 8 );
205 y = expl(x);
206 if( x > MAXSTIR )
207 	{ /* Avoid overflow in pow() */
208 	v = powl( x, 0.5L * x - 0.25L );
209 	y = v * (v / y);
210 	}
211 else
212 	{
213 	y = powl( x, x - 0.5L ) / y;
214 	}
215 y = SQTPI * y * w;
216 return( y );
217 }
218 
219 long double
220 tgammal(long double x)
221 {
222 long double p, q, z;
223 int i;
224 
225 signgam = 1;
226 if( isnan(x) )
227 	return(NAN);
228 if(x == INFINITY)
229 	return(INFINITY);
230 if(x == -INFINITY)
231 	return(x - x);
232 q = fabsl(x);
233 
234 if( q > 13.0L )
235 	{
236 	if( q > MAXGAML )
237 		goto goverf;
238 	if( x < 0.0L )
239 		{
240 		p = floorl(q);
241 		if( p == q )
242 			return (x - x) / (x - x);
243 		i = p;
244 		if( (i & 1) == 0 )
245 			signgam = -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 goverf:
257 			return( signgam * INFINITY);
258 			}
259 		z = PIL/z;
260 		}
261 	else
262 		{
263 		z = stirf(x);
264 		}
265 	return( signgam * z );
266 	}
267 
268 z = 1.0L;
269 while( x >= 3.0L )
270 	{
271 	x -= 1.0L;
272 	z *= x;
273 	}
274 
275 while( x < -0.03125L )
276 	{
277 	z /= x;
278 	x += 1.0L;
279 	}
280 
281 if( x <= 0.03125L )
282 	goto small;
283 
284 while( x < 2.0L )
285 	{
286 	z /= x;
287 	x += 1.0L;
288 	}
289 
290 if( x == 2.0L )
291 	return(z);
292 
293 x -= 2.0L;
294 p = __polevll( x, P, 7 );
295 q = __polevll( x, Q, 8 );
296 z = z * p / q;
297 if( z < 0 )
298 	signgam = -1;
299 return z;
300 
301 small:
302 if( x == 0.0L )
303 	return (x - x) / (x - x);
304 else
305 	{
306 	if( x < 0.0L )
307 		{
308 		x = -x;
309 		q = z / (x * __polevll( x, SN, 8 ));
310 		signgam = -1;
311 		}
312 	else
313 		q = z / (x * __polevll( x, S, 8 ));
314 	}
315 return q;
316 }
317