xref: /openbsd-src/lib/libm/src/b_tgamma.c (revision 2f2c00629eff6a304ebffb255fc56f4fa7a1833b)
1*2f2c0062Sguenther /*	$OpenBSD: b_tgamma.c,v 1.10 2016/09/12 19:47:02 guenther Exp $	*/
25b14c9c6Smartynas /*-
35b14c9c6Smartynas  * Copyright (c) 1992, 1993
45b14c9c6Smartynas  *	The Regents of the University of California.  All rights reserved.
55b14c9c6Smartynas  *
65b14c9c6Smartynas  * Redistribution and use in source and binary forms, with or without
75b14c9c6Smartynas  * modification, are permitted provided that the following conditions
85b14c9c6Smartynas  * are met:
95b14c9c6Smartynas  * 1. Redistributions of source code must retain the above copyright
105b14c9c6Smartynas  *    notice, this list of conditions and the following disclaimer.
115b14c9c6Smartynas  * 2. Redistributions in binary form must reproduce the above copyright
125b14c9c6Smartynas  *    notice, this list of conditions and the following disclaimer in the
135b14c9c6Smartynas  *    documentation and/or other materials provided with the distribution.
145b14c9c6Smartynas  * 3. Neither the name of the University nor the names of its contributors
155b14c9c6Smartynas  *    may be used to endorse or promote products derived from this software
165b14c9c6Smartynas  *    without specific prior written permission.
175b14c9c6Smartynas  *
185b14c9c6Smartynas  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
195b14c9c6Smartynas  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
205b14c9c6Smartynas  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
215b14c9c6Smartynas  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
225b14c9c6Smartynas  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
235b14c9c6Smartynas  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
245b14c9c6Smartynas  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
255b14c9c6Smartynas  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
265b14c9c6Smartynas  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
275b14c9c6Smartynas  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
285b14c9c6Smartynas  * SUCH DAMAGE.
295b14c9c6Smartynas  */
305b14c9c6Smartynas 
315b14c9c6Smartynas /*
325b14c9c6Smartynas  * This code by P. McIlroy, Oct 1992;
335b14c9c6Smartynas  *
345b14c9c6Smartynas  * The financial support of UUNET Communications Services is greatfully
355b14c9c6Smartynas  * acknowledged.
365b14c9c6Smartynas  */
375b14c9c6Smartynas 
3849393c00Smartynas #include <float.h>
3949393c00Smartynas #include <math.h>
4049393c00Smartynas 
415b14c9c6Smartynas #include "math_private.h"
425b14c9c6Smartynas 
435b14c9c6Smartynas /* METHOD:
445b14c9c6Smartynas  * x < 0: Use reflection formula, G(x) = pi/(sin(pi*x)*x*G(x))
455b14c9c6Smartynas  *	At negative integers, return NaN and raise invalid.
465b14c9c6Smartynas  *
475b14c9c6Smartynas  * x < 6.5:
485b14c9c6Smartynas  *	Use argument reduction G(x+1) = xG(x) to reach the
495b14c9c6Smartynas  *	range [1.066124,2.066124].  Use a rational
505b14c9c6Smartynas  *	approximation centered at the minimum (x0+1) to
515b14c9c6Smartynas  *	ensure monotonicity.
525b14c9c6Smartynas  *
535b14c9c6Smartynas  * x >= 6.5: Use the asymptotic approximation (Stirling's formula)
545b14c9c6Smartynas  *	adjusted for equal-ripples:
555b14c9c6Smartynas  *
565b14c9c6Smartynas  *	log(G(x)) ~= (x-.5)*(log(x)-1) + .5(log(2*pi)-1) + 1/x*P(1/(x*x))
575b14c9c6Smartynas  *
585b14c9c6Smartynas  *	Keep extra precision in multiplying (x-.5)(log(x)-1), to
595b14c9c6Smartynas  *	avoid premature round-off.
605b14c9c6Smartynas  *
615b14c9c6Smartynas  * Special values:
625b14c9c6Smartynas  *	-Inf:			return NaN and raise invalid;
635b14c9c6Smartynas  *	negative integer:	return NaN and raise invalid;
645b14c9c6Smartynas  *	other x ~< -177.79:	return +-0 and raise underflow;
655b14c9c6Smartynas  *	+-0:			return +-Inf and raise divide-by-zero;
665b14c9c6Smartynas  *	finite x ~> 171.63:	return +Inf and raise overflow;
675b14c9c6Smartynas  *	+Inf:			return +Inf;
685b14c9c6Smartynas  *	NaN: 			return NaN.
695b14c9c6Smartynas  *
705b14c9c6Smartynas  * Accuracy: tgamma(x) is accurate to within
715b14c9c6Smartynas  *	x > 0:  error provably < 0.9ulp.
725b14c9c6Smartynas  *	Maximum observed in 1,000,000 trials was .87ulp.
735b14c9c6Smartynas  *	x < 0:
745b14c9c6Smartynas  *	Maximum observed error < 4ulp in 1,000,000 trials.
755b14c9c6Smartynas  */
765b14c9c6Smartynas 
775b14c9c6Smartynas static double neg_gam(double);
785b14c9c6Smartynas static double small_gam(double);
795b14c9c6Smartynas static double smaller_gam(double);
805b14c9c6Smartynas static struct Double large_gam(double);
815b14c9c6Smartynas static struct Double ratfun_gam(double, double);
825b14c9c6Smartynas 
835b14c9c6Smartynas /*
845b14c9c6Smartynas  * Rational approximation, A0 + x*x*P(x)/Q(x), on the interval
855b14c9c6Smartynas  * [1.066.., 2.066..] accurate to 4.25e-19.
865b14c9c6Smartynas  */
875b14c9c6Smartynas #define LEFT -.3955078125	/* left boundary for rat. approx */
885b14c9c6Smartynas #define x0 .461632144968362356785	/* xmin - 1 */
895b14c9c6Smartynas 
905b14c9c6Smartynas #define a0_hi 0.88560319441088874992
915b14c9c6Smartynas #define a0_lo -.00000000000000004996427036469019695
925b14c9c6Smartynas #define P0	 6.21389571821820863029017800727e-01
935b14c9c6Smartynas #define P1	 2.65757198651533466104979197553e-01
945b14c9c6Smartynas #define P2	 5.53859446429917461063308081748e-03
955b14c9c6Smartynas #define P3	 1.38456698304096573887145282811e-03
965b14c9c6Smartynas #define P4	 2.40659950032711365819348969808e-03
975b14c9c6Smartynas #define Q0	 1.45019531250000000000000000000e+00
985b14c9c6Smartynas #define Q1	 1.06258521948016171343454061571e+00
995b14c9c6Smartynas #define Q2	-2.07474561943859936441469926649e-01
1005b14c9c6Smartynas #define Q3	-1.46734131782005422506287573015e-01
1015b14c9c6Smartynas #define Q4	 3.07878176156175520361557573779e-02
1025b14c9c6Smartynas #define Q5	 5.12449347980666221336054633184e-03
1035b14c9c6Smartynas #define Q6	-1.76012741431666995019222898833e-03
1045b14c9c6Smartynas #define Q7	 9.35021023573788935372153030556e-05
1055b14c9c6Smartynas #define Q8	 6.13275507472443958924745652239e-06
1065b14c9c6Smartynas /*
1075b14c9c6Smartynas  * Constants for large x approximation (x in [6, Inf])
1085b14c9c6Smartynas  * (Accurate to 2.8*10^-19 absolute)
1095b14c9c6Smartynas  */
1105b14c9c6Smartynas #define lns2pi_hi 0.418945312500000
1115b14c9c6Smartynas #define lns2pi_lo -.000006779295327258219670263595
1125b14c9c6Smartynas #define Pa0	 8.33333333333333148296162562474e-02
1135b14c9c6Smartynas #define Pa1	-2.77777777774548123579378966497e-03
1145b14c9c6Smartynas #define Pa2	 7.93650778754435631476282786423e-04
1155b14c9c6Smartynas #define Pa3	-5.95235082566672847950717262222e-04
1165b14c9c6Smartynas #define Pa4	 8.41428560346653702135821806252e-04
1175b14c9c6Smartynas #define Pa5	-1.89773526463879200348872089421e-03
1185b14c9c6Smartynas #define Pa6	 5.69394463439411649408050664078e-03
1195b14c9c6Smartynas #define Pa7	-1.44705562421428915453880392761e-02
1205b14c9c6Smartynas 
1215b14c9c6Smartynas static const double zero = 0., one = 1.0, tiny = 1e-300;
1225b14c9c6Smartynas 
1235b14c9c6Smartynas double
tgamma(double x)1245b14c9c6Smartynas tgamma(double x)
1255b14c9c6Smartynas {
1265b14c9c6Smartynas 	struct Double u;
1275b14c9c6Smartynas 
1285b14c9c6Smartynas 	if (x >= 6) {
1295b14c9c6Smartynas 		if(x > 171.63)
1305b14c9c6Smartynas 			return(x/zero);
1315b14c9c6Smartynas 		u = large_gam(x);
1325b14c9c6Smartynas 		return(__exp__D(u.a, u.b));
1335b14c9c6Smartynas 	} else if (x >= 1.0 + LEFT + x0)
1345b14c9c6Smartynas 		return (small_gam(x));
1355b14c9c6Smartynas 	else if (x > 1.e-17)
1365b14c9c6Smartynas 		return (smaller_gam(x));
1375b14c9c6Smartynas 	else if (x > -1.e-17) {
1385b14c9c6Smartynas 		if (x != 0.0)
1395b14c9c6Smartynas 			u.a = one - tiny;	/* raise inexact */
1405b14c9c6Smartynas 		return (one/x);
141eab01a26Sguenther 	} else if (!isfinite(x)) {
1425b14c9c6Smartynas 		return (x - x);			/* x = NaN, -Inf */
1435b14c9c6Smartynas 	 } else
1445b14c9c6Smartynas 		return (neg_gam(x));
1455b14c9c6Smartynas }
146*2f2c0062Sguenther DEF_STD(tgamma);
147*2f2c0062Sguenther LDBL_MAYBE_UNUSED_CLONE(tgamma);
1485b14c9c6Smartynas 
1495b14c9c6Smartynas /*
1505b14c9c6Smartynas  * We simply call tgamma() rather than bloating the math library
1515b14c9c6Smartynas  * with a float-optimized version of it.  The reason is that tgammaf()
1525b14c9c6Smartynas  * is essentially useless, since the function is superexponential
1535b14c9c6Smartynas  * and floats have very limited range.  -- das@freebsd.org
1545b14c9c6Smartynas  */
1555b14c9c6Smartynas 
1565b14c9c6Smartynas float
tgammaf(float x)1575b14c9c6Smartynas tgammaf(float x)
1585b14c9c6Smartynas {
1595b14c9c6Smartynas 	return tgamma(x);
1605b14c9c6Smartynas }
1615b14c9c6Smartynas 
1625b14c9c6Smartynas /*
1635b14c9c6Smartynas  * Accurate to max(ulp(1/128) absolute, 2^-66 relative) error.
1645b14c9c6Smartynas  */
1655b14c9c6Smartynas 
1665b14c9c6Smartynas static struct Double
large_gam(double x)1675b14c9c6Smartynas large_gam(double x)
1685b14c9c6Smartynas {
1695b14c9c6Smartynas 	double z, p;
1705b14c9c6Smartynas 	struct Double t, u, v;
1715b14c9c6Smartynas 
1725b14c9c6Smartynas 	z = one/(x*x);
1735b14c9c6Smartynas 	p = Pa0+z*(Pa1+z*(Pa2+z*(Pa3+z*(Pa4+z*(Pa5+z*(Pa6+z*Pa7))))));
1745b14c9c6Smartynas 	p = p/x;
1755b14c9c6Smartynas 
1765b14c9c6Smartynas 	u = __log__D(x);
1775b14c9c6Smartynas 	u.a -= one;
1785b14c9c6Smartynas 	v.a = (x -= .5);
1795b14c9c6Smartynas 	TRUNC(v.a);
1805b14c9c6Smartynas 	v.b = x - v.a;
1815b14c9c6Smartynas 	t.a = v.a*u.a;			/* t = (x-.5)*(log(x)-1) */
1825b14c9c6Smartynas 	t.b = v.b*u.a + x*u.b;
1835b14c9c6Smartynas 	/* return t.a + t.b + lns2pi_hi + lns2pi_lo + p */
1845b14c9c6Smartynas 	t.b += lns2pi_lo; t.b += p;
1855b14c9c6Smartynas 	u.a = lns2pi_hi + t.b; u.a += t.a;
1865b14c9c6Smartynas 	u.b = t.a - u.a;
1875b14c9c6Smartynas 	u.b += lns2pi_hi; u.b += t.b;
1885b14c9c6Smartynas 	return (u);
1895b14c9c6Smartynas }
1905b14c9c6Smartynas 
1915b14c9c6Smartynas /*
1925b14c9c6Smartynas  * Good to < 1 ulp.  (provably .90 ulp; .87 ulp on 1,000,000 runs.)
1935b14c9c6Smartynas  * It also has correct monotonicity.
1945b14c9c6Smartynas  */
1955b14c9c6Smartynas 
1965b14c9c6Smartynas static double
small_gam(double x)1975b14c9c6Smartynas small_gam(double x)
1985b14c9c6Smartynas {
1995b14c9c6Smartynas 	double y, ym1, t;
2005b14c9c6Smartynas 	struct Double yy, r;
2015b14c9c6Smartynas 	y = x - one;
2025b14c9c6Smartynas 	ym1 = y - one;
2035b14c9c6Smartynas 	if (y <= 1.0 + (LEFT + x0)) {
2045b14c9c6Smartynas 		yy = ratfun_gam(y - x0, 0);
2055b14c9c6Smartynas 		return (yy.a + yy.b);
2065b14c9c6Smartynas 	}
2075b14c9c6Smartynas 	r.a = y;
2085b14c9c6Smartynas 	TRUNC(r.a);
2095b14c9c6Smartynas 	yy.a = r.a - one;
2105b14c9c6Smartynas 	y = ym1;
2115b14c9c6Smartynas 	yy.b = r.b = y - yy.a;
2125b14c9c6Smartynas 	/* Argument reduction: G(x+1) = x*G(x) */
2135b14c9c6Smartynas 	for (ym1 = y-one; ym1 > LEFT + x0; y = ym1--, yy.a--) {
2145b14c9c6Smartynas 		t = r.a*yy.a;
2155b14c9c6Smartynas 		r.b = r.a*yy.b + y*r.b;
2165b14c9c6Smartynas 		r.a = t;
2175b14c9c6Smartynas 		TRUNC(r.a);
2185b14c9c6Smartynas 		r.b += (t - r.a);
2195b14c9c6Smartynas 	}
2205b14c9c6Smartynas 	/* Return r*tgamma(y). */
2215b14c9c6Smartynas 	yy = ratfun_gam(y - x0, 0);
2225b14c9c6Smartynas 	y = r.b*(yy.a + yy.b) + r.a*yy.b;
2235b14c9c6Smartynas 	y += yy.a*r.a;
2245b14c9c6Smartynas 	return (y);
2255b14c9c6Smartynas }
2265b14c9c6Smartynas 
2275b14c9c6Smartynas /*
2285b14c9c6Smartynas  * Good on (0, 1+x0+LEFT].  Accurate to 1ulp.
2295b14c9c6Smartynas  */
2305b14c9c6Smartynas 
2315b14c9c6Smartynas static double
smaller_gam(double x)2325b14c9c6Smartynas smaller_gam(double x)
2335b14c9c6Smartynas {
2345b14c9c6Smartynas 	double t, d;
2355b14c9c6Smartynas 	struct Double r, xx;
2365b14c9c6Smartynas 	if (x < x0 + LEFT) {
2375173bed6Smartynas 		t = x;
2385173bed6Smartynas 		TRUNC(t);
2395b14c9c6Smartynas 		d = (t+x)*(x-t);
2405b14c9c6Smartynas 		t *= t;
2415173bed6Smartynas 		xx.a = (t + x);
2425173bed6Smartynas 		TRUNC(xx.a);
2435b14c9c6Smartynas 		xx.b = x - xx.a; xx.b += t; xx.b += d;
2445b14c9c6Smartynas 		t = (one-x0); t += x;
2455b14c9c6Smartynas 		d = (one-x0); d -= t; d += x;
2465b14c9c6Smartynas 		x = xx.a + xx.b;
2475b14c9c6Smartynas 	} else {
2485173bed6Smartynas 		xx.a =  x;
2495173bed6Smartynas 		TRUNC(xx.a);
2505b14c9c6Smartynas 		xx.b = x - xx.a;
2515b14c9c6Smartynas 		t = x - x0;
2525b14c9c6Smartynas 		d = (-x0 -t); d += x;
2535b14c9c6Smartynas 	}
2545b14c9c6Smartynas 	r = ratfun_gam(t, d);
2555173bed6Smartynas 	d = r.a/x;
2565173bed6Smartynas 	TRUNC(d);
2575b14c9c6Smartynas 	r.a -= d*xx.a; r.a -= d*xx.b; r.a += r.b;
2585b14c9c6Smartynas 	return (d + r.a/x);
2595b14c9c6Smartynas }
2605b14c9c6Smartynas 
2615b14c9c6Smartynas /*
2625b14c9c6Smartynas  * returns (z+c)^2 * P(z)/Q(z) + a0
2635b14c9c6Smartynas  */
2645b14c9c6Smartynas 
2655b14c9c6Smartynas static struct Double
ratfun_gam(double z,double c)2665b14c9c6Smartynas ratfun_gam(double z, double c)
2675b14c9c6Smartynas {
2685b14c9c6Smartynas 	double p, q;
2695b14c9c6Smartynas 	struct Double r, t;
2705b14c9c6Smartynas 
2715b14c9c6Smartynas 	q = Q0 +z*(Q1+z*(Q2+z*(Q3+z*(Q4+z*(Q5+z*(Q6+z*(Q7+z*Q8)))))));
2725b14c9c6Smartynas 	p = P0 + z*(P1 + z*(P2 + z*(P3 + z*P4)));
2735b14c9c6Smartynas 
2745b14c9c6Smartynas 	/* return r.a + r.b = a0 + (z+c)^2*p/q, with r.a truncated to 26 bits. */
2755b14c9c6Smartynas 	p = p/q;
2765173bed6Smartynas 	t.a = z;
2775173bed6Smartynas 	TRUNC(t.a);			/* t ~= z + c */
2785b14c9c6Smartynas 	t.b = (z - t.a) + c;
2795b14c9c6Smartynas 	t.b *= (t.a + z);
2805b14c9c6Smartynas 	q = (t.a *= t.a);		/* t = (z+c)^2 */
2815b14c9c6Smartynas 	TRUNC(t.a);
2825b14c9c6Smartynas 	t.b += (q - t.a);
2835173bed6Smartynas 	r.a = p;
2845173bed6Smartynas 	TRUNC(r.a);			/* r = P/Q */
2855b14c9c6Smartynas 	r.b = p - r.a;
2865b14c9c6Smartynas 	t.b = t.b*p + t.a*r.b + a0_lo;
2875b14c9c6Smartynas 	t.a *= r.a;			/* t = (z+c)^2*(P/Q) */
2885173bed6Smartynas 	r.a = t.a + a0_hi;
2895173bed6Smartynas 	TRUNC(r.a);
2905b14c9c6Smartynas 	r.b = ((a0_hi-r.a) + t.a) + t.b;
2915b14c9c6Smartynas 	return (r);			/* r = a0 + t */
2925b14c9c6Smartynas }
2935b14c9c6Smartynas 
2945b14c9c6Smartynas static double
neg_gam(double x)2955b14c9c6Smartynas neg_gam(double x)
2965b14c9c6Smartynas {
2975b14c9c6Smartynas 	int sgn = 1;
2985b14c9c6Smartynas 	struct Double lg, lsine;
2995b14c9c6Smartynas 	double y, z;
3005b14c9c6Smartynas 
3015b14c9c6Smartynas 	y = ceil(x);
3025b14c9c6Smartynas 	if (y == x)		/* Negative integer. */
3035b14c9c6Smartynas 		return ((x - x) / zero);
3045b14c9c6Smartynas 	z = y - x;
3055b14c9c6Smartynas 	if (z > 0.5)
3065b14c9c6Smartynas 		z = one - z;
3075b14c9c6Smartynas 	y = 0.5 * y;
3085b14c9c6Smartynas 	if (y == ceil(y))
3095b14c9c6Smartynas 		sgn = -1;
3105b14c9c6Smartynas 	if (z < .25)
3115b14c9c6Smartynas 		z = sin(M_PI*z);
3125b14c9c6Smartynas 	else
3135b14c9c6Smartynas 		z = cos(M_PI*(0.5-z));
3145b14c9c6Smartynas 	/* Special case: G(1-x) = Inf; G(x) may be nonzero. */
3155b14c9c6Smartynas 	if (x < -170) {
3165b14c9c6Smartynas 		if (x < -190)
3175b14c9c6Smartynas 			return ((double)sgn*tiny*tiny);
3185b14c9c6Smartynas 		y = one - x;		/* exact: 128 < |x| < 255 */
3195b14c9c6Smartynas 		lg = large_gam(y);
3205b14c9c6Smartynas 		lsine = __log__D(M_PI/z);	/* = TRUNC(log(u)) + small */
3215b14c9c6Smartynas 		lg.a -= lsine.a;		/* exact (opposite signs) */
3225b14c9c6Smartynas 		lg.b -= lsine.b;
3235b14c9c6Smartynas 		y = -(lg.a + lg.b);
3245b14c9c6Smartynas 		z = (y + lg.a) + lg.b;
3255b14c9c6Smartynas 		y = __exp__D(y, z);
3265b14c9c6Smartynas 		if (sgn < 0) y = -y;
3275b14c9c6Smartynas 		return (y);
3285b14c9c6Smartynas 	}
3295b14c9c6Smartynas 	y = one-x;
3305b14c9c6Smartynas 	if (one-y == x)
3315b14c9c6Smartynas 		y = tgamma(y);
3325b14c9c6Smartynas 	else		/* 1-x is inexact */
3335b14c9c6Smartynas 		y = -x*tgamma(-x);
3345b14c9c6Smartynas 	if (sgn < 0) y = -y;
3355b14c9c6Smartynas 	return (M_PI / (y*z));
3365b14c9c6Smartynas }
337