#if !_UWIN || _lib_lgamma
void _STUB_lgamma(){}
#else
/*-
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
#endif /* not lint */
/*
* Coded by Peter McIlroy, Nov 1992;
*
* The financial support of UUNET Communications Services is greatfully
* acknowledged.
*/
#include <math.h>
#include <errno.h>
#include "mathimpl.h"
/* Log gamma function.
* Error: x > 0 error < 1.3ulp.
* x > 4, error < 1ulp.
* x > 9, error < .6ulp.
* x < 0, all bets are off. (When G(x) ~ 1, log(G(x)) ~ 0)
* Method:
* x > 6:
* Use the asymptotic expansion (Stirling's Formula)
* 0 < x < 6:
* Use gamma(x+1) = x*gamma(x) for argument reduction.
* Use rational approximation in
* the range 1.2, 2.5
* Two approximations are used, one centered at the
* minimum to ensure monotonicity; one centered at 2
* to maintain small relative error.
* x < 0:
* Use the reflection formula,
* Special values:
* non-positive integer returns +Inf.
* NaN returns NaN
*/
static int endian;
#define _IEEE 0
/* double and float have same size exponent field */
#define TRUNC(x) x = (double) (float) (x)
#else
#endif
static double small_lgam(double);
static double large_lgam(double);
static double neg_lgam(double);
int signgam;
/*
* Constants for approximation in [1.244,1.712]
*/
/*
* Constants for approximation in [1.71, 2.5]
*/
/*
* Stirling's Formula, adjusted for equal-ripple. x in [6,Inf].
*/
{
double r;
signgam = 1;
if (!finite(x))
if (_IEEE)
return (x+x);
if (x > 6 + RIGHT) {
r = large_lgam(x);
return (r);
} else if (x > 1e-16)
return (small_lgam(x));
else if (x > -1e-16) {
if (x < 0)
signgam = -1, x = -x;
return (-log(x));
} else
return (neg_lgam(x));
}
static double
large_lgam(double x)
{
double z, p, x1;
struct Double t, u, v;
u = __log__D(x);
u.a -= 1.0;
if (x > 1e15) {
v.a = x - 0.5;
TRUNC(v.a);
v.b = (x - v.a) - 0.5;
t.a = u.a*v.a;
t.b = x*u.b + v.b*u.a;
return(t.a + t.b);
}
x1 = 1./x;
/* error in approximation = 2.8e-19 */
p = p*x1; /* error < 2.3e-18 absolute */
/* 0 < p < 1/64 (at x = 5.5) */
v.a = x = x - 0.5;
TRUNC(v.a); /* truncate v.a to 26 bits. */
v.b = x - v.a;
t.a = v.a*u.a; /* t = (x-.5)*(log(x)-1) */
t.b = v.b*u.a + x*u.b;
t.b += p; t.b += lns2pi; /* return t + lns2pi + p */
return (t.a + t.b);
}
static double
small_lgam(double x)
{
int x_int;
x_int = (int)(x + .5);
y = x - x_int;
t = y - x0;
y--; x_int++;
goto CONTINUE;
} else if (y < -LEFT) {
t = y +(1.0-x0);
z = t - x0_lo;
r = t*(z*(p/q) - x0_lo);
t = .5*t*t;
z = 1.0;
switch (x_int) {
case 6: z = (y + 5);
case 5: z *= (y + 4);
case 4: z *= (y + 3);
case 3: z *= (y + 2);
case 0: r -= log1p(x);
}
} else {
p = p*(y/q);
t = (double)(float) y;
z = y-t;
q = hi*t;
z = 1.0;
switch (x_int) {
case 6: z = (y + 5);
case 5: z *= (y + 4);
case 4: z *= (y + 3);
case 3: z *= (y + 2);
r += rr.b; r += q;
return(rr.a + r);
case 2: return (q+ r);
r += q; r-= rr.a;
return(r);
r -= rr.b;
q -= rr.a;
return (r+q);
}
}
}
static double
neg_lgam(double x)
{
int xi;
extern double gamma();
/* avoid destructive cancellation as much as possible */
if (x > -170) {
xi = (int)x;
if (xi == x)
if (_IEEE)
else
y = gamma(x);
if (y < 0)
y = -y, signgam = -1;
return (log(y));
}
z = floor(x + .5);
if (z == x) { /* convention: G(-(integer)) -> +Inf */
if (_IEEE)
else
}
y = .5*ceil(x);
if (y == ceil(y))
signgam = -1;
x = -x;
z = fabs(x + z); /* 0 < z <= .5 */
if (z < .25)
else
y = large_lgam(x);
return (z - y);
}
#endif