b_tgamma.c revision 169209
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 341573Srgrimes#ifndef lint 351573Srgrimesstatic char sccsid[] = "@(#)gamma.c 8.1 (Berkeley) 6/4/93"; 361573Srgrimes#endif /* not lint */ 3793211Sbde#include <sys/cdefs.h> 3892887Sobrien__FBSDID("$FreeBSD: head/lib/msun/bsdsrc/b_tgamma.c 169209 2007-05-02 13:49:28Z bde $"); 391573Srgrimes 401573Srgrimes/* 411573Srgrimes * This code by P. McIlroy, Oct 1992; 421573Srgrimes * 431573Srgrimes * The financial support of UUNET Communications Services is greatfully 441573Srgrimes * acknowledged. 451573Srgrimes */ 461573Srgrimes 471573Srgrimes#include <math.h> 481573Srgrimes#include "mathimpl.h" 491573Srgrimes 501573Srgrimes/* METHOD: 511573Srgrimes * x < 0: Use reflection formula, G(x) = pi/(sin(pi*x)*x*G(x)) 52169209Sbde * At negative integers, return +Inf and raise divide-by-zero. 531573Srgrimes * 541573Srgrimes * x < 6.5: 551573Srgrimes * Use argument reduction G(x+1) = xG(x) to reach the 561573Srgrimes * range [1.066124,2.066124]. Use a rational 571573Srgrimes * approximation centered at the minimum (x0+1) to 581573Srgrimes * ensure monotonicity. 591573Srgrimes * 601573Srgrimes * x >= 6.5: Use the asymptotic approximation (Stirling's formula) 611573Srgrimes * adjusted for equal-ripples: 621573Srgrimes * 631573Srgrimes * log(G(x)) ~= (x-.5)*(log(x)-1) + .5(log(2*pi)-1) + 1/x*P(1/(x*x)) 641573Srgrimes * 651573Srgrimes * Keep extra precision in multiplying (x-.5)(log(x)-1), to 661573Srgrimes * avoid premature round-off. 671573Srgrimes * 681573Srgrimes * Special values: 69169209Sbde * -Inf: return +Inf (without raising any exception!); 70169209Sbde * negative integer: return +Inf and raise divide-by-zero; 71169209Sbde * other x ~< 177.79: return +-0 and raise underflow; 72169209Sbde * +-0: return +-Inf and raise divide-by-zero; 73169209Sbde * finite x ~> 171.63: return +Inf and raise divide-by-zero(!); 74169209Sbde * +Inf: return +Inf and raise divide-by-zero(!); 75169209Sbde * NaN: return NaN. 761573Srgrimes * 77169209Sbde * Accuracy: tgamma(x) is accurate to within 781573Srgrimes * x > 0: error provably < 0.9ulp. 791573Srgrimes * Maximum observed in 1,000,000 trials was .87ulp. 801573Srgrimes * x < 0: 811573Srgrimes * Maximum observed error < 4ulp in 1,000,000 trials. 821573Srgrimes */ 831573Srgrimes 8492917Sobrienstatic double neg_gam(double); 8592917Sobrienstatic double small_gam(double); 8692917Sobrienstatic double smaller_gam(double); 8792917Sobrienstatic struct Double large_gam(double); 8892917Sobrienstatic struct Double ratfun_gam(double, double); 891573Srgrimes 901573Srgrimes/* 911573Srgrimes * Rational approximation, A0 + x*x*P(x)/Q(x), on the interval 921573Srgrimes * [1.066.., 2.066..] accurate to 4.25e-19. 931573Srgrimes */ 941573Srgrimes#define LEFT -.3955078125 /* left boundary for rat. approx */ 951573Srgrimes#define x0 .461632144968362356785 /* xmin - 1 */ 961573Srgrimes 971573Srgrimes#define a0_hi 0.88560319441088874992 981573Srgrimes#define a0_lo -.00000000000000004996427036469019695 991573Srgrimes#define P0 6.21389571821820863029017800727e-01 1001573Srgrimes#define P1 2.65757198651533466104979197553e-01 1011573Srgrimes#define P2 5.53859446429917461063308081748e-03 1021573Srgrimes#define P3 1.38456698304096573887145282811e-03 1031573Srgrimes#define P4 2.40659950032711365819348969808e-03 1041573Srgrimes#define Q0 1.45019531250000000000000000000e+00 1051573Srgrimes#define Q1 1.06258521948016171343454061571e+00 1061573Srgrimes#define Q2 -2.07474561943859936441469926649e-01 1071573Srgrimes#define Q3 -1.46734131782005422506287573015e-01 1081573Srgrimes#define Q4 3.07878176156175520361557573779e-02 1091573Srgrimes#define Q5 5.12449347980666221336054633184e-03 1101573Srgrimes#define Q6 -1.76012741431666995019222898833e-03 1111573Srgrimes#define Q7 9.35021023573788935372153030556e-05 1121573Srgrimes#define Q8 6.13275507472443958924745652239e-06 1131573Srgrimes/* 1141573Srgrimes * Constants for large x approximation (x in [6, Inf]) 1151573Srgrimes * (Accurate to 2.8*10^-19 absolute) 1161573Srgrimes */ 1171573Srgrimes#define lns2pi_hi 0.418945312500000 1181573Srgrimes#define lns2pi_lo -.000006779295327258219670263595 1191573Srgrimes#define Pa0 8.33333333333333148296162562474e-02 1201573Srgrimes#define Pa1 -2.77777777774548123579378966497e-03 1211573Srgrimes#define Pa2 7.93650778754435631476282786423e-04 1221573Srgrimes#define Pa3 -5.95235082566672847950717262222e-04 1231573Srgrimes#define Pa4 8.41428560346653702135821806252e-04 1241573Srgrimes#define Pa5 -1.89773526463879200348872089421e-03 1251573Srgrimes#define Pa6 5.69394463439411649408050664078e-03 1261573Srgrimes#define Pa7 -1.44705562421428915453880392761e-02 1271573Srgrimes 1281573Srgrimesstatic const double zero = 0., one = 1.0, tiny = 1e-300; 129138924Sdas 1301573Srgrimesdouble 13193211Sbdetgamma(x) 1321573Srgrimes double x; 1331573Srgrimes{ 1341573Srgrimes struct Double u; 1351573Srgrimes 1361573Srgrimes if (x >= 6) { 1371573Srgrimes if(x > 171.63) 1381573Srgrimes return(one/zero); 1391573Srgrimes u = large_gam(x); 1401573Srgrimes return(__exp__D(u.a, u.b)); 1411573Srgrimes } else if (x >= 1.0 + LEFT + x0) 1421573Srgrimes return (small_gam(x)); 1431573Srgrimes else if (x > 1.e-17) 1441573Srgrimes return (smaller_gam(x)); 1451573Srgrimes else if (x > -1.e-17) { 1461573Srgrimes if (x == 0.0) 147138924Sdas return (one/x); 1481573Srgrimes one+1e-20; /* Raise inexact flag. */ 1491573Srgrimes return (one/x); 150138924Sdas } else if (!finite(x)) 151138924Sdas return (x*x); /* x = NaN, -Inf */ 152138924Sdas else 1531573Srgrimes return (neg_gam(x)); 1541573Srgrimes} 1551573Srgrimes/* 1561573Srgrimes * Accurate to max(ulp(1/128) absolute, 2^-66 relative) error. 1571573Srgrimes */ 1581573Srgrimesstatic struct Double 1591573Srgrimeslarge_gam(x) 1601573Srgrimes double x; 1611573Srgrimes{ 1621573Srgrimes double z, p; 1631573Srgrimes struct Double t, u, v; 1641573Srgrimes 1651573Srgrimes z = one/(x*x); 1661573Srgrimes p = Pa0+z*(Pa1+z*(Pa2+z*(Pa3+z*(Pa4+z*(Pa5+z*(Pa6+z*Pa7)))))); 1671573Srgrimes p = p/x; 1681573Srgrimes 1691573Srgrimes u = __log__D(x); 1701573Srgrimes u.a -= one; 1711573Srgrimes v.a = (x -= .5); 1721573Srgrimes TRUNC(v.a); 1731573Srgrimes v.b = x - v.a; 1741573Srgrimes t.a = v.a*u.a; /* t = (x-.5)*(log(x)-1) */ 1751573Srgrimes t.b = v.b*u.a + x*u.b; 1761573Srgrimes /* return t.a + t.b + lns2pi_hi + lns2pi_lo + p */ 1771573Srgrimes t.b += lns2pi_lo; t.b += p; 1781573Srgrimes u.a = lns2pi_hi + t.b; u.a += t.a; 1791573Srgrimes u.b = t.a - u.a; 1801573Srgrimes u.b += lns2pi_hi; u.b += t.b; 1811573Srgrimes return (u); 1821573Srgrimes} 1831573Srgrimes/* 1841573Srgrimes * Good to < 1 ulp. (provably .90 ulp; .87 ulp on 1,000,000 runs.) 1851573Srgrimes * It also has correct monotonicity. 1861573Srgrimes */ 1871573Srgrimesstatic double 1881573Srgrimessmall_gam(x) 1891573Srgrimes double x; 1901573Srgrimes{ 191138924Sdas double y, ym1, t; 1921573Srgrimes struct Double yy, r; 1931573Srgrimes y = x - one; 1941573Srgrimes ym1 = y - one; 1951573Srgrimes if (y <= 1.0 + (LEFT + x0)) { 1961573Srgrimes yy = ratfun_gam(y - x0, 0); 1971573Srgrimes return (yy.a + yy.b); 1981573Srgrimes } 1991573Srgrimes r.a = y; 2001573Srgrimes TRUNC(r.a); 2011573Srgrimes yy.a = r.a - one; 2021573Srgrimes y = ym1; 2031573Srgrimes yy.b = r.b = y - yy.a; 2041573Srgrimes /* Argument reduction: G(x+1) = x*G(x) */ 2051573Srgrimes for (ym1 = y-one; ym1 > LEFT + x0; y = ym1--, yy.a--) { 2061573Srgrimes t = r.a*yy.a; 2071573Srgrimes r.b = r.a*yy.b + y*r.b; 2081573Srgrimes r.a = t; 2091573Srgrimes TRUNC(r.a); 2101573Srgrimes r.b += (t - r.a); 2111573Srgrimes } 21293211Sbde /* Return r*tgamma(y). */ 2131573Srgrimes yy = ratfun_gam(y - x0, 0); 2141573Srgrimes y = r.b*(yy.a + yy.b) + r.a*yy.b; 2151573Srgrimes y += yy.a*r.a; 2161573Srgrimes return (y); 2171573Srgrimes} 2181573Srgrimes/* 2191573Srgrimes * Good on (0, 1+x0+LEFT]. Accurate to 1ulp. 2201573Srgrimes */ 2211573Srgrimesstatic double 2221573Srgrimessmaller_gam(x) 2231573Srgrimes double x; 2241573Srgrimes{ 2251573Srgrimes double t, d; 2261573Srgrimes struct Double r, xx; 2271573Srgrimes if (x < x0 + LEFT) { 2281573Srgrimes t = x, TRUNC(t); 2291573Srgrimes d = (t+x)*(x-t); 2301573Srgrimes t *= t; 2311573Srgrimes xx.a = (t + x), TRUNC(xx.a); 2321573Srgrimes xx.b = x - xx.a; xx.b += t; xx.b += d; 2331573Srgrimes t = (one-x0); t += x; 2341573Srgrimes d = (one-x0); d -= t; d += x; 2351573Srgrimes x = xx.a + xx.b; 2361573Srgrimes } else { 2371573Srgrimes xx.a = x, TRUNC(xx.a); 2381573Srgrimes xx.b = x - xx.a; 2391573Srgrimes t = x - x0; 2401573Srgrimes d = (-x0 -t); d += x; 2411573Srgrimes } 2421573Srgrimes r = ratfun_gam(t, d); 2431573Srgrimes d = r.a/x, TRUNC(d); 2441573Srgrimes r.a -= d*xx.a; r.a -= d*xx.b; r.a += r.b; 2451573Srgrimes return (d + r.a/x); 2461573Srgrimes} 2471573Srgrimes/* 2481573Srgrimes * returns (z+c)^2 * P(z)/Q(z) + a0 2491573Srgrimes */ 2501573Srgrimesstatic struct Double 2511573Srgrimesratfun_gam(z, c) 2521573Srgrimes double z, c; 2531573Srgrimes{ 2541573Srgrimes double p, q; 2551573Srgrimes struct Double r, t; 2561573Srgrimes 2571573Srgrimes q = Q0 +z*(Q1+z*(Q2+z*(Q3+z*(Q4+z*(Q5+z*(Q6+z*(Q7+z*Q8))))))); 2581573Srgrimes p = P0 + z*(P1 + z*(P2 + z*(P3 + z*P4))); 2591573Srgrimes 2601573Srgrimes /* return r.a + r.b = a0 + (z+c)^2*p/q, with r.a truncated to 26 bits. */ 2611573Srgrimes p = p/q; 2621573Srgrimes t.a = z, TRUNC(t.a); /* t ~= z + c */ 2631573Srgrimes t.b = (z - t.a) + c; 2641573Srgrimes t.b *= (t.a + z); 2651573Srgrimes q = (t.a *= t.a); /* t = (z+c)^2 */ 2661573Srgrimes TRUNC(t.a); 2671573Srgrimes t.b += (q - t.a); 2681573Srgrimes r.a = p, TRUNC(r.a); /* r = P/Q */ 2691573Srgrimes r.b = p - r.a; 2701573Srgrimes t.b = t.b*p + t.a*r.b + a0_lo; 2711573Srgrimes t.a *= r.a; /* t = (z+c)^2*(P/Q) */ 2721573Srgrimes r.a = t.a + a0_hi, TRUNC(r.a); 2731573Srgrimes r.b = ((a0_hi-r.a) + t.a) + t.b; 2741573Srgrimes return (r); /* r = a0 + t */ 2751573Srgrimes} 2761573Srgrimes 2771573Srgrimesstatic double 2781573Srgrimesneg_gam(x) 2791573Srgrimes double x; 2801573Srgrimes{ 2811573Srgrimes int sgn = 1; 2821573Srgrimes struct Double lg, lsine; 2831573Srgrimes double y, z; 2841573Srgrimes 2851573Srgrimes y = floor(x + .5); 2861573Srgrimes if (y == x) /* Negative integer. */ 287138924Sdas return (one/zero); 2881573Srgrimes z = fabs(x - y); 2891573Srgrimes y = .5*ceil(x); 2901573Srgrimes if (y == ceil(y)) 2911573Srgrimes sgn = -1; 2921573Srgrimes if (z < .25) 2931573Srgrimes z = sin(M_PI*z); 2941573Srgrimes else 2951573Srgrimes z = cos(M_PI*(0.5-z)); 2961573Srgrimes /* Special case: G(1-x) = Inf; G(x) may be nonzero. */ 2971573Srgrimes if (x < -170) { 2981573Srgrimes if (x < -190) 2991573Srgrimes return ((double)sgn*tiny*tiny); 3001573Srgrimes y = one - x; /* exact: 128 < |x| < 255 */ 3011573Srgrimes lg = large_gam(y); 3021573Srgrimes lsine = __log__D(M_PI/z); /* = TRUNC(log(u)) + small */ 3031573Srgrimes lg.a -= lsine.a; /* exact (opposite signs) */ 3041573Srgrimes lg.b -= lsine.b; 3051573Srgrimes y = -(lg.a + lg.b); 3061573Srgrimes z = (y + lg.a) + lg.b; 3071573Srgrimes y = __exp__D(y, z); 3081573Srgrimes if (sgn < 0) y = -y; 3091573Srgrimes return (y); 3101573Srgrimes } 3111573Srgrimes y = one-x; 3121573Srgrimes if (one-y == x) 31393211Sbde y = tgamma(y); 3141573Srgrimes else /* 1-x is inexact */ 31593211Sbde y = -x*tgamma(-x); 3161573Srgrimes if (sgn < 0) y = -y; 3171573Srgrimes return (M_PI / (y*z)); 3181573Srgrimes} 319