148402Sbostic /*- 256957Sbostic * Copyright (c) 1992 The Regents of the University of California. 348402Sbostic * All rights reserved. 448402Sbostic * 556957Sbostic * %sccs.include.redist.c% 634120Sbostic */ 734120Sbostic 824600Szliu #ifndef lint 9*57452Sbostic static char sccsid[] = "@(#)lgamma.c 5.12 (Berkeley) 01/10/93"; 1034120Sbostic #endif /* not lint */ 1124600Szliu 1257152Sbostic /* 1357152Sbostic * Coded by Peter McIlroy, Nov 1992; 1457152Sbostic * 1557152Sbostic * The financial support of UUNET Communications Services is greatfully 1657152Sbostic * acknowledged. 1757152Sbostic */ 1857152Sbostic 1956952Sbostic #include <math.h> 2056952Sbostic #include <errno.h> 2124600Szliu 2256952Sbostic #include "mathimpl.h" 2324600Szliu 2457128Smcilroy /* Log gamma function. 2557128Smcilroy * Error: x > 0 error < 1.3ulp. 2657128Smcilroy * x > 4, error < 1ulp. 2757128Smcilroy * x > 9, error < .6ulp. 2857152Sbostic * x < 0, all bets are off. (When G(x) ~ 1, log(G(x)) ~ 0) 2957128Smcilroy * Method: 3057128Smcilroy * x > 6: 3157128Smcilroy * Use the asymptotic expansion (Stirling's Formula) 3257128Smcilroy * 0 < x < 6: 3357152Sbostic * Use gamma(x+1) = x*gamma(x) for argument reduction. 3457128Smcilroy * Use rational approximation in 3557128Smcilroy * the range 1.2, 2.5 3657152Sbostic * Two approximations are used, one centered at the 3757152Sbostic * minimum to ensure monotonicity; one centered at 2 3857152Sbostic * to maintain small relative error. 3957128Smcilroy * x < 0: 4057128Smcilroy * Use the reflection formula, 4157128Smcilroy * G(1-x)G(x) = PI/sin(PI*x) 4257128Smcilroy * Special values: 4357128Smcilroy * non-positive integer returns +Inf. 4457128Smcilroy * NaN returns NaN 4524600Szliu */ 4657152Sbostic static int endian; 4756952Sbostic #if defined(vax) || defined(tahoe) 4857152Sbostic #define _IEEE 0 4957128Smcilroy /* double and float have same size exponent field */ 5057152Sbostic #define TRUNC(x) x = (double) (float) (x) 5156952Sbostic #else 5257152Sbostic #define _IEEE 1 5357152Sbostic #define TRUNC(x) *(((int *) &x) + endian) &= 0xf8000000 5457152Sbostic #define infnan(x) 0.0 5556952Sbostic #endif 5635679Sbostic 5757128Smcilroy extern double log1p(double); 5857128Smcilroy static double small_lgam(double); 5957128Smcilroy static double large_lgam(double); 6057128Smcilroy static double neg_lgam(double); 6157128Smcilroy static double zero = 0.0, one = 1.0; 6257128Smcilroy int signgam; 6324600Szliu 6456952Sbostic #define UNDERFL (1e-1020 * 1e-1020) 6556952Sbostic 6657128Smcilroy #define LEFT (1.0 - (x0 + .25)) 6757128Smcilroy #define RIGHT (x0 - .218) 6857128Smcilroy /* 6957128Smcilroy /* Constants for approximation in [1.244,1.712] 7057128Smcilroy */ 7157128Smcilroy #define x0 0.461632144968362356785 7257128Smcilroy #define x0_lo -.000000000000000015522348162858676890521 7357128Smcilroy #define a0_hi -0.12148629128932952880859 7457128Smcilroy #define a0_lo .0000000007534799204229502 7557128Smcilroy #define r0 -2.771227512955130520e-002 7657128Smcilroy #define r1 -2.980729795228150847e-001 7757128Smcilroy #define r2 -3.257411333183093394e-001 7857128Smcilroy #define r3 -1.126814387531706041e-001 7957128Smcilroy #define r4 -1.129130057170225562e-002 8057128Smcilroy #define r5 -2.259650588213369095e-005 8157128Smcilroy #define s0 1.714457160001714442e+000 8257128Smcilroy #define s1 2.786469504618194648e+000 8357128Smcilroy #define s2 1.564546365519179805e+000 8457128Smcilroy #define s3 3.485846389981109850e-001 8557128Smcilroy #define s4 2.467759345363656348e-002 8657128Smcilroy /* 8757128Smcilroy * Constants for approximation in [1.71, 2.5] 8857128Smcilroy */ 8957128Smcilroy #define a1_hi 4.227843350984671344505727574870e-01 9057128Smcilroy #define a1_lo 4.670126436531227189e-18 9157128Smcilroy #define p0 3.224670334241133695662995251041e-01 9257128Smcilroy #define p1 3.569659696950364669021382724168e-01 9357128Smcilroy #define p2 1.342918716072560025853732668111e-01 9457128Smcilroy #define p3 1.950702176409779831089963408886e-02 9557128Smcilroy #define p4 8.546740251667538090796227834289e-04 9657128Smcilroy #define q0 1.000000000000000444089209850062e+00 9757128Smcilroy #define q1 1.315850076960161985084596381057e+00 9857128Smcilroy #define q2 6.274644311862156431658377186977e-01 9957128Smcilroy #define q3 1.304706631926259297049597307705e-01 10057128Smcilroy #define q4 1.102815279606722369265536798366e-02 10157128Smcilroy #define q5 2.512690594856678929537585620579e-04 10257128Smcilroy #define q6 -1.003597548112371003358107325598e-06 10357128Smcilroy /* 10457128Smcilroy * Stirling's Formula, adjusted for equal-ripple. x in [6,Inf]. 10557128Smcilroy */ 10657152Sbostic #define lns2pi .418938533204672741780329736405 10757152Sbostic #define pb0 8.33333333333333148296162562474e-02 10857152Sbostic #define pb1 -2.77777777774548123579378966497e-03 10957152Sbostic #define pb2 7.93650778754435631476282786423e-04 11057152Sbostic #define pb3 -5.95235082566672847950717262222e-04 11157152Sbostic #define pb4 8.41428560346653702135821806252e-04 11257152Sbostic #define pb5 -1.89773526463879200348872089421e-03 11357152Sbostic #define pb6 5.69394463439411649408050664078e-03 11457152Sbostic #define pb7 -1.44705562421428915453880392761e-02 11556952Sbostic 11624600Szliu double 11757128Smcilroy lgamma(double x) 11824600Szliu { 11956952Sbostic double r; 12057152Sbostic 12156952Sbostic signgam = 1; 12257152Sbostic endian = ((*(int *) &one)) ? 1 : 0; 12357152Sbostic 12457128Smcilroy if (!finite(x)) 12557128Smcilroy if (_IEEE) 12657128Smcilroy return (x+x); 12757128Smcilroy else return (infnan(EDOM)); 12857128Smcilroy 12956952Sbostic if (x > 6 + RIGHT) { 13056952Sbostic r = large_lgam(x); 13156952Sbostic return (r); 13257128Smcilroy } else if (x > 1e-16) 13356952Sbostic return (small_lgam(x)); 13457128Smcilroy else if (x > -1e-16) { 13556952Sbostic if (x < 0) 13656952Sbostic signgam = -1, x = -x; 13756952Sbostic return (-log(x)); 13856952Sbostic } else 13956952Sbostic return (neg_lgam(x)); 14024600Szliu } 14124600Szliu 14224600Szliu static double 14357128Smcilroy large_lgam(double x) 14424600Szliu { 14556952Sbostic double z, p, x1; 14624600Szliu int i; 14756952Sbostic struct Double t, u, v; 148*57452Sbostic u = __log__D(x); 14957128Smcilroy u.a -= 1.0; 15057128Smcilroy if (x > 1e15) { 15157128Smcilroy v.a = x - 0.5; 15257128Smcilroy TRUNC(v.a); 15357128Smcilroy v.b = (x - v.a) - 0.5; 15457128Smcilroy t.a = u.a*v.a; 15557128Smcilroy t.b = x*u.b + v.b*u.a; 15657128Smcilroy if (_IEEE == 0 && !finite(t.a)) 15757128Smcilroy return(infnan(ERANGE)); 15857128Smcilroy return(t.a + t.b); 15957128Smcilroy } 16057128Smcilroy x1 = 1./x; 16157128Smcilroy z = x1*x1; 16257128Smcilroy p = pb0+z*(pb1+z*(pb2+z*(pb3+z*(pb4+z*(pb5+z*(pb6+z*pb7)))))); 16357128Smcilroy /* error in approximation = 2.8e-19 */ 16424600Szliu 16557128Smcilroy p = p*x1; /* error < 2.3e-18 absolute */ 16657128Smcilroy /* 0 < p < 1/64 (at x = 5.5) */ 16757164Smcilroy v.a = x = x - 0.5; 16857128Smcilroy TRUNC(v.a); /* truncate v.a to 26 bits. */ 16956952Sbostic v.b = x - v.a; 17056952Sbostic t.a = v.a*u.a; /* t = (x-.5)*(log(x)-1) */ 17157129Smcilroy t.b = v.b*u.a + x*u.b; 17257128Smcilroy t.b += p; t.b += lns2pi; /* return t + lns2pi + p */ 17357128Smcilroy return (t.a + t.b); 17424600Szliu } 17557128Smcilroy 17624600Szliu static double 17757128Smcilroy small_lgam(double x) 17824600Szliu { 17957128Smcilroy int x_int; 18057128Smcilroy double y, z, t, r = 0, p, q, hi, lo; 18156952Sbostic struct Double rr; 18257128Smcilroy x_int = (x + .5); 18357128Smcilroy y = x - x_int; 18457128Smcilroy if (x_int <= 2 && y > RIGHT) { 18557128Smcilroy t = y - x0; 18657128Smcilroy y--; x_int++; 18757128Smcilroy goto CONTINUE; 18857128Smcilroy } else if (y < -LEFT) { 18957128Smcilroy t = y +(1.0-x0); 19057128Smcilroy CONTINUE: 19156952Sbostic z = t - x0_lo; 19256952Sbostic p = r0+z*(r1+z*(r2+z*(r3+z*(r4+z*r5)))); 19356952Sbostic q = s0+z*(s1+z*(s2+z*(s3+z*s4))); 19457128Smcilroy r = t*(z*(p/q) - x0_lo); 19557128Smcilroy t = .5*t*t; 19657128Smcilroy z = 1.0; 19757128Smcilroy switch (x_int) { 19857128Smcilroy case 6: z = (y + 5); 19957128Smcilroy case 5: z *= (y + 4); 20057128Smcilroy case 4: z *= (y + 3); 20157128Smcilroy case 3: z *= (y + 2); 202*57452Sbostic rr = __log__D(z); 20357128Smcilroy rr.b += a0_lo; rr.a += a0_hi; 20457128Smcilroy return(((r+rr.b)+t+rr.a)); 20557128Smcilroy case 2: return(((r+a0_lo)+t)+a0_hi); 20657128Smcilroy case 0: r -= log1p(x); 207*57452Sbostic default: rr = __log__D(x); 20857128Smcilroy rr.a -= a0_hi; rr.b -= a0_lo; 20957128Smcilroy return(((r - rr.b) + t) - rr.a); 21057128Smcilroy } 21156952Sbostic } else { 21257128Smcilroy p = p0+y*(p1+y*(p2+y*(p3+y*p4))); 21356952Sbostic q = q0+y*(q1+y*(q2+y*(q3+y*(q4+y*(q5+y*q6))))); 21457128Smcilroy p = p*(y/q); 21557128Smcilroy t = (double)(float) y; 21657128Smcilroy z = y-t; 21757128Smcilroy hi = (double)(float) (p+a1_hi); 21857128Smcilroy lo = a1_hi - hi; lo += p; lo += a1_lo; 21957128Smcilroy r = lo*y + z*hi; /* q + r = y*(a0+p/q) */ 22057128Smcilroy q = hi*t; 22157128Smcilroy z = 1.0; 22257128Smcilroy switch (x_int) { 22357128Smcilroy case 6: z = (y + 5); 22457128Smcilroy case 5: z *= (y + 4); 22557128Smcilroy case 4: z *= (y + 3); 22657128Smcilroy case 3: z *= (y + 2); 227*57452Sbostic rr = __log__D(z); 22857128Smcilroy r += rr.b; r += q; 22957128Smcilroy return(rr.a + r); 23057128Smcilroy case 2: return (q+ r); 231*57452Sbostic case 0: rr = __log__D(x); 23257128Smcilroy r -= rr.b; r -= log1p(x); 23357128Smcilroy r += q; r-= rr.a; 23457128Smcilroy return(r); 235*57452Sbostic default: rr = __log__D(x); 23657128Smcilroy r -= rr.b; 23757128Smcilroy q -= rr.a; 23857128Smcilroy return (r+q); 23957128Smcilroy } 24024600Szliu } 24124600Szliu } 24224600Szliu 24324600Szliu static double 24457128Smcilroy neg_lgam(double x) 24524600Szliu { 24657152Sbostic int xi; 24756952Sbostic double y, z, one = 1.0, zero = 0.0; 24857152Sbostic extern double gamma(); 24924600Szliu 25057152Sbostic /* avoid destructive cancellation as much as possible */ 25157152Sbostic if (x > -170) { 25257152Sbostic xi = x; 25357152Sbostic if (xi == x) 25457152Sbostic if (_IEEE) 25557152Sbostic return(one/zero); 25657152Sbostic else 25757152Sbostic return(infnan(ERANGE)); 25857152Sbostic y = gamma(x); 25957152Sbostic if (y < 0) 26057152Sbostic y = -y, signgam = -1; 26157152Sbostic return (log(y)); 26257152Sbostic } 26356952Sbostic z = floor(x + .5); 26457128Smcilroy if (z == x) { /* convention: G(-(integer)) -> +Inf */ 26557128Smcilroy if (_IEEE) 26657128Smcilroy return (one/zero); 26757128Smcilroy else 26857128Smcilroy return (infnan(ERANGE)); 26956952Sbostic } 27057128Smcilroy y = .5*ceil(x); 27157128Smcilroy if (y == ceil(y)) 27256952Sbostic signgam = -1; 27356952Sbostic x = -x; 27456952Sbostic z = fabs(x + z); /* 0 < z <= .5 */ 27556952Sbostic if (z < .25) 27656952Sbostic z = sin(M_PI*z); 27756952Sbostic else 27857128Smcilroy z = cos(M_PI*(0.5-z)); 27957152Sbostic z = log(M_PI/(z*x)); 28057167Smcilroy y = large_lgam(x); 28157152Sbostic return (z - y); 28224600Szliu } 283