xref: /csrg-svn/lib/libm/common_source/gamma.c (revision 57152)
156953Sbostic /*-
256953Sbostic  * Copyright (c) 1992 The Regents of the University of California.
356953Sbostic  * All rights reserved.
456953Sbostic  *
556953Sbostic  * %sccs.include.redist.c%
656953Sbostic  */
756953Sbostic 
856953Sbostic #ifndef lint
9*57152Sbostic static char sccsid[] = "@(#)gamma.c	5.2 (Berkeley) 12/16/92";
1056953Sbostic #endif /* not lint */
1156953Sbostic 
12*57152Sbostic /*
13*57152Sbostic  * This code by P. McIlroy, Oct 1992;
14*57152Sbostic  *
15*57152Sbostic  * The financial support of UUNET Communications Services is greatfully
16*57152Sbostic  * acknowledged.
17*57152Sbostic  */
18*57152Sbostic 
1956953Sbostic #include <math.h>
20*57152Sbostic #include "mathimpl.h"
2156953Sbostic #include <errno.h>
2256953Sbostic 
2356953Sbostic /* METHOD:
2456953Sbostic  * x < 0: Use reflection formula, G(x) = pi/(sin(pi*x)*x*G(x))
2556953Sbostic  * 	At negative integers, return +Inf, and set errno.
2656953Sbostic  *
27*57152Sbostic  * x < 6.5:
28*57152Sbostic  *	Use argument reduction G(x+1) = xG(x) to reach the
29*57152Sbostic  *	range [1.066124,2.066124].  Use a rational
30*57152Sbostic  *	approximation centered at the minimum (x0+1) to
31*57152Sbostic  *	ensure monotonicity.
3256953Sbostic  *
33*57152Sbostic  * x >= 6.5: Use the asymptotic approximation (Stirling's formula)
34*57152Sbostic  *	adjusted for equal-ripples:
3556953Sbostic  *
3656953Sbostic  *	log(G(x)) ~= (x-.5)*(log(x)-1) + .5(log(2*pi)-1) + 1/x*P(1/(x*x))
3756953Sbostic  *
3856953Sbostic  *	Keep extra precision in multiplying (x-.5)(log(x)-1), to
3956953Sbostic  *	avoid premature round-off.
4056953Sbostic  *
41*57152Sbostic  * Special values:
42*57152Sbostic  *	non-positive integer:	Set overflow trap; return +Inf;
43*57152Sbostic  *	x > 171.63:		Set overflow trap; return +Inf;
44*57152Sbostic  *	NaN: 			Set invalid trap;  return NaN
45*57152Sbostic  *
46*57152Sbostic  * Accuracy: Gamma(x) is accurate to within
47*57152Sbostic  *	x > 0:  error provably < 0.9ulp.
48*57152Sbostic  *	Maximum observed in 1,000,000 trials was .87ulp.
49*57152Sbostic  *	x < 0:
50*57152Sbostic  *	Maximum observed error < 4ulp in 1,000,000 trials.
51*57152Sbostic  */
52*57152Sbostic 
53*57152Sbostic static double neg_gam __P((double));
54*57152Sbostic static double small_gam __P((double));
55*57152Sbostic static double smaller_gam __P((double));
56*57152Sbostic static struct Double large_gam __P((double));
57*57152Sbostic static struct Double ratfun_gam __P((double, double));
58*57152Sbostic 
59*57152Sbostic /*
60*57152Sbostic  * Rational approximation, A0 + x*x*P(x)/Q(x), on the interval
61*57152Sbostic  * [1.066.., 2.066..] accurate to 4.25e-19.
62*57152Sbostic  */
63*57152Sbostic #define LEFT -.3955078125	/* left boundary for rat. approx */
64*57152Sbostic #define x0 .461632144968362356785	/* xmin - 1 */
65*57152Sbostic 
6656953Sbostic #define a0_hi 0.88560319441088874992
6756953Sbostic #define a0_lo -.00000000000000004996427036469019695
68*57152Sbostic #define P0	 6.21389571821820863029017800727e-01
69*57152Sbostic #define P1	 2.65757198651533466104979197553e-01
70*57152Sbostic #define P2	 5.53859446429917461063308081748e-03
71*57152Sbostic #define P3	 1.38456698304096573887145282811e-03
72*57152Sbostic #define P4	 2.40659950032711365819348969808e-03
73*57152Sbostic #define Q0	 1.45019531250000000000000000000e+00
74*57152Sbostic #define Q1	 1.06258521948016171343454061571e+00
75*57152Sbostic #define Q2	-2.07474561943859936441469926649e-01
76*57152Sbostic #define Q3	-1.46734131782005422506287573015e-01
77*57152Sbostic #define Q4	 3.07878176156175520361557573779e-02
78*57152Sbostic #define Q5	 5.12449347980666221336054633184e-03
79*57152Sbostic #define Q6	-1.76012741431666995019222898833e-03
80*57152Sbostic #define Q7	 9.35021023573788935372153030556e-05
81*57152Sbostic #define Q8	 6.13275507472443958924745652239e-06
82*57152Sbostic /*
83*57152Sbostic  * Constants for large x approximation (x in [6, Inf])
84*57152Sbostic  * (Accurate to 2.8*10^-19 absolute)
85*57152Sbostic  */
8656953Sbostic #define lns2pi_hi 0.418945312500000
8756953Sbostic #define lns2pi_lo -.000006779295327258219670263595
88*57152Sbostic #define Pa0	 8.33333333333333148296162562474e-02
89*57152Sbostic #define Pa1	-2.77777777774548123579378966497e-03
90*57152Sbostic #define Pa2	 7.93650778754435631476282786423e-04
91*57152Sbostic #define Pa3	-5.95235082566672847950717262222e-04
92*57152Sbostic #define Pa4	 8.41428560346653702135821806252e-04
93*57152Sbostic #define Pa5	-1.89773526463879200348872089421e-03
94*57152Sbostic #define Pa6	 5.69394463439411649408050664078e-03
95*57152Sbostic #define Pa7	-1.44705562421428915453880392761e-02
9656953Sbostic 
97*57152Sbostic static const double zero = 0., one = 1.0, tiny = 1e-300;
98*57152Sbostic static int endian;
99*57152Sbostic /*
100*57152Sbostic  * TRUNC sets trailing bits in a floating-point number to zero.
101*57152Sbostic  * is a temporary variable.
102*57152Sbostic  */
103*57152Sbostic #if defined(vax) || defined(tahoe)
104*57152Sbostic #define _IEEE		0
105*57152Sbostic #define TRUNC(x)	x = (double) (float) (x)
106*57152Sbostic #else
107*57152Sbostic #define _IEEE		1
108*57152Sbostic #define TRUNC(x)	*(((int *) &x) + endian) &= 0xf8000000
109*57152Sbostic #define infnan(x)	0.0
110*57152Sbostic #endif
11156953Sbostic 
11256953Sbostic double
11356953Sbostic gamma(x)
11456953Sbostic 	double x;
11556953Sbostic {
11656953Sbostic 	struct Double u;
117*57152Sbostic 	endian = (*(int *) &one) ? 1 : 0;
118*57152Sbostic 
119*57152Sbostic 	if (x >= 6) {
12056953Sbostic 		if(x > 171.63)
121*57152Sbostic 			return(one/zero);
12256953Sbostic 		u = large_gam(x);
12356953Sbostic 		return(exp__D(u.a, u.b));
12456953Sbostic 	} else if (x >= 1.0 + LEFT + x0)
12556953Sbostic 		return (small_gam(x));
126*57152Sbostic 	else if (x > 1.e-17)
12756953Sbostic 		return (smaller_gam(x));
128*57152Sbostic 	else if (x > -1.e-17) {
129*57152Sbostic 		if (x == 0.0)
130*57152Sbostic 			if (!_IEEE) return (infnan(ERANGE));
131*57152Sbostic 			else return (one/x);
132*57152Sbostic 		one+1e-20;		/* Raise inexact flag. */
133*57152Sbostic 		return (one/x);
134*57152Sbostic 	} else if (!finite(x)) {
135*57152Sbostic 		if (_IEEE)		/* x = NaN, -Inf */
136*57152Sbostic 			return (x*x);
137*57152Sbostic 		else
138*57152Sbostic 			return (infnan(EDOM));
139*57152Sbostic 	 } else
14056953Sbostic 		return (neg_gam(x));
14156953Sbostic }
142*57152Sbostic /*
143*57152Sbostic  * Accurate to max(ulp(1/128) absolute, 2^-66 relative) error.
144*57152Sbostic  */
14556953Sbostic static struct Double
14656953Sbostic large_gam(x)
14756953Sbostic 	double x;
14856953Sbostic {
14956953Sbostic 	double z, p;
15056953Sbostic 	int i;
15156953Sbostic 	struct Double t, u, v;
152*57152Sbostic 
153*57152Sbostic 	z = one/(x*x);
154*57152Sbostic 	p = Pa0+z*(Pa1+z*(Pa2+z*(Pa3+z*(Pa4+z*(Pa5+z*(Pa6+z*Pa7))))));
155*57152Sbostic 	p = p/x;
156*57152Sbostic 
15756953Sbostic 	u = log__D(x);
158*57152Sbostic 	u.a -= one;
15956953Sbostic 	v.a = (x -= .5);
16056953Sbostic 	TRUNC(v.a);
16156953Sbostic 	v.b = x - v.a;
16256953Sbostic 	t.a = v.a*u.a;			/* t = (x-.5)*(log(x)-1) */
16356953Sbostic 	t.b = v.b*u.a + x*u.b;
16456953Sbostic 	/* return t.a + t.b + lns2pi_hi + lns2pi_lo + p */
165*57152Sbostic 	t.b += lns2pi_lo; t.b += p;
16656953Sbostic 	u.a = lns2pi_hi + t.b; u.a += t.a;
16756953Sbostic 	u.b = t.a - u.a;
16856953Sbostic 	u.b += lns2pi_hi; u.b += t.b;
16956953Sbostic 	return (u);
17056953Sbostic }
171*57152Sbostic /*
172*57152Sbostic  * Good to < 1 ulp.  (provably .90 ulp; .87 ulp on 1,000,000 runs.)
173*57152Sbostic  * It also has correct monotonicity.
17456953Sbostic  */
17556953Sbostic static double
17656953Sbostic small_gam(x)
17756953Sbostic 	double x;
17856953Sbostic {
179*57152Sbostic 	double y, ym1, t, x1;
18056953Sbostic 	struct Double yy, r;
181*57152Sbostic 	y = x - one;
182*57152Sbostic 	ym1 = y - one;
18356953Sbostic 	if (y <= 1.0 + (LEFT + x0)) {
18456953Sbostic 		yy = ratfun_gam(y - x0, 0);
18556953Sbostic 		return (yy.a + yy.b);
18656953Sbostic 	}
187*57152Sbostic 	r.a = y;
18856953Sbostic 	TRUNC(r.a);
189*57152Sbostic 	yy.a = r.a - one;
190*57152Sbostic 	y = ym1;
19156953Sbostic 	yy.b = r.b = y - yy.a;
192*57152Sbostic 	/* Argument reduction: G(x+1) = x*G(x) */
193*57152Sbostic 	for (ym1 = y-one; ym1 > LEFT + x0; y = ym1--, yy.a--) {
194*57152Sbostic 		t = r.a*yy.a;
195*57152Sbostic 		r.b = r.a*yy.b + y*r.b;
196*57152Sbostic 		r.a = t;
197*57152Sbostic 		TRUNC(r.a);
198*57152Sbostic 		r.b += (t - r.a);
199*57152Sbostic 	}
200*57152Sbostic 	/* Return r*gamma(y). */
20156953Sbostic 	yy = ratfun_gam(y - x0, 0);
202*57152Sbostic 	y = r.b*(yy.a + yy.b) + r.a*yy.b;
20356953Sbostic 	y += yy.a*r.a;
20456953Sbostic 	return (y);
20556953Sbostic }
206*57152Sbostic /*
207*57152Sbostic  * Good on (0, 1+x0+LEFT].  Accurate to 1ulp.
20856953Sbostic  */
20956953Sbostic static double
21056953Sbostic smaller_gam(x)
21156953Sbostic 	double x;
21256953Sbostic {
21356953Sbostic 	double t, d;
21456953Sbostic 	struct Double r, xx;
21556953Sbostic 	if (x < x0 + LEFT) {
21656953Sbostic 		t = x, TRUNC(t);
21756953Sbostic 		d = (t+x)*(x-t);
21856953Sbostic 		t *= t;
219*57152Sbostic 		xx.a = (t + x), TRUNC(xx.a);
22056953Sbostic 		xx.b = x - xx.a; xx.b += t; xx.b += d;
221*57152Sbostic 		t = (one-x0); t += x;
222*57152Sbostic 		d = (one-x0); d -= t; d += x;
22356953Sbostic 		x = xx.a + xx.b;
22456953Sbostic 	} else {
22556953Sbostic 		xx.a =  x, TRUNC(xx.a);
22656953Sbostic 		xx.b = x - xx.a;
22756953Sbostic 		t = x - x0;
22856953Sbostic 		d = (-x0 -t); d += x;
22956953Sbostic 	}
23056953Sbostic 	r = ratfun_gam(t, d);
23156953Sbostic 	d = r.a/x, TRUNC(d);
23256953Sbostic 	r.a -= d*xx.a; r.a -= d*xx.b; r.a += r.b;
23356953Sbostic 	return (d + r.a/x);
23456953Sbostic }
235*57152Sbostic /*
236*57152Sbostic  * returns (z+c)^2 * P(z)/Q(z) + a0
237*57152Sbostic  */
23856953Sbostic static struct Double
23956953Sbostic ratfun_gam(z, c)
24056953Sbostic 	double z, c;
24156953Sbostic {
24256953Sbostic 	int i;
243*57152Sbostic 	double p, q;
244*57152Sbostic 	struct Double r, t;
24556953Sbostic 
24656953Sbostic 	q = Q0 +z*(Q1+z*(Q2+z*(Q3+z*(Q4+z*(Q5+z*(Q6+z*(Q7+z*Q8)))))));
24756953Sbostic 	p = P0 + z*(P1 + z*(P2 + z*(P3 + z*P4)));
24856953Sbostic 
249*57152Sbostic 	/* return r.a + r.b = a0 + (z+c)^2*p/q, with r.a truncated to 26 bits. */
25056953Sbostic 	p = p/q;
251*57152Sbostic 	t.a = z, TRUNC(t.a);		/* t ~= z + c */
252*57152Sbostic 	t.b = (z - t.a) + c;
253*57152Sbostic 	t.b *= (t.a + z);
254*57152Sbostic 	q = (t.a *= t.a);		/* t = (z+c)^2 */
255*57152Sbostic 	TRUNC(t.a);
256*57152Sbostic 	t.b += (q - t.a);
257*57152Sbostic 	r.a = p, TRUNC(r.a);		/* r = P/Q */
258*57152Sbostic 	r.b = p - r.a;
259*57152Sbostic 	t.b = t.b*p + t.a*r.b + a0_lo;
260*57152Sbostic 	t.a *= r.a;			/* t = (z+c)^2*(P/Q) */
261*57152Sbostic 	r.a = t.a + a0_hi, TRUNC(r.a);
262*57152Sbostic 	r.b = ((a0_hi-r.a) + t.a) + t.b;
263*57152Sbostic 	return (r);			/* r = a0 + t */
26456953Sbostic }
265*57152Sbostic 
26656953Sbostic static double
26756953Sbostic neg_gam(x)
26856953Sbostic 	double x;
26956953Sbostic {
27056953Sbostic 	int sgn = 1;
27156953Sbostic 	struct Double lg, lsine;
272*57152Sbostic 	double y, z;
273*57152Sbostic 
27456953Sbostic 	y = floor(x + .5);
275*57152Sbostic 	if (y == x)		/* Negative integer. */
276*57152Sbostic 		if(!_IEEE)
277*57152Sbostic 			return (infnan(ERANGE));
278*57152Sbostic 		else
279*57152Sbostic 			return (one/zero);
28056953Sbostic 	z = fabs(x - y);
281*57152Sbostic 	y = .5*ceil(x);
282*57152Sbostic 	if (y == ceil(y))
28356953Sbostic 		sgn = -1;
284*57152Sbostic 	if (z < .25)
285*57152Sbostic 		z = sin(M_PI*z);
286*57152Sbostic 	else
287*57152Sbostic 		z = cos(M_PI*(0.5-z));
288*57152Sbostic 	/* Special case: G(1-x) = Inf; G(x) may be nonzero. */
289*57152Sbostic 	if (x < -170) {
290*57152Sbostic 		if (x < -190)
291*57152Sbostic 			return ((double)sgn*tiny*tiny);
292*57152Sbostic 		y = one - x;		/* exact: 128 < |x| < 255 */
293*57152Sbostic 		lg = large_gam(y);
294*57152Sbostic 		lsine = log__D(M_PI/z);	/* = TRUNC(log(u)) + small */
295*57152Sbostic 		lg.a -= lsine.a;	/* exact (opposite signs) */
296*57152Sbostic 		lg.b -= lsine.b;
297*57152Sbostic 		y = -(lg.a + lg.b);
298*57152Sbostic 		z = (y - lg.a) - lg.b;
29956953Sbostic 		y = exp__D(y, z);
300*57152Sbostic 		if (sgn < 0) y = -y;
30156953Sbostic 		return (y);
30256953Sbostic 	}
303*57152Sbostic 	y = one-x;
304*57152Sbostic 	if (one-y == x)
305*57152Sbostic 		y = gamma(y);
30656953Sbostic 	else		/* 1-x is inexact */
307*57152Sbostic 		y = -x*gamma(-x);
30856953Sbostic 	if (sgn < 0) y = -y;
309*57152Sbostic 	return (M_PI / (y*z));
31056953Sbostic }
311