#include "FEATURE/uwin"12#if !_UWIN || _lib_gamma34void _STUB_gamma(){}56#else78/*-9* Copyright (c) 1992, 199310* The Regents of the University of California. All rights reserved.11*12* Redistribution and use in source and binary forms, with or without13* modification, are permitted provided that the following conditions14* are met:15* 1. Redistributions of source code must retain the above copyright16* notice, this list of conditions and the following disclaimer.17* 2. Redistributions in binary form must reproduce the above copyright18* notice, this list of conditions and the following disclaimer in the19* documentation and/or other materials provided with the distribution.20* 3. Neither the name of the University nor the names of its contributors21* may be used to endorse or promote products derived from this software22* without specific prior written permission.23*24* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND25* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE26* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE27* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE28* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL29* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS30* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)31* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT32* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY33* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF34* SUCH DAMAGE.35*/3637#ifndef lint38static char sccsid[] = "@(#)gamma.c 8.1 (Berkeley) 6/4/93";39#endif /* not lint */4041/*42* This code by P. McIlroy, Oct 1992;43*44* The financial support of UUNET Communications Services is greatfully45* acknowledged.46*/4748#define gamma ______gamma4950#include <math.h>51#include <errno.h>52#include "mathimpl.h"5354#undef gamma5556/* METHOD:57* x < 0: Use reflection formula, G(x) = pi/(sin(pi*x)*x*G(x))58* At negative integers, return +Inf, and set errno.59*60* x < 6.5:61* Use argument reduction G(x+1) = xG(x) to reach the62* range [1.066124,2.066124]. Use a rational63* approximation centered at the minimum (x0+1) to64* ensure monotonicity.65*66* x >= 6.5: Use the asymptotic approximation (Stirling's formula)67* adjusted for equal-ripples:68*69* log(G(x)) ~= (x-.5)*(log(x)-1) + .5(log(2*pi)-1) + 1/x*P(1/(x*x))70*71* Keep extra precision in multiplying (x-.5)(log(x)-1), to72* avoid premature round-off.73*74* Special values:75* non-positive integer: Set overflow trap; return +Inf;76* x > 171.63: Set overflow trap; return +Inf;77* NaN: Set invalid trap; return NaN78*79* Accuracy: Gamma(x) is accurate to within80* x > 0: error provably < 0.9ulp.81* Maximum observed in 1,000,000 trials was .87ulp.82* x < 0:83* Maximum observed error < 4ulp in 1,000,000 trials.84*/8586static double neg_gam __P((double));87static double small_gam __P((double));88static double smaller_gam __P((double));89static struct Double large_gam __P((double));90static struct Double ratfun_gam __P((double, double));9192/*93* Rational approximation, A0 + x*x*P(x)/Q(x), on the interval94* [1.066.., 2.066..] accurate to 4.25e-19.95*/96#define LEFT -.3955078125 /* left boundary for rat. approx */97#define x0 .461632144968362356785 /* xmin - 1 */9899#define a0_hi 0.88560319441088874992100#define a0_lo -.00000000000000004996427036469019695101#define P0 6.21389571821820863029017800727e-01102#define P1 2.65757198651533466104979197553e-01103#define P2 5.53859446429917461063308081748e-03104#define P3 1.38456698304096573887145282811e-03105#define P4 2.40659950032711365819348969808e-03106#define Q0 1.45019531250000000000000000000e+00107#define Q1 1.06258521948016171343454061571e+00108#define Q2 -2.07474561943859936441469926649e-01109#define Q3 -1.46734131782005422506287573015e-01110#define Q4 3.07878176156175520361557573779e-02111#define Q5 5.12449347980666221336054633184e-03112#define Q6 -1.76012741431666995019222898833e-03113#define Q7 9.35021023573788935372153030556e-05114#define Q8 6.13275507472443958924745652239e-06115/*116* Constants for large x approximation (x in [6, Inf])117* (Accurate to 2.8*10^-19 absolute)118*/119#define lns2pi_hi 0.418945312500000120#define lns2pi_lo -.000006779295327258219670263595121#define Pa0 8.33333333333333148296162562474e-02122#define Pa1 -2.77777777774548123579378966497e-03123#define Pa2 7.93650778754435631476282786423e-04124#define Pa3 -5.95235082566672847950717262222e-04125#define Pa4 8.41428560346653702135821806252e-04126#define Pa5 -1.89773526463879200348872089421e-03127#define Pa6 5.69394463439411649408050664078e-03128#define Pa7 -1.44705562421428915453880392761e-02129130static const double zero = 0., one = 1.0, tiny = 1e-300;131static int endian;132/*133* TRUNC sets trailing bits in a floating-point number to zero.134* is a temporary variable.135*/136#if defined(vax) || defined(tahoe)137#define _IEEE 0138#define TRUNC(x) x = (double) (float) (x)139#else140#define _IEEE 1141#define TRUNC(x) *(((int *) &x) + endian) &= 0xf8000000142#define infnan(x) 0.0143#endif144145extern double gamma(x)146double x;147{148struct Double u;149endian = (*(int *) &one) ? 1 : 0;150151if (x >= 6) {152if(x > 171.63)153return(one/zero);154u = large_gam(x);155return(__exp__D(u.a, u.b));156} else if (x >= 1.0 + LEFT + x0)157return (small_gam(x));158else if (x > 1.e-17)159return (smaller_gam(x));160else if (x > -1.e-17) {161if (x == 0.0)162if (!_IEEE) return (infnan(ERANGE));163else return (one/x);164one+1e-20; /* Raise inexact flag. */165return (one/x);166} else if (!finite(x)) {167if (_IEEE) /* x = NaN, -Inf */168return (x*x);169else170return (infnan(EDOM));171} else172return (neg_gam(x));173}174/*175* Accurate to max(ulp(1/128) absolute, 2^-66 relative) error.176*/177static struct Double178large_gam(x)179double x;180{181double z, p;182struct Double t, u, v;183184z = one/(x*x);185p = Pa0+z*(Pa1+z*(Pa2+z*(Pa3+z*(Pa4+z*(Pa5+z*(Pa6+z*Pa7))))));186p = p/x;187188u = __log__D(x);189u.a -= one;190v.a = (x -= .5);191TRUNC(v.a);192v.b = x - v.a;193t.a = v.a*u.a; /* t = (x-.5)*(log(x)-1) */194t.b = v.b*u.a + x*u.b;195/* return t.a + t.b + lns2pi_hi + lns2pi_lo + p */196t.b += lns2pi_lo; t.b += p;197u.a = lns2pi_hi + t.b; u.a += t.a;198u.b = t.a - u.a;199u.b += lns2pi_hi; u.b += t.b;200return (u);201}202/*203* Good to < 1 ulp. (provably .90 ulp; .87 ulp on 1,000,000 runs.)204* It also has correct monotonicity.205*/206static double207small_gam(x)208double x;209{210double y, ym1, t;211struct Double yy, r;212y = x - one;213ym1 = y - one;214if (y <= 1.0 + (LEFT + x0)) {215yy = ratfun_gam(y - x0, 0);216return (yy.a + yy.b);217}218r.a = y;219TRUNC(r.a);220yy.a = r.a - one;221y = ym1;222yy.b = r.b = y - yy.a;223/* Argument reduction: G(x+1) = x*G(x) */224for (ym1 = y-one; ym1 > LEFT + x0; y = ym1--, yy.a--) {225t = r.a*yy.a;226r.b = r.a*yy.b + y*r.b;227r.a = t;228TRUNC(r.a);229r.b += (t - r.a);230}231/* Return r*gamma(y). */232yy = ratfun_gam(y - x0, 0);233y = r.b*(yy.a + yy.b) + r.a*yy.b;234y += yy.a*r.a;235return (y);236}237/*238* Good on (0, 1+x0+LEFT]. Accurate to 1ulp.239*/240static double241smaller_gam(x)242double x;243{244double t, d;245struct Double r, xx;246if (x < x0 + LEFT) {247t = x, TRUNC(t);248d = (t+x)*(x-t);249t *= t;250xx.a = (t + x), TRUNC(xx.a);251xx.b = x - xx.a; xx.b += t; xx.b += d;252t = (one-x0); t += x;253d = (one-x0); d -= t; d += x;254x = xx.a + xx.b;255} else {256xx.a = x, TRUNC(xx.a);257xx.b = x - xx.a;258t = x - x0;259d = (-x0 -t); d += x;260}261r = ratfun_gam(t, d);262d = r.a/x, TRUNC(d);263r.a -= d*xx.a; r.a -= d*xx.b; r.a += r.b;264return (d + r.a/x);265}266/*267* returns (z+c)^2 * P(z)/Q(z) + a0268*/269static struct Double270ratfun_gam(z, c)271double z, c;272{273double p, q;274struct Double r, t;275276q = Q0 +z*(Q1+z*(Q2+z*(Q3+z*(Q4+z*(Q5+z*(Q6+z*(Q7+z*Q8)))))));277p = P0 + z*(P1 + z*(P2 + z*(P3 + z*P4)));278279/* return r.a + r.b = a0 + (z+c)^2*p/q, with r.a truncated to 26 bits. */280p = p/q;281t.a = z, TRUNC(t.a); /* t ~= z + c */282t.b = (z - t.a) + c;283t.b *= (t.a + z);284q = (t.a *= t.a); /* t = (z+c)^2 */285TRUNC(t.a);286t.b += (q - t.a);287r.a = p, TRUNC(r.a); /* r = P/Q */288r.b = p - r.a;289t.b = t.b*p + t.a*r.b + a0_lo;290t.a *= r.a; /* t = (z+c)^2*(P/Q) */291r.a = t.a + a0_hi, TRUNC(r.a);292r.b = ((a0_hi-r.a) + t.a) + t.b;293return (r); /* r = a0 + t */294}295296static double297neg_gam(x)298double x;299{300int sgn = 1;301struct Double lg, lsine;302double y, z;303304y = floor(x + .5);305if (y == x) /* Negative integer. */306if(!_IEEE)307return (infnan(ERANGE));308else309return (one/zero);310z = fabs(x - y);311y = .5*ceil(x);312if (y == ceil(y))313sgn = -1;314if (z < .25)315z = sin(M_PI*z);316else317z = cos(M_PI*(0.5-z));318/* Special case: G(1-x) = Inf; G(x) may be nonzero. */319if (x < -170) {320if (x < -190)321return ((double)sgn*tiny*tiny);322y = one - x; /* exact: 128 < |x| < 255 */323lg = large_gam(y);324lsine = __log__D(M_PI/z); /* = TRUNC(log(u)) + small */325lg.a -= lsine.a; /* exact (opposite signs) */326lg.b -= lsine.b;327y = -(lg.a + lg.b);328z = (y + lg.a) + lg.b;329y = __exp__D(y, z);330if (sgn < 0) y = -y;331return (y);332}333y = one-x;334if (one-y == x)335y = gamma(y);336else /* 1-x is inexact */337y = -x*gamma(-x);338if (sgn < 0) y = -y;339return (M_PI / (y*z));340}341342#endif343344345