xref: /minix3/lib/libm/noieee_src/n_gamma.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /*      $NetBSD: n_gamma.c,v 1.9 2013/11/09 21:41:03 christos Exp $ */
22fe8fb19SBen Gras /*-
32fe8fb19SBen Gras  * Copyright (c) 1992, 1993
42fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
52fe8fb19SBen Gras  *
62fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
72fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
82fe8fb19SBen Gras  * are met:
92fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
102fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
112fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
122fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
132fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
142fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
152fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
162fe8fb19SBen Gras  *    without specific prior written permission.
172fe8fb19SBen Gras  *
182fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
192fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
202fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
212fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
222fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
232fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
242fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
252fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
262fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
272fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
282fe8fb19SBen Gras  * SUCH DAMAGE.
292fe8fb19SBen Gras  */
302fe8fb19SBen Gras 
312fe8fb19SBen Gras #ifndef lint
322fe8fb19SBen Gras #if 0
332fe8fb19SBen Gras static char sccsid[] = "@(#)gamma.c	8.1 (Berkeley) 6/4/93";
342fe8fb19SBen Gras #endif
352fe8fb19SBen Gras #endif /* not lint */
362fe8fb19SBen Gras 
372fe8fb19SBen Gras /*
382fe8fb19SBen Gras  * This code by P. McIlroy, Oct 1992;
392fe8fb19SBen Gras  *
402fe8fb19SBen Gras  * The financial support of UUNET Communications Services is gratefully
412fe8fb19SBen Gras  * acknowledged.
422fe8fb19SBen Gras  */
432fe8fb19SBen Gras 
442fe8fb19SBen Gras #include <math.h>
452fe8fb19SBen Gras #include "mathimpl.h"
462fe8fb19SBen Gras #include <errno.h>
472fe8fb19SBen Gras 
482fe8fb19SBen Gras /* METHOD:
492fe8fb19SBen Gras  * x < 0: Use reflection formula, G(x) = pi/(sin(pi*x)*x*G(x))
502fe8fb19SBen Gras  * 	At negative integers, return +Inf, and set errno.
512fe8fb19SBen Gras  *
522fe8fb19SBen Gras  * x < 6.5:
532fe8fb19SBen Gras  *	Use argument reduction G(x+1) = xG(x) to reach the
542fe8fb19SBen Gras  *	range [1.066124,2.066124].  Use a rational
552fe8fb19SBen Gras  *	approximation centered at the minimum (x0+1) to
562fe8fb19SBen Gras  *	ensure monotonicity.
572fe8fb19SBen Gras  *
582fe8fb19SBen Gras  * x >= 6.5: Use the asymptotic approximation (Stirling's formula)
592fe8fb19SBen Gras  *	adjusted for equal-ripples:
602fe8fb19SBen Gras  *
612fe8fb19SBen Gras  *	log(G(x)) ~= (x-.5)*(log(x)-1) + .5(log(2*pi)-1) + 1/x*P(1/(x*x))
622fe8fb19SBen Gras  *
632fe8fb19SBen Gras  *	Keep extra precision in multiplying (x-.5)(log(x)-1), to
642fe8fb19SBen Gras  *	avoid premature round-off.
652fe8fb19SBen Gras  *
662fe8fb19SBen Gras  * Special values:
672fe8fb19SBen Gras  *	non-positive integer:	Set overflow trap; return +Inf;
682fe8fb19SBen Gras  *	x > 171.63:		Set overflow trap; return +Inf;
692fe8fb19SBen Gras  *	NaN: 			Set invalid trap;  return NaN
702fe8fb19SBen Gras  *
712fe8fb19SBen Gras  * Accuracy: Gamma(x) is accurate to within
722fe8fb19SBen Gras  *	x > 0:  error provably < 0.9ulp.
732fe8fb19SBen Gras  *	Maximum observed in 1,000,000 trials was .87ulp.
742fe8fb19SBen Gras  *	x < 0:
752fe8fb19SBen Gras  *	Maximum observed error < 4ulp in 1,000,000 trials.
762fe8fb19SBen Gras  */
772fe8fb19SBen Gras 
782fe8fb19SBen Gras static double neg_gam (double);
792fe8fb19SBen Gras static double small_gam (double);
802fe8fb19SBen Gras static double smaller_gam (double);
812fe8fb19SBen Gras static struct Double large_gam (double);
822fe8fb19SBen Gras static struct Double ratfun_gam (double, double);
832fe8fb19SBen Gras 
842fe8fb19SBen Gras /*
852fe8fb19SBen Gras  * Rational approximation, A0 + x*x*P(x)/Q(x), on the interval
862fe8fb19SBen Gras  * [1.066.., 2.066..] accurate to 4.25e-19.
872fe8fb19SBen Gras  */
882fe8fb19SBen Gras #define LEFT -.3955078125	/* left boundary for rat. approx */
892fe8fb19SBen Gras #define x0 .461632144968362356785	/* xmin - 1 */
902fe8fb19SBen Gras 
912fe8fb19SBen Gras #define a0_hi 0.88560319441088874992
922fe8fb19SBen Gras #define a0_lo -.00000000000000004996427036469019695
932fe8fb19SBen Gras #define P0	 6.21389571821820863029017800727e-01
942fe8fb19SBen Gras #define P1	 2.65757198651533466104979197553e-01
952fe8fb19SBen Gras #define P2	 5.53859446429917461063308081748e-03
962fe8fb19SBen Gras #define P3	 1.38456698304096573887145282811e-03
972fe8fb19SBen Gras #define P4	 2.40659950032711365819348969808e-03
982fe8fb19SBen Gras #define Q0	 1.45019531250000000000000000000e+00
992fe8fb19SBen Gras #define Q1	 1.06258521948016171343454061571e+00
1002fe8fb19SBen Gras #define Q2	-2.07474561943859936441469926649e-01
1012fe8fb19SBen Gras #define Q3	-1.46734131782005422506287573015e-01
1022fe8fb19SBen Gras #define Q4	 3.07878176156175520361557573779e-02
1032fe8fb19SBen Gras #define Q5	 5.12449347980666221336054633184e-03
1042fe8fb19SBen Gras #define Q6	-1.76012741431666995019222898833e-03
1052fe8fb19SBen Gras #define Q7	 9.35021023573788935372153030556e-05
1062fe8fb19SBen Gras #define Q8	 6.13275507472443958924745652239e-06
1072fe8fb19SBen Gras /*
1082fe8fb19SBen Gras  * Constants for large x approximation (x in [6, Inf])
1092fe8fb19SBen Gras  * (Accurate to 2.8*10^-19 absolute)
1102fe8fb19SBen Gras  */
1112fe8fb19SBen Gras #define lns2pi_hi 0.418945312500000
1122fe8fb19SBen Gras #define lns2pi_lo -.000006779295327258219670263595
1132fe8fb19SBen Gras #define Pa0	 8.33333333333333148296162562474e-02
1142fe8fb19SBen Gras #define Pa1	-2.77777777774548123579378966497e-03
1152fe8fb19SBen Gras #define Pa2	 7.93650778754435631476282786423e-04
1162fe8fb19SBen Gras #define Pa3	-5.95235082566672847950717262222e-04
1172fe8fb19SBen Gras #define Pa4	 8.41428560346653702135821806252e-04
1182fe8fb19SBen Gras #define Pa5	-1.89773526463879200348872089421e-03
1192fe8fb19SBen Gras #define Pa6	 5.69394463439411649408050664078e-03
1202fe8fb19SBen Gras #define Pa7	-1.44705562421428915453880392761e-02
1212fe8fb19SBen Gras 
122*84d9c625SLionel Sambuc static const double zero = 0., one = 1.0, tiny = _TINY;
1232fe8fb19SBen Gras /*
1242fe8fb19SBen Gras  * TRUNC sets trailing bits in a floating-point number to zero.
1252fe8fb19SBen Gras  * is a temporary variable.
1262fe8fb19SBen Gras  */
1272fe8fb19SBen Gras #if defined(__vax__) || defined(tahoe)
1282fe8fb19SBen Gras #define _IEEE		0
1292fe8fb19SBen Gras #define TRUNC(x)	x = (double) (float) (x)
1302fe8fb19SBen Gras #else
1312fe8fb19SBen Gras static int endian;
1322fe8fb19SBen Gras #define _IEEE		1
1332fe8fb19SBen Gras #define TRUNC(x)	*(((int *) &x) + endian) &= 0xf8000000
1342fe8fb19SBen Gras #define infnan(x)	0.0
1352fe8fb19SBen Gras #endif
1362fe8fb19SBen Gras 
1372fe8fb19SBen Gras double
gamma(double x)138*84d9c625SLionel Sambuc gamma(double x)
1392fe8fb19SBen Gras {
1402fe8fb19SBen Gras 	double b;
1412fe8fb19SBen Gras 	struct Double u;
1422fe8fb19SBen Gras #if _IEEE
1432fe8fb19SBen Gras 	int endian = (*(int *) &one) ? 1 : 0;
1442fe8fb19SBen Gras #endif
1452fe8fb19SBen Gras 
1462fe8fb19SBen Gras 	if (x >= 6) {
1472fe8fb19SBen Gras 		if(x > 171.63)
1482fe8fb19SBen Gras 			return(one/zero);
1492fe8fb19SBen Gras 		u = large_gam(x);
1502fe8fb19SBen Gras 		return(__exp__D(u.a, u.b));
1512fe8fb19SBen Gras 	} else if (x >= 1.0 + LEFT + x0) {
1522fe8fb19SBen Gras 		return (small_gam(x));
1532fe8fb19SBen Gras 	} else if (x > 1.e-17) {
1542fe8fb19SBen Gras 		return (smaller_gam(x));
1552fe8fb19SBen Gras 	} else if (x > -1.e-17) {
1562fe8fb19SBen Gras 		if (x == 0.0) {
1572fe8fb19SBen Gras 			if (!_IEEE) return (infnan(ERANGE));
1582fe8fb19SBen Gras 			else return (one/x);
1592fe8fb19SBen Gras 		}
1602fe8fb19SBen Gras 		b =one+1e-20;		/* Raise inexact flag. ??? -ragge */
161*84d9c625SLionel Sambuc 		__USE(b);
1622fe8fb19SBen Gras 		return (one/x);
1632fe8fb19SBen Gras 	} else if (!finite(x)) {
1642fe8fb19SBen Gras 		if (_IEEE)		/* x = NaN, -Inf */
1652fe8fb19SBen Gras 			return (x*x);
1662fe8fb19SBen Gras 		else
1672fe8fb19SBen Gras 			return (infnan(EDOM));
1682fe8fb19SBen Gras 	 } else
1692fe8fb19SBen Gras 		return (neg_gam(x));
1702fe8fb19SBen Gras }
1712fe8fb19SBen Gras /*
1722fe8fb19SBen Gras  * Accurate to max(ulp(1/128) absolute, 2^-66 relative) error.
1732fe8fb19SBen Gras  */
1742fe8fb19SBen Gras static struct Double
large_gam(double x)1752fe8fb19SBen Gras large_gam(double x)
1762fe8fb19SBen Gras {
1772fe8fb19SBen Gras 	double z, p;
1782fe8fb19SBen Gras 	struct Double t, u, v;
1792fe8fb19SBen Gras 
1802fe8fb19SBen Gras 	z = one/(x*x);
1812fe8fb19SBen Gras 	p = Pa0+z*(Pa1+z*(Pa2+z*(Pa3+z*(Pa4+z*(Pa5+z*(Pa6+z*Pa7))))));
1822fe8fb19SBen Gras 	p = p/x;
1832fe8fb19SBen Gras 
1842fe8fb19SBen Gras 	u = __log__D(x);
1852fe8fb19SBen Gras 	u.a -= one;
1862fe8fb19SBen Gras 	v.a = (x -= .5);
1872fe8fb19SBen Gras 	TRUNC(v.a);
1882fe8fb19SBen Gras 	v.b = x - v.a;
1892fe8fb19SBen Gras 	t.a = v.a*u.a;			/* t = (x-.5)*(log(x)-1) */
1902fe8fb19SBen Gras 	t.b = v.b*u.a + x*u.b;
1912fe8fb19SBen Gras 	/* return t.a + t.b + lns2pi_hi + lns2pi_lo + p */
1922fe8fb19SBen Gras 	t.b += lns2pi_lo; t.b += p;
1932fe8fb19SBen Gras 	u.a = lns2pi_hi + t.b; u.a += t.a;
1942fe8fb19SBen Gras 	u.b = t.a - u.a;
1952fe8fb19SBen Gras 	u.b += lns2pi_hi; u.b += t.b;
1962fe8fb19SBen Gras 	return (u);
1972fe8fb19SBen Gras }
1982fe8fb19SBen Gras /*
1992fe8fb19SBen Gras  * Good to < 1 ulp.  (provably .90 ulp; .87 ulp on 1,000,000 runs.)
2002fe8fb19SBen Gras  * It also has correct monotonicity.
2012fe8fb19SBen Gras  */
2022fe8fb19SBen Gras static double
small_gam(double x)2032fe8fb19SBen Gras small_gam(double x)
2042fe8fb19SBen Gras {
2052fe8fb19SBen Gras 	double y, ym1, t;
2062fe8fb19SBen Gras 	struct Double yy, r;
2072fe8fb19SBen Gras 	y = x - one;
2082fe8fb19SBen Gras 	ym1 = y - one;
2092fe8fb19SBen Gras 	if (y <= 1.0 + (LEFT + x0)) {
2102fe8fb19SBen Gras 		yy = ratfun_gam(y - x0, 0);
2112fe8fb19SBen Gras 		return (yy.a + yy.b);
2122fe8fb19SBen Gras 	}
2132fe8fb19SBen Gras 	r.a = y;
2142fe8fb19SBen Gras 	TRUNC(r.a);
2152fe8fb19SBen Gras 	yy.a = r.a - one;
2162fe8fb19SBen Gras 	y = ym1;
2172fe8fb19SBen Gras 	yy.b = r.b = y - yy.a;
2182fe8fb19SBen Gras 	/* Argument reduction: G(x+1) = x*G(x) */
2192fe8fb19SBen Gras 	for (ym1 = y-one; ym1 > LEFT + x0; y = ym1--, yy.a--) {
2202fe8fb19SBen Gras 		t = r.a*yy.a;
2212fe8fb19SBen Gras 		r.b = r.a*yy.b + y*r.b;
2222fe8fb19SBen Gras 		r.a = t;
2232fe8fb19SBen Gras 		TRUNC(r.a);
2242fe8fb19SBen Gras 		r.b += (t - r.a);
2252fe8fb19SBen Gras 	}
2262fe8fb19SBen Gras 	/* Return r*gamma(y). */
2272fe8fb19SBen Gras 	yy = ratfun_gam(y - x0, 0);
2282fe8fb19SBen Gras 	y = r.b*(yy.a + yy.b) + r.a*yy.b;
2292fe8fb19SBen Gras 	y += yy.a*r.a;
2302fe8fb19SBen Gras 	return (y);
2312fe8fb19SBen Gras }
2322fe8fb19SBen Gras /*
2332fe8fb19SBen Gras  * Good on (0, 1+x0+LEFT].  Accurate to 1ulp.
2342fe8fb19SBen Gras  */
2352fe8fb19SBen Gras static double
smaller_gam(double x)2362fe8fb19SBen Gras smaller_gam(double x)
2372fe8fb19SBen Gras {
2382fe8fb19SBen Gras 	double t, d;
2392fe8fb19SBen Gras 	struct Double r, xx;
2402fe8fb19SBen Gras 	if (x < x0 + LEFT) {
2412fe8fb19SBen Gras 		t = x, TRUNC(t);
2422fe8fb19SBen Gras 		d = (t+x)*(x-t);
2432fe8fb19SBen Gras 		t *= t;
2442fe8fb19SBen Gras 		xx.a = (t + x), TRUNC(xx.a);
2452fe8fb19SBen Gras 		xx.b = x - xx.a; xx.b += t; xx.b += d;
2462fe8fb19SBen Gras 		t = (one-x0); t += x;
2472fe8fb19SBen Gras 		d = (one-x0); d -= t; d += x;
2482fe8fb19SBen Gras 		x = xx.a + xx.b;
2492fe8fb19SBen Gras 	} else {
2502fe8fb19SBen Gras 		xx.a =  x, TRUNC(xx.a);
2512fe8fb19SBen Gras 		xx.b = x - xx.a;
2522fe8fb19SBen Gras 		t = x - x0;
2532fe8fb19SBen Gras 		d = (-x0 -t); d += x;
2542fe8fb19SBen Gras 	}
2552fe8fb19SBen Gras 	r = ratfun_gam(t, d);
2562fe8fb19SBen Gras 	d = r.a/x, TRUNC(d);
2572fe8fb19SBen Gras 	r.a -= d*xx.a; r.a -= d*xx.b; r.a += r.b;
2582fe8fb19SBen Gras 	return (d + r.a/x);
2592fe8fb19SBen Gras }
2602fe8fb19SBen Gras /*
2612fe8fb19SBen Gras  * returns (z+c)^2 * P(z)/Q(z) + a0
2622fe8fb19SBen Gras  */
2632fe8fb19SBen Gras static struct Double
ratfun_gam(double z,double c)2642fe8fb19SBen Gras ratfun_gam(double z, double c)
2652fe8fb19SBen Gras {
2662fe8fb19SBen Gras 	double p, q;
2672fe8fb19SBen Gras 	struct Double r, t;
2682fe8fb19SBen Gras 
2692fe8fb19SBen Gras 	q = Q0 +z*(Q1+z*(Q2+z*(Q3+z*(Q4+z*(Q5+z*(Q6+z*(Q7+z*Q8)))))));
2702fe8fb19SBen Gras 	p = P0 + z*(P1 + z*(P2 + z*(P3 + z*P4)));
2712fe8fb19SBen Gras 
2722fe8fb19SBen Gras 	/* return r.a + r.b = a0 + (z+c)^2*p/q, with r.a truncated to 26 bits. */
2732fe8fb19SBen Gras 	p = p/q;
2742fe8fb19SBen Gras 	t.a = z, TRUNC(t.a);		/* t ~= z + c */
2752fe8fb19SBen Gras 	t.b = (z - t.a) + c;
2762fe8fb19SBen Gras 	t.b *= (t.a + z);
2772fe8fb19SBen Gras 	q = (t.a *= t.a);		/* t = (z+c)^2 */
2782fe8fb19SBen Gras 	TRUNC(t.a);
2792fe8fb19SBen Gras 	t.b += (q - t.a);
2802fe8fb19SBen Gras 	r.a = p, TRUNC(r.a);		/* r = P/Q */
2812fe8fb19SBen Gras 	r.b = p - r.a;
2822fe8fb19SBen Gras 	t.b = t.b*p + t.a*r.b + a0_lo;
2832fe8fb19SBen Gras 	t.a *= r.a;			/* t = (z+c)^2*(P/Q) */
2842fe8fb19SBen Gras 	r.a = t.a + a0_hi, TRUNC(r.a);
2852fe8fb19SBen Gras 	r.b = ((a0_hi-r.a) + t.a) + t.b;
2862fe8fb19SBen Gras 	return (r);			/* r = a0 + t */
2872fe8fb19SBen Gras }
2882fe8fb19SBen Gras 
2892fe8fb19SBen Gras static double
neg_gam(double x)2902fe8fb19SBen Gras neg_gam(double x)
2912fe8fb19SBen Gras {
2922fe8fb19SBen Gras 	int sgn = 1;
2932fe8fb19SBen Gras 	struct Double lg, lsine;
2942fe8fb19SBen Gras 	double y, z;
2952fe8fb19SBen Gras 
2962fe8fb19SBen Gras 	y = floor(x + .5);
2972fe8fb19SBen Gras 	if (y == x) {		/* Negative integer. */
2982fe8fb19SBen Gras 		if(!_IEEE)
2992fe8fb19SBen Gras 			return (infnan(ERANGE));
3002fe8fb19SBen Gras 		else
3012fe8fb19SBen Gras 			return (one/zero);
3022fe8fb19SBen Gras 	}
3032fe8fb19SBen Gras 	z = fabs(x - y);
3042fe8fb19SBen Gras 	y = .5*ceil(x);
3052fe8fb19SBen Gras 	if (y == ceil(y))
3062fe8fb19SBen Gras 		sgn = -1;
3072fe8fb19SBen Gras 	if (z < .25)
3082fe8fb19SBen Gras 		z = sin(M_PI*z);
3092fe8fb19SBen Gras 	else
3102fe8fb19SBen Gras 		z = cos(M_PI*(0.5-z));
3112fe8fb19SBen Gras 	/* Special case: G(1-x) = Inf; G(x) may be nonzero. */
3122fe8fb19SBen Gras 	if (x < -170) {
3132fe8fb19SBen Gras 		if (x < -190)
3142fe8fb19SBen Gras 			return ((double)sgn*tiny*tiny);
3152fe8fb19SBen Gras 		y = one - x;		/* exact: 128 < |x| < 255 */
3162fe8fb19SBen Gras 		lg = large_gam(y);
3172fe8fb19SBen Gras 		lsine = __log__D(M_PI/z);	/* = TRUNC(log(u)) + small */
3182fe8fb19SBen Gras 		lg.a -= lsine.a;		/* exact (opposite signs) */
3192fe8fb19SBen Gras 		lg.b -= lsine.b;
3202fe8fb19SBen Gras 		y = -(lg.a + lg.b);
3212fe8fb19SBen Gras 		z = (y + lg.a) + lg.b;
3222fe8fb19SBen Gras 		y = __exp__D(y, z);
3232fe8fb19SBen Gras 		if (sgn < 0) y = -y;
3242fe8fb19SBen Gras 		return (y);
3252fe8fb19SBen Gras 	}
3262fe8fb19SBen Gras 	y = one-x;
3272fe8fb19SBen Gras 	if (one-y == x)
3282fe8fb19SBen Gras 		y = gamma(y);
3292fe8fb19SBen Gras 	else		/* 1-x is inexact */
3302fe8fb19SBen Gras 		y = -x*gamma(-x);
3312fe8fb19SBen Gras 	if (sgn < 0) y = -y;
3322fe8fb19SBen Gras 	return (M_PI / (y*z));
3332fe8fb19SBen Gras }
334