148402Sbostic /*-
261309Sbostic  * Copyright (c) 1992, 1993
361309Sbostic  *	The Regents of the University of California.  All rights reserved.
448402Sbostic  *
556957Sbostic  * %sccs.include.redist.c%
634120Sbostic  */
734120Sbostic 
824600Szliu #ifndef lint
9*64989Smckusick static char sccsid[] = "@(#)lgamma.c	8.2 (Berkeley) 11/30/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 static double small_lgam(double);
5857128Smcilroy static double large_lgam(double);
5957128Smcilroy static double neg_lgam(double);
6057128Smcilroy static double zero = 0.0, one = 1.0;
6157128Smcilroy int signgam;
6224600Szliu 
6356952Sbostic #define UNDERFL (1e-1020 * 1e-1020)
6456952Sbostic 
6557128Smcilroy #define LEFT	(1.0 - (x0 + .25))
6657128Smcilroy #define RIGHT	(x0 - .218)
6757128Smcilroy /*
6857128Smcilroy /* Constants for approximation in [1.244,1.712]
6957128Smcilroy */
7057128Smcilroy #define x0	0.461632144968362356785
7157128Smcilroy #define x0_lo	-.000000000000000015522348162858676890521
7257128Smcilroy #define a0_hi	-0.12148629128932952880859
7357128Smcilroy #define a0_lo	.0000000007534799204229502
7457128Smcilroy #define r0	-2.771227512955130520e-002
7557128Smcilroy #define r1	-2.980729795228150847e-001
7657128Smcilroy #define r2	-3.257411333183093394e-001
7757128Smcilroy #define r3	-1.126814387531706041e-001
7857128Smcilroy #define r4	-1.129130057170225562e-002
7957128Smcilroy #define r5	-2.259650588213369095e-005
8057128Smcilroy #define s0	 1.714457160001714442e+000
8157128Smcilroy #define s1	 2.786469504618194648e+000
8257128Smcilroy #define s2	 1.564546365519179805e+000
8357128Smcilroy #define s3	 3.485846389981109850e-001
8457128Smcilroy #define s4	 2.467759345363656348e-002
8557128Smcilroy /*
8657128Smcilroy  * Constants for approximation in [1.71, 2.5]
8757128Smcilroy */
8857128Smcilroy #define a1_hi	4.227843350984671344505727574870e-01
8957128Smcilroy #define a1_lo	4.670126436531227189e-18
9057128Smcilroy #define p0	3.224670334241133695662995251041e-01
9157128Smcilroy #define p1	3.569659696950364669021382724168e-01
9257128Smcilroy #define p2	1.342918716072560025853732668111e-01
9357128Smcilroy #define p3	1.950702176409779831089963408886e-02
9457128Smcilroy #define p4	8.546740251667538090796227834289e-04
9557128Smcilroy #define q0	1.000000000000000444089209850062e+00
9657128Smcilroy #define q1	1.315850076960161985084596381057e+00
9757128Smcilroy #define q2	6.274644311862156431658377186977e-01
9857128Smcilroy #define q3	1.304706631926259297049597307705e-01
9957128Smcilroy #define q4	1.102815279606722369265536798366e-02
10057128Smcilroy #define q5	2.512690594856678929537585620579e-04
10157128Smcilroy #define q6	-1.003597548112371003358107325598e-06
10257128Smcilroy /*
10357128Smcilroy  * Stirling's Formula, adjusted for equal-ripple. x in [6,Inf].
10457128Smcilroy */
10557152Sbostic #define lns2pi	.418938533204672741780329736405
10657152Sbostic #define pb0	 8.33333333333333148296162562474e-02
10757152Sbostic #define pb1	-2.77777777774548123579378966497e-03
10857152Sbostic #define pb2	 7.93650778754435631476282786423e-04
10957152Sbostic #define pb3	-5.95235082566672847950717262222e-04
11057152Sbostic #define pb4	 8.41428560346653702135821806252e-04
11157152Sbostic #define pb5	-1.89773526463879200348872089421e-03
11257152Sbostic #define pb6	 5.69394463439411649408050664078e-03
11357152Sbostic #define pb7	-1.44705562421428915453880392761e-02
11456952Sbostic 
115*64989Smckusick __pure double
lgamma(double x)11657128Smcilroy lgamma(double x)
11724600Szliu {
11856952Sbostic 	double r;
11957152Sbostic 
12056952Sbostic 	signgam = 1;
12157152Sbostic 	endian = ((*(int *) &one)) ? 1 : 0;
12257152Sbostic 
12357128Smcilroy 	if (!finite(x))
12457128Smcilroy 		if (_IEEE)
12557128Smcilroy 			return (x+x);
12657128Smcilroy 		else return (infnan(EDOM));
12757128Smcilroy 
12856952Sbostic 	if (x > 6 + RIGHT) {
12956952Sbostic 		r = large_lgam(x);
13056952Sbostic 		return (r);
13157128Smcilroy 	} else if (x > 1e-16)
13256952Sbostic 		return (small_lgam(x));
13357128Smcilroy 	else if (x > -1e-16) {
13456952Sbostic 		if (x < 0)
13556952Sbostic 			signgam = -1, x = -x;
13656952Sbostic 		return (-log(x));
13756952Sbostic 	} else
13856952Sbostic 		return (neg_lgam(x));
13924600Szliu }
14024600Szliu 
14124600Szliu static double
large_lgam(double x)14257128Smcilroy large_lgam(double x)
14324600Szliu {
14456952Sbostic 	double z, p, x1;
14524600Szliu 	int i;
14656952Sbostic 	struct Double t, u, v;
14757452Sbostic 	u = __log__D(x);
14857128Smcilroy 	u.a -= 1.0;
14957128Smcilroy 	if (x > 1e15) {
15057128Smcilroy 		v.a = x - 0.5;
15157128Smcilroy 		TRUNC(v.a);
15257128Smcilroy 		v.b = (x - v.a) - 0.5;
15357128Smcilroy 		t.a = u.a*v.a;
15457128Smcilroy 		t.b = x*u.b + v.b*u.a;
15557128Smcilroy 		if (_IEEE == 0 && !finite(t.a))
15657128Smcilroy 			return(infnan(ERANGE));
15757128Smcilroy 		return(t.a + t.b);
15857128Smcilroy 	}
15957128Smcilroy 	x1 = 1./x;
16057128Smcilroy 	z = x1*x1;
16157128Smcilroy 	p = pb0+z*(pb1+z*(pb2+z*(pb3+z*(pb4+z*(pb5+z*(pb6+z*pb7))))));
16257128Smcilroy 					/* error in approximation = 2.8e-19 */
16324600Szliu 
16457128Smcilroy 	p = p*x1;			/* error < 2.3e-18 absolute */
16557128Smcilroy 					/* 0 < p < 1/64 (at x = 5.5) */
16657164Smcilroy 	v.a = x = x - 0.5;
16757128Smcilroy 	TRUNC(v.a);			/* truncate v.a to 26 bits. */
16856952Sbostic 	v.b = x - v.a;
16956952Sbostic 	t.a = v.a*u.a;			/* t = (x-.5)*(log(x)-1) */
17057129Smcilroy 	t.b = v.b*u.a + x*u.b;
17157128Smcilroy 	t.b += p; t.b += lns2pi;	/* return t + lns2pi + p */
17257128Smcilroy 	return (t.a + t.b);
17324600Szliu }
17457128Smcilroy 
17524600Szliu static double
small_lgam(double x)17657128Smcilroy small_lgam(double x)
17724600Szliu {
17857128Smcilroy 	int x_int;
17957128Smcilroy 	double y, z, t, r = 0, p, q, hi, lo;
18056952Sbostic 	struct Double rr;
18157128Smcilroy 	x_int = (x + .5);
18257128Smcilroy 	y = x - x_int;
18357128Smcilroy 	if (x_int <= 2 && y > RIGHT) {
18457128Smcilroy 		t = y - x0;
18557128Smcilroy 		y--; x_int++;
18657128Smcilroy 		goto CONTINUE;
18757128Smcilroy 	} else if (y < -LEFT) {
18857128Smcilroy 		t = y +(1.0-x0);
18957128Smcilroy CONTINUE:
19056952Sbostic 		z = t - x0_lo;
19156952Sbostic 		p = r0+z*(r1+z*(r2+z*(r3+z*(r4+z*r5))));
19256952Sbostic 		q = s0+z*(s1+z*(s2+z*(s3+z*s4)));
19357128Smcilroy 		r = t*(z*(p/q) - x0_lo);
19457128Smcilroy 		t = .5*t*t;
19557128Smcilroy 		z = 1.0;
19657128Smcilroy 		switch (x_int) {
19757128Smcilroy 		case 6:	z  = (y + 5);
19857128Smcilroy 		case 5:	z *= (y + 4);
19957128Smcilroy 		case 4:	z *= (y + 3);
20057128Smcilroy 		case 3:	z *= (y + 2);
20157452Sbostic 			rr = __log__D(z);
20257128Smcilroy 			rr.b += a0_lo; rr.a += a0_hi;
20357128Smcilroy 			return(((r+rr.b)+t+rr.a));
20457128Smcilroy 		case 2: return(((r+a0_lo)+t)+a0_hi);
20557128Smcilroy 		case 0: r -= log1p(x);
20657452Sbostic 		default: rr = __log__D(x);
20757128Smcilroy 			rr.a -= a0_hi; rr.b -= a0_lo;
20857128Smcilroy 			return(((r - rr.b) + t) - rr.a);
20957128Smcilroy 		}
21056952Sbostic 	} else {
21157128Smcilroy 		p = p0+y*(p1+y*(p2+y*(p3+y*p4)));
21256952Sbostic 		q = q0+y*(q1+y*(q2+y*(q3+y*(q4+y*(q5+y*q6)))));
21357128Smcilroy 		p = p*(y/q);
21457128Smcilroy 		t = (double)(float) y;
21557128Smcilroy 		z = y-t;
21657128Smcilroy 		hi = (double)(float) (p+a1_hi);
21757128Smcilroy 		lo = a1_hi - hi; lo += p; lo += a1_lo;
21857128Smcilroy 		r = lo*y + z*hi;	/* q + r = y*(a0+p/q) */
21957128Smcilroy 		q = hi*t;
22057128Smcilroy 		z = 1.0;
22157128Smcilroy 		switch (x_int) {
22257128Smcilroy 		case 6:	z  = (y + 5);
22357128Smcilroy 		case 5:	z *= (y + 4);
22457128Smcilroy 		case 4:	z *= (y + 3);
22557128Smcilroy 		case 3:	z *= (y + 2);
22657452Sbostic 			rr = __log__D(z);
22757128Smcilroy 			r += rr.b; r += q;
22857128Smcilroy 			return(rr.a + r);
22957128Smcilroy 		case 2:	return (q+ r);
23057452Sbostic 		case 0: rr = __log__D(x);
23157128Smcilroy 			r -= rr.b; r -= log1p(x);
23257128Smcilroy 			r += q; r-= rr.a;
23357128Smcilroy 			return(r);
23457452Sbostic 		default: rr = __log__D(x);
23557128Smcilroy 			r -= rr.b;
23657128Smcilroy 			q -= rr.a;
23757128Smcilroy 			return (r+q);
23857128Smcilroy 		}
23924600Szliu 	}
24024600Szliu }
24124600Szliu 
24224600Szliu static double
neg_lgam(double x)24357128Smcilroy neg_lgam(double x)
24424600Szliu {
24557152Sbostic 	int xi;
24656952Sbostic 	double y, z, one = 1.0, zero = 0.0;
24757152Sbostic 	extern double gamma();
24824600Szliu 
24957152Sbostic 	/* avoid destructive cancellation as much as possible */
25057152Sbostic 	if (x > -170) {
25157152Sbostic 		xi = x;
25257152Sbostic 		if (xi == x)
25357152Sbostic 			if (_IEEE)
25457152Sbostic 				return(one/zero);
25557152Sbostic 			else
25657152Sbostic 				return(infnan(ERANGE));
25757152Sbostic 		y = gamma(x);
25857152Sbostic 		if (y < 0)
25957152Sbostic 			y = -y, signgam = -1;
26057152Sbostic 		return (log(y));
26157152Sbostic 	}
26256952Sbostic 	z = floor(x + .5);
26357128Smcilroy 	if (z == x) {		/* convention: G(-(integer)) -> +Inf */
26457128Smcilroy 		if (_IEEE)
26557128Smcilroy 			return (one/zero);
26657128Smcilroy 		else
26757128Smcilroy 			return (infnan(ERANGE));
26856952Sbostic 	}
26957128Smcilroy 	y = .5*ceil(x);
27057128Smcilroy 	if (y == ceil(y))
27156952Sbostic 		signgam = -1;
27256952Sbostic 	x = -x;
27356952Sbostic 	z = fabs(x + z);	/* 0 < z <= .5 */
27456952Sbostic 	if (z < .25)
27556952Sbostic 		z = sin(M_PI*z);
27656952Sbostic 	else
27757128Smcilroy 		z = cos(M_PI*(0.5-z));
27857152Sbostic 	z = log(M_PI/(z*x));
27957167Smcilroy 	y = large_lgam(x);
28057152Sbostic 	return (z - y);
28124600Szliu }
282