156953Sbostic /*-
2*61297Sbostic * Copyright (c) 1992, 1993
3*61297Sbostic * The Regents of the University of California. All rights reserved.
456953Sbostic *
556953Sbostic * %sccs.include.redist.c%
656953Sbostic */
756953Sbostic
856953Sbostic #ifndef lint
9*61297Sbostic static char sccsid[] = "@(#)gamma.c 8.1 (Berkeley) 06/04/93";
1056953Sbostic #endif /* not lint */
1156953Sbostic
1257152Sbostic /*
1357152Sbostic * This code by P. McIlroy, Oct 1992;
1457152Sbostic *
1557152Sbostic * The financial support of UUNET Communications Services is greatfully
1657152Sbostic * acknowledged.
1757152Sbostic */
1857152Sbostic
1956953Sbostic #include <math.h>
2057152Sbostic #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 *
2757152Sbostic * x < 6.5:
2857152Sbostic * Use argument reduction G(x+1) = xG(x) to reach the
2957152Sbostic * range [1.066124,2.066124]. Use a rational
3057152Sbostic * approximation centered at the minimum (x0+1) to
3157152Sbostic * ensure monotonicity.
3256953Sbostic *
3357152Sbostic * x >= 6.5: Use the asymptotic approximation (Stirling's formula)
3457152Sbostic * 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 *
4157152Sbostic * Special values:
4257152Sbostic * non-positive integer: Set overflow trap; return +Inf;
4357152Sbostic * x > 171.63: Set overflow trap; return +Inf;
4457152Sbostic * NaN: Set invalid trap; return NaN
4557152Sbostic *
4657152Sbostic * Accuracy: Gamma(x) is accurate to within
4757152Sbostic * x > 0: error provably < 0.9ulp.
4857152Sbostic * Maximum observed in 1,000,000 trials was .87ulp.
4957152Sbostic * x < 0:
5057152Sbostic * Maximum observed error < 4ulp in 1,000,000 trials.
5157152Sbostic */
5257152Sbostic
5357152Sbostic static double neg_gam __P((double));
5457152Sbostic static double small_gam __P((double));
5557152Sbostic static double smaller_gam __P((double));
5657152Sbostic static struct Double large_gam __P((double));
5757152Sbostic static struct Double ratfun_gam __P((double, double));
5857152Sbostic
5957152Sbostic /*
6057152Sbostic * Rational approximation, A0 + x*x*P(x)/Q(x), on the interval
6157152Sbostic * [1.066.., 2.066..] accurate to 4.25e-19.
6257152Sbostic */
6357152Sbostic #define LEFT -.3955078125 /* left boundary for rat. approx */
6457152Sbostic #define x0 .461632144968362356785 /* xmin - 1 */
6557152Sbostic
6656953Sbostic #define a0_hi 0.88560319441088874992
6756953Sbostic #define a0_lo -.00000000000000004996427036469019695
6857152Sbostic #define P0 6.21389571821820863029017800727e-01
6957152Sbostic #define P1 2.65757198651533466104979197553e-01
7057152Sbostic #define P2 5.53859446429917461063308081748e-03
7157152Sbostic #define P3 1.38456698304096573887145282811e-03
7257152Sbostic #define P4 2.40659950032711365819348969808e-03
7357152Sbostic #define Q0 1.45019531250000000000000000000e+00
7457152Sbostic #define Q1 1.06258521948016171343454061571e+00
7557152Sbostic #define Q2 -2.07474561943859936441469926649e-01
7657152Sbostic #define Q3 -1.46734131782005422506287573015e-01
7757152Sbostic #define Q4 3.07878176156175520361557573779e-02
7857152Sbostic #define Q5 5.12449347980666221336054633184e-03
7957152Sbostic #define Q6 -1.76012741431666995019222898833e-03
8057152Sbostic #define Q7 9.35021023573788935372153030556e-05
8157152Sbostic #define Q8 6.13275507472443958924745652239e-06
8257152Sbostic /*
8357152Sbostic * Constants for large x approximation (x in [6, Inf])
8457152Sbostic * (Accurate to 2.8*10^-19 absolute)
8557152Sbostic */
8656953Sbostic #define lns2pi_hi 0.418945312500000
8756953Sbostic #define lns2pi_lo -.000006779295327258219670263595
8857152Sbostic #define Pa0 8.33333333333333148296162562474e-02
8957152Sbostic #define Pa1 -2.77777777774548123579378966497e-03
9057152Sbostic #define Pa2 7.93650778754435631476282786423e-04
9157152Sbostic #define Pa3 -5.95235082566672847950717262222e-04
9257152Sbostic #define Pa4 8.41428560346653702135821806252e-04
9357152Sbostic #define Pa5 -1.89773526463879200348872089421e-03
9457152Sbostic #define Pa6 5.69394463439411649408050664078e-03
9557152Sbostic #define Pa7 -1.44705562421428915453880392761e-02
9656953Sbostic
9757152Sbostic static const double zero = 0., one = 1.0, tiny = 1e-300;
9857152Sbostic static int endian;
9957152Sbostic /*
10057152Sbostic * TRUNC sets trailing bits in a floating-point number to zero.
10157152Sbostic * is a temporary variable.
10257152Sbostic */
10357152Sbostic #if defined(vax) || defined(tahoe)
10457152Sbostic #define _IEEE 0
10557152Sbostic #define TRUNC(x) x = (double) (float) (x)
10657152Sbostic #else
10757152Sbostic #define _IEEE 1
10857152Sbostic #define TRUNC(x) *(((int *) &x) + endian) &= 0xf8000000
10957152Sbostic #define infnan(x) 0.0
11057152Sbostic #endif
11156953Sbostic
11256953Sbostic double
gamma(x)11356953Sbostic gamma(x)
11456953Sbostic double x;
11556953Sbostic {
11656953Sbostic struct Double u;
11757152Sbostic endian = (*(int *) &one) ? 1 : 0;
11857152Sbostic
11957152Sbostic if (x >= 6) {
12056953Sbostic if(x > 171.63)
12157152Sbostic return(one/zero);
12256953Sbostic u = large_gam(x);
12357452Sbostic return(__exp__D(u.a, u.b));
12456953Sbostic } else if (x >= 1.0 + LEFT + x0)
12556953Sbostic return (small_gam(x));
12657152Sbostic else if (x > 1.e-17)
12756953Sbostic return (smaller_gam(x));
12857152Sbostic else if (x > -1.e-17) {
12957152Sbostic if (x == 0.0)
13057152Sbostic if (!_IEEE) return (infnan(ERANGE));
13157152Sbostic else return (one/x);
13257152Sbostic one+1e-20; /* Raise inexact flag. */
13357152Sbostic return (one/x);
13457152Sbostic } else if (!finite(x)) {
13557152Sbostic if (_IEEE) /* x = NaN, -Inf */
13657152Sbostic return (x*x);
13757152Sbostic else
13857152Sbostic return (infnan(EDOM));
13957152Sbostic } else
14056953Sbostic return (neg_gam(x));
14156953Sbostic }
14257152Sbostic /*
14357152Sbostic * Accurate to max(ulp(1/128) absolute, 2^-66 relative) error.
14457152Sbostic */
14556953Sbostic static struct Double
large_gam(x)14656953Sbostic large_gam(x)
14756953Sbostic double x;
14856953Sbostic {
14956953Sbostic double z, p;
15056953Sbostic int i;
15156953Sbostic struct Double t, u, v;
15257152Sbostic
15357152Sbostic z = one/(x*x);
15457152Sbostic p = Pa0+z*(Pa1+z*(Pa2+z*(Pa3+z*(Pa4+z*(Pa5+z*(Pa6+z*Pa7))))));
15557152Sbostic p = p/x;
15657152Sbostic
15757452Sbostic u = __log__D(x);
15857152Sbostic 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 */
16557152Sbostic 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 }
17157152Sbostic /*
17257152Sbostic * Good to < 1 ulp. (provably .90 ulp; .87 ulp on 1,000,000 runs.)
17357152Sbostic * It also has correct monotonicity.
17456953Sbostic */
17556953Sbostic static double
small_gam(x)17656953Sbostic small_gam(x)
17756953Sbostic double x;
17856953Sbostic {
17957152Sbostic double y, ym1, t, x1;
18056953Sbostic struct Double yy, r;
18157152Sbostic y = x - one;
18257152Sbostic ym1 = y - one;
18356953Sbostic if (y <= 1.0 + (LEFT + x0)) {
18456953Sbostic yy = ratfun_gam(y - x0, 0);
18556953Sbostic return (yy.a + yy.b);
18656953Sbostic }
18757152Sbostic r.a = y;
18856953Sbostic TRUNC(r.a);
18957152Sbostic yy.a = r.a - one;
19057152Sbostic y = ym1;
19156953Sbostic yy.b = r.b = y - yy.a;
19257152Sbostic /* Argument reduction: G(x+1) = x*G(x) */
19357152Sbostic for (ym1 = y-one; ym1 > LEFT + x0; y = ym1--, yy.a--) {
19457152Sbostic t = r.a*yy.a;
19557152Sbostic r.b = r.a*yy.b + y*r.b;
19657152Sbostic r.a = t;
19757152Sbostic TRUNC(r.a);
19857152Sbostic r.b += (t - r.a);
19957152Sbostic }
20057152Sbostic /* Return r*gamma(y). */
20156953Sbostic yy = ratfun_gam(y - x0, 0);
20257152Sbostic y = r.b*(yy.a + yy.b) + r.a*yy.b;
20356953Sbostic y += yy.a*r.a;
20456953Sbostic return (y);
20556953Sbostic }
20657152Sbostic /*
20757152Sbostic * Good on (0, 1+x0+LEFT]. Accurate to 1ulp.
20856953Sbostic */
20956953Sbostic static double
smaller_gam(x)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;
21957152Sbostic xx.a = (t + x), TRUNC(xx.a);
22056953Sbostic xx.b = x - xx.a; xx.b += t; xx.b += d;
22157152Sbostic t = (one-x0); t += x;
22257152Sbostic 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 }
23557152Sbostic /*
23657152Sbostic * returns (z+c)^2 * P(z)/Q(z) + a0
23757152Sbostic */
23856953Sbostic static struct Double
ratfun_gam(z,c)23956953Sbostic ratfun_gam(z, c)
24056953Sbostic double z, c;
24156953Sbostic {
24256953Sbostic int i;
24357152Sbostic double p, q;
24457152Sbostic 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
24957152Sbostic /* return r.a + r.b = a0 + (z+c)^2*p/q, with r.a truncated to 26 bits. */
25056953Sbostic p = p/q;
25157152Sbostic t.a = z, TRUNC(t.a); /* t ~= z + c */
25257152Sbostic t.b = (z - t.a) + c;
25357152Sbostic t.b *= (t.a + z);
25457152Sbostic q = (t.a *= t.a); /* t = (z+c)^2 */
25557152Sbostic TRUNC(t.a);
25657152Sbostic t.b += (q - t.a);
25757152Sbostic r.a = p, TRUNC(r.a); /* r = P/Q */
25857152Sbostic r.b = p - r.a;
25957152Sbostic t.b = t.b*p + t.a*r.b + a0_lo;
26057152Sbostic t.a *= r.a; /* t = (z+c)^2*(P/Q) */
26157152Sbostic r.a = t.a + a0_hi, TRUNC(r.a);
26257152Sbostic r.b = ((a0_hi-r.a) + t.a) + t.b;
26357152Sbostic return (r); /* r = a0 + t */
26456953Sbostic }
26557152Sbostic
26656953Sbostic static double
neg_gam(x)26756953Sbostic neg_gam(x)
26856953Sbostic double x;
26956953Sbostic {
27056953Sbostic int sgn = 1;
27156953Sbostic struct Double lg, lsine;
27257152Sbostic double y, z;
27357152Sbostic
27456953Sbostic y = floor(x + .5);
27557152Sbostic if (y == x) /* Negative integer. */
27657152Sbostic if(!_IEEE)
27757152Sbostic return (infnan(ERANGE));
27857152Sbostic else
27957152Sbostic return (one/zero);
28056953Sbostic z = fabs(x - y);
28157152Sbostic y = .5*ceil(x);
28257152Sbostic if (y == ceil(y))
28356953Sbostic sgn = -1;
28457152Sbostic if (z < .25)
28557152Sbostic z = sin(M_PI*z);
28657152Sbostic else
28757152Sbostic z = cos(M_PI*(0.5-z));
28857152Sbostic /* Special case: G(1-x) = Inf; G(x) may be nonzero. */
28957152Sbostic if (x < -170) {
29057152Sbostic if (x < -190)
29157152Sbostic return ((double)sgn*tiny*tiny);
29257152Sbostic y = one - x; /* exact: 128 < |x| < 255 */
29357152Sbostic lg = large_gam(y);
29457452Sbostic lsine = __log__D(M_PI/z); /* = TRUNC(log(u)) + small */
29557452Sbostic lg.a -= lsine.a; /* exact (opposite signs) */
29657152Sbostic lg.b -= lsine.b;
29757152Sbostic y = -(lg.a + lg.b);
29857171Smcilroy z = (y + lg.a) + lg.b;
29957452Sbostic y = __exp__D(y, z);
30057152Sbostic if (sgn < 0) y = -y;
30156953Sbostic return (y);
30256953Sbostic }
30357152Sbostic y = one-x;
30457152Sbostic if (one-y == x)
30557152Sbostic y = gamma(y);
30656953Sbostic else /* 1-x is inexact */
30757152Sbostic y = -x*gamma(-x);
30856953Sbostic if (sgn < 0) y = -y;
30957152Sbostic return (M_PI / (y*z));
31056953Sbostic }
311