11573Srgrimes/*- 21573Srgrimes * Copyright (c) 1992, 1993 31573Srgrimes * The Regents of the University of California. All rights reserved. 41573Srgrimes * 51573Srgrimes * Redistribution and use in source and binary forms, with or without 61573Srgrimes * modification, are permitted provided that the following conditions 71573Srgrimes * are met: 81573Srgrimes * 1. Redistributions of source code must retain the above copyright 91573Srgrimes * notice, this list of conditions and the following disclaimer. 101573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111573Srgrimes * notice, this list of conditions and the following disclaimer in the 121573Srgrimes * documentation and/or other materials provided with the distribution. 131573Srgrimes * 3. All advertising materials mentioning features or use of this software 141573Srgrimes * must display the following acknowledgement: 151573Srgrimes * This product includes software developed by the University of 161573Srgrimes * California, Berkeley and its contributors. 171573Srgrimes * 4. Neither the name of the University nor the names of its contributors 181573Srgrimes * may be used to endorse or promote products derived from this software 191573Srgrimes * without specific prior written permission. 201573Srgrimes * 211573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241573Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311573Srgrimes * SUCH DAMAGE. 321573Srgrimes */ 331573Srgrimes 34176449Sdas/* @(#)gamma.c 8.1 (Berkeley) 6/4/93 */ 3593211Sbde#include <sys/cdefs.h> 3692887Sobrien__FBSDID("$FreeBSD$"); 371573Srgrimes 381573Srgrimes/* 391573Srgrimes * This code by P. McIlroy, Oct 1992; 401573Srgrimes * 411573Srgrimes * The financial support of UUNET Communications Services is greatfully 421573Srgrimes * acknowledged. 431573Srgrimes */ 441573Srgrimes 451573Srgrimes#include <math.h> 461573Srgrimes#include "mathimpl.h" 471573Srgrimes 481573Srgrimes/* METHOD: 491573Srgrimes * x < 0: Use reflection formula, G(x) = pi/(sin(pi*x)*x*G(x)) 50169212Sbde * At negative integers, return NaN and raise invalid. 511573Srgrimes * 521573Srgrimes * x < 6.5: 531573Srgrimes * Use argument reduction G(x+1) = xG(x) to reach the 541573Srgrimes * range [1.066124,2.066124]. Use a rational 551573Srgrimes * approximation centered at the minimum (x0+1) to 561573Srgrimes * ensure monotonicity. 571573Srgrimes * 581573Srgrimes * x >= 6.5: Use the asymptotic approximation (Stirling's formula) 591573Srgrimes * adjusted for equal-ripples: 601573Srgrimes * 611573Srgrimes * log(G(x)) ~= (x-.5)*(log(x)-1) + .5(log(2*pi)-1) + 1/x*P(1/(x*x)) 621573Srgrimes * 631573Srgrimes * Keep extra precision in multiplying (x-.5)(log(x)-1), to 641573Srgrimes * avoid premature round-off. 651573Srgrimes * 661573Srgrimes * Special values: 67169212Sbde * -Inf: return NaN and raise invalid; 68169212Sbde * negative integer: return NaN and raise invalid; 69169209Sbde * other x ~< 177.79: return +-0 and raise underflow; 70169209Sbde * +-0: return +-Inf and raise divide-by-zero; 71169212Sbde * finite x ~> 171.63: return +Inf and raise overflow; 72169212Sbde * +Inf: return +Inf; 73169209Sbde * NaN: return NaN. 741573Srgrimes * 75169209Sbde * Accuracy: tgamma(x) is accurate to within 761573Srgrimes * x > 0: error provably < 0.9ulp. 771573Srgrimes * Maximum observed in 1,000,000 trials was .87ulp. 781573Srgrimes * x < 0: 791573Srgrimes * Maximum observed error < 4ulp in 1,000,000 trials. 801573Srgrimes */ 811573Srgrimes 8292917Sobrienstatic double neg_gam(double); 8392917Sobrienstatic double small_gam(double); 8492917Sobrienstatic double smaller_gam(double); 8592917Sobrienstatic struct Double large_gam(double); 8692917Sobrienstatic struct Double ratfun_gam(double, double); 871573Srgrimes 881573Srgrimes/* 891573Srgrimes * Rational approximation, A0 + x*x*P(x)/Q(x), on the interval 901573Srgrimes * [1.066.., 2.066..] accurate to 4.25e-19. 911573Srgrimes */ 921573Srgrimes#define LEFT -.3955078125 /* left boundary for rat. approx */ 931573Srgrimes#define x0 .461632144968362356785 /* xmin - 1 */ 941573Srgrimes 951573Srgrimes#define a0_hi 0.88560319441088874992 961573Srgrimes#define a0_lo -.00000000000000004996427036469019695 971573Srgrimes#define P0 6.21389571821820863029017800727e-01 981573Srgrimes#define P1 2.65757198651533466104979197553e-01 991573Srgrimes#define P2 5.53859446429917461063308081748e-03 1001573Srgrimes#define P3 1.38456698304096573887145282811e-03 1011573Srgrimes#define P4 2.40659950032711365819348969808e-03 1021573Srgrimes#define Q0 1.45019531250000000000000000000e+00 1031573Srgrimes#define Q1 1.06258521948016171343454061571e+00 1041573Srgrimes#define Q2 -2.07474561943859936441469926649e-01 1051573Srgrimes#define Q3 -1.46734131782005422506287573015e-01 1061573Srgrimes#define Q4 3.07878176156175520361557573779e-02 1071573Srgrimes#define Q5 5.12449347980666221336054633184e-03 1081573Srgrimes#define Q6 -1.76012741431666995019222898833e-03 1091573Srgrimes#define Q7 9.35021023573788935372153030556e-05 1101573Srgrimes#define Q8 6.13275507472443958924745652239e-06 1111573Srgrimes/* 1121573Srgrimes * Constants for large x approximation (x in [6, Inf]) 1131573Srgrimes * (Accurate to 2.8*10^-19 absolute) 1141573Srgrimes */ 1151573Srgrimes#define lns2pi_hi 0.418945312500000 1161573Srgrimes#define lns2pi_lo -.000006779295327258219670263595 1171573Srgrimes#define Pa0 8.33333333333333148296162562474e-02 1181573Srgrimes#define Pa1 -2.77777777774548123579378966497e-03 1191573Srgrimes#define Pa2 7.93650778754435631476282786423e-04 1201573Srgrimes#define Pa3 -5.95235082566672847950717262222e-04 1211573Srgrimes#define Pa4 8.41428560346653702135821806252e-04 1221573Srgrimes#define Pa5 -1.89773526463879200348872089421e-03 1231573Srgrimes#define Pa6 5.69394463439411649408050664078e-03 1241573Srgrimes#define Pa7 -1.44705562421428915453880392761e-02 1251573Srgrimes 1261573Srgrimesstatic const double zero = 0., one = 1.0, tiny = 1e-300; 127138924Sdas 1281573Srgrimesdouble 12993211Sbdetgamma(x) 1301573Srgrimes double x; 1311573Srgrimes{ 1321573Srgrimes struct Double u; 1331573Srgrimes 1341573Srgrimes if (x >= 6) { 1351573Srgrimes if(x > 171.63) 136169212Sbde return (x / zero); 1371573Srgrimes u = large_gam(x); 1381573Srgrimes return(__exp__D(u.a, u.b)); 1391573Srgrimes } else if (x >= 1.0 + LEFT + x0) 1401573Srgrimes return (small_gam(x)); 1411573Srgrimes else if (x > 1.e-17) 1421573Srgrimes return (smaller_gam(x)); 1431573Srgrimes else if (x > -1.e-17) { 144169212Sbde if (x != 0.0) 145169212Sbde u.a = one - tiny; /* raise inexact */ 1461573Srgrimes return (one/x); 147138924Sdas } else if (!finite(x)) 148169212Sbde return (x - x); /* x is NaN or -Inf */ 149138924Sdas else 1501573Srgrimes return (neg_gam(x)); 1511573Srgrimes} 1521573Srgrimes/* 1531573Srgrimes * Accurate to max(ulp(1/128) absolute, 2^-66 relative) error. 1541573Srgrimes */ 1551573Srgrimesstatic struct Double 1561573Srgrimeslarge_gam(x) 1571573Srgrimes double x; 1581573Srgrimes{ 1591573Srgrimes double z, p; 1601573Srgrimes struct Double t, u, v; 1611573Srgrimes 1621573Srgrimes z = one/(x*x); 1631573Srgrimes p = Pa0+z*(Pa1+z*(Pa2+z*(Pa3+z*(Pa4+z*(Pa5+z*(Pa6+z*Pa7)))))); 1641573Srgrimes p = p/x; 1651573Srgrimes 1661573Srgrimes u = __log__D(x); 1671573Srgrimes u.a -= one; 1681573Srgrimes v.a = (x -= .5); 1691573Srgrimes TRUNC(v.a); 1701573Srgrimes v.b = x - v.a; 1711573Srgrimes t.a = v.a*u.a; /* t = (x-.5)*(log(x)-1) */ 1721573Srgrimes t.b = v.b*u.a + x*u.b; 1731573Srgrimes /* return t.a + t.b + lns2pi_hi + lns2pi_lo + p */ 1741573Srgrimes t.b += lns2pi_lo; t.b += p; 1751573Srgrimes u.a = lns2pi_hi + t.b; u.a += t.a; 1761573Srgrimes u.b = t.a - u.a; 1771573Srgrimes u.b += lns2pi_hi; u.b += t.b; 1781573Srgrimes return (u); 1791573Srgrimes} 1801573Srgrimes/* 1811573Srgrimes * Good to < 1 ulp. (provably .90 ulp; .87 ulp on 1,000,000 runs.) 1821573Srgrimes * It also has correct monotonicity. 1831573Srgrimes */ 1841573Srgrimesstatic double 1851573Srgrimessmall_gam(x) 1861573Srgrimes double x; 1871573Srgrimes{ 188138924Sdas double y, ym1, t; 1891573Srgrimes struct Double yy, r; 1901573Srgrimes y = x - one; 1911573Srgrimes ym1 = y - one; 1921573Srgrimes if (y <= 1.0 + (LEFT + x0)) { 1931573Srgrimes yy = ratfun_gam(y - x0, 0); 1941573Srgrimes return (yy.a + yy.b); 1951573Srgrimes } 1961573Srgrimes r.a = y; 1971573Srgrimes TRUNC(r.a); 1981573Srgrimes yy.a = r.a - one; 1991573Srgrimes y = ym1; 2001573Srgrimes yy.b = r.b = y - yy.a; 2011573Srgrimes /* Argument reduction: G(x+1) = x*G(x) */ 2021573Srgrimes for (ym1 = y-one; ym1 > LEFT + x0; y = ym1--, yy.a--) { 2031573Srgrimes t = r.a*yy.a; 2041573Srgrimes r.b = r.a*yy.b + y*r.b; 2051573Srgrimes r.a = t; 2061573Srgrimes TRUNC(r.a); 2071573Srgrimes r.b += (t - r.a); 2081573Srgrimes } 20993211Sbde /* Return r*tgamma(y). */ 2101573Srgrimes yy = ratfun_gam(y - x0, 0); 2111573Srgrimes y = r.b*(yy.a + yy.b) + r.a*yy.b; 2121573Srgrimes y += yy.a*r.a; 2131573Srgrimes return (y); 2141573Srgrimes} 2151573Srgrimes/* 2161573Srgrimes * Good on (0, 1+x0+LEFT]. Accurate to 1ulp. 2171573Srgrimes */ 2181573Srgrimesstatic double 2191573Srgrimessmaller_gam(x) 2201573Srgrimes double x; 2211573Srgrimes{ 2221573Srgrimes double t, d; 2231573Srgrimes struct Double r, xx; 2241573Srgrimes if (x < x0 + LEFT) { 2251573Srgrimes t = x, TRUNC(t); 2261573Srgrimes d = (t+x)*(x-t); 2271573Srgrimes t *= t; 2281573Srgrimes xx.a = (t + x), TRUNC(xx.a); 2291573Srgrimes xx.b = x - xx.a; xx.b += t; xx.b += d; 2301573Srgrimes t = (one-x0); t += x; 2311573Srgrimes d = (one-x0); d -= t; d += x; 2321573Srgrimes x = xx.a + xx.b; 2331573Srgrimes } else { 2341573Srgrimes xx.a = x, TRUNC(xx.a); 2351573Srgrimes xx.b = x - xx.a; 2361573Srgrimes t = x - x0; 2371573Srgrimes d = (-x0 -t); d += x; 2381573Srgrimes } 2391573Srgrimes r = ratfun_gam(t, d); 2401573Srgrimes d = r.a/x, TRUNC(d); 2411573Srgrimes r.a -= d*xx.a; r.a -= d*xx.b; r.a += r.b; 2421573Srgrimes return (d + r.a/x); 2431573Srgrimes} 2441573Srgrimes/* 2451573Srgrimes * returns (z+c)^2 * P(z)/Q(z) + a0 2461573Srgrimes */ 2471573Srgrimesstatic struct Double 2481573Srgrimesratfun_gam(z, c) 2491573Srgrimes double z, c; 2501573Srgrimes{ 2511573Srgrimes double p, q; 2521573Srgrimes struct Double r, t; 2531573Srgrimes 2541573Srgrimes q = Q0 +z*(Q1+z*(Q2+z*(Q3+z*(Q4+z*(Q5+z*(Q6+z*(Q7+z*Q8))))))); 2551573Srgrimes p = P0 + z*(P1 + z*(P2 + z*(P3 + z*P4))); 2561573Srgrimes 2571573Srgrimes /* return r.a + r.b = a0 + (z+c)^2*p/q, with r.a truncated to 26 bits. */ 2581573Srgrimes p = p/q; 2591573Srgrimes t.a = z, TRUNC(t.a); /* t ~= z + c */ 2601573Srgrimes t.b = (z - t.a) + c; 2611573Srgrimes t.b *= (t.a + z); 2621573Srgrimes q = (t.a *= t.a); /* t = (z+c)^2 */ 2631573Srgrimes TRUNC(t.a); 2641573Srgrimes t.b += (q - t.a); 2651573Srgrimes r.a = p, TRUNC(r.a); /* r = P/Q */ 2661573Srgrimes r.b = p - r.a; 2671573Srgrimes t.b = t.b*p + t.a*r.b + a0_lo; 2681573Srgrimes t.a *= r.a; /* t = (z+c)^2*(P/Q) */ 2691573Srgrimes r.a = t.a + a0_hi, TRUNC(r.a); 2701573Srgrimes r.b = ((a0_hi-r.a) + t.a) + t.b; 2711573Srgrimes return (r); /* r = a0 + t */ 2721573Srgrimes} 2731573Srgrimes 2741573Srgrimesstatic double 2751573Srgrimesneg_gam(x) 2761573Srgrimes double x; 2771573Srgrimes{ 2781573Srgrimes int sgn = 1; 2791573Srgrimes struct Double lg, lsine; 2801573Srgrimes double y, z; 2811573Srgrimes 282169212Sbde y = ceil(x); 2831573Srgrimes if (y == x) /* Negative integer. */ 284169212Sbde return ((x - x) / zero); 285169212Sbde z = y - x; 286169212Sbde if (z > 0.5) 287169212Sbde z = one - z; 288169212Sbde y = 0.5 * y; 2891573Srgrimes if (y == ceil(y)) 2901573Srgrimes sgn = -1; 2911573Srgrimes if (z < .25) 2921573Srgrimes z = sin(M_PI*z); 2931573Srgrimes else 2941573Srgrimes z = cos(M_PI*(0.5-z)); 2951573Srgrimes /* Special case: G(1-x) = Inf; G(x) may be nonzero. */ 2961573Srgrimes if (x < -170) { 2971573Srgrimes if (x < -190) 2981573Srgrimes return ((double)sgn*tiny*tiny); 2991573Srgrimes y = one - x; /* exact: 128 < |x| < 255 */ 3001573Srgrimes lg = large_gam(y); 3011573Srgrimes lsine = __log__D(M_PI/z); /* = TRUNC(log(u)) + small */ 3021573Srgrimes lg.a -= lsine.a; /* exact (opposite signs) */ 3031573Srgrimes lg.b -= lsine.b; 3041573Srgrimes y = -(lg.a + lg.b); 3051573Srgrimes z = (y + lg.a) + lg.b; 3061573Srgrimes y = __exp__D(y, z); 3071573Srgrimes if (sgn < 0) y = -y; 3081573Srgrimes return (y); 3091573Srgrimes } 3101573Srgrimes y = one-x; 3111573Srgrimes if (one-y == x) 31293211Sbde y = tgamma(y); 3131573Srgrimes else /* 1-x is inexact */ 31493211Sbde y = -x*tgamma(-x); 3151573Srgrimes if (sgn < 0) y = -y; 3161573Srgrimes return (M_PI / (y*z)); 3171573Srgrimes} 318