2008-06-15 06:18:06 +04:00
|
|
|
/* tgamma.c - public domain implementation of function tgamma(3m)
|
2008-02-07 04:43:43 +03:00
|
|
|
|
|
|
|
reference - Haruhiko Okumura: C-gengo niyoru saishin algorithm jiten
|
|
|
|
(New Algorithm handbook in C language) (Gijyutsu hyouron
|
|
|
|
sha, Tokyo, 1991) [in Japanese]
|
|
|
|
http://oku.edu.mie-u.ac.jp/~okumura/algo/
|
|
|
|
*/
|
|
|
|
|
|
|
|
/***********************************************************
|
|
|
|
gamma.c -- Gamma function
|
|
|
|
***********************************************************/
|
2008-03-20 19:22:03 +03:00
|
|
|
#include "ruby/config.h"
|
2017-05-12 19:12:46 +03:00
|
|
|
#include "ruby/missing.h"
|
2008-02-07 04:43:43 +03:00
|
|
|
#include <math.h>
|
2008-02-09 17:45:43 +03:00
|
|
|
#include <errno.h>
|
|
|
|
|
2017-05-12 19:12:46 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
# include <float.h>
|
|
|
|
# if !defined __MINGW32__ || defined __NO_ISOCEXT
|
|
|
|
# ifndef isnan
|
|
|
|
# define isnan(x) _isnan(x)
|
|
|
|
# endif
|
|
|
|
# ifndef isinf
|
|
|
|
# define isinf(x) (!_finite(x) && !_isnan(x))
|
|
|
|
# endif
|
|
|
|
# ifndef finite
|
|
|
|
# define finite(x) _finite(x)
|
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2017-05-12 19:01:17 +03:00
|
|
|
#ifndef HAVE_LGAMMA_R
|
2008-02-09 17:45:43 +03:00
|
|
|
|
2008-02-09 06:07:34 +03:00
|
|
|
#include <errno.h>
|
2008-02-07 04:43:43 +03:00
|
|
|
#define PI 3.14159265358979324 /* $\pi$ */
|
|
|
|
#define LOG_2PI 1.83787706640934548 /* $\log 2\pi$ */
|
|
|
|
#define N 8
|
|
|
|
|
|
|
|
#define B0 1 /* Bernoulli numbers */
|
|
|
|
#define B1 (-1.0 / 2.0)
|
|
|
|
#define B2 ( 1.0 / 6.0)
|
|
|
|
#define B4 (-1.0 / 30.0)
|
|
|
|
#define B6 ( 1.0 / 42.0)
|
|
|
|
#define B8 (-1.0 / 30.0)
|
|
|
|
#define B10 ( 5.0 / 66.0)
|
|
|
|
#define B12 (-691.0 / 2730.0)
|
|
|
|
#define B14 ( 7.0 / 6.0)
|
|
|
|
#define B16 (-3617.0 / 510.0)
|
|
|
|
|
|
|
|
static double
|
|
|
|
loggamma(double x) /* the natural logarithm of the Gamma function. */
|
|
|
|
{
|
|
|
|
double v, w;
|
|
|
|
|
|
|
|
v = 1;
|
|
|
|
while (x < N) { v *= x; x++; }
|
|
|
|
w = 1 / (x * x);
|
|
|
|
return ((((((((B16 / (16 * 15)) * w + (B14 / (14 * 13))) * w
|
|
|
|
+ (B12 / (12 * 11))) * w + (B10 / (10 * 9))) * w
|
|
|
|
+ (B8 / ( 8 * 7))) * w + (B6 / ( 6 * 5))) * w
|
|
|
|
+ (B4 / ( 4 * 3))) * w + (B2 / ( 2 * 1))) / x
|
|
|
|
+ 0.5 * LOG_2PI - log(v) - x + (x - 0.5) * log(x);
|
|
|
|
}
|
2017-05-12 19:01:17 +03:00
|
|
|
#endif
|
2008-02-07 04:43:43 +03:00
|
|
|
|
|
|
|
double tgamma(double x) /* Gamma function */
|
|
|
|
{
|
2017-05-12 19:01:17 +03:00
|
|
|
int sign;
|
2008-02-09 06:07:34 +03:00
|
|
|
if (x == 0.0) { /* Pole Error */
|
|
|
|
errno = ERANGE;
|
|
|
|
return 1/x < 0 ? -HUGE_VAL : HUGE_VAL;
|
|
|
|
}
|
2017-05-12 19:12:46 +03:00
|
|
|
if (isinf(x)) {
|
|
|
|
if (x < 0) goto domain_error;
|
|
|
|
return x;
|
|
|
|
}
|
2008-02-09 06:07:34 +03:00
|
|
|
if (x < 0) {
|
|
|
|
static double zero = 0.0;
|
|
|
|
double i, f;
|
|
|
|
f = modf(-x, &i);
|
|
|
|
if (f == 0.0) { /* Domain Error */
|
2017-05-12 19:12:46 +03:00
|
|
|
domain_error:
|
2008-02-09 06:07:34 +03:00
|
|
|
errno = EDOM;
|
|
|
|
return zero/zero;
|
|
|
|
}
|
2017-05-12 19:01:17 +03:00
|
|
|
#ifndef HAVE_LGAMMA_R
|
2008-02-09 06:07:34 +03:00
|
|
|
sign = (fmod(i, 2.0) != 0.0) ? 1 : -1;
|
|
|
|
return sign * PI / (sin(PI * f) * exp(loggamma(1 - x)));
|
2017-05-12 19:01:17 +03:00
|
|
|
#endif
|
2008-02-09 06:07:34 +03:00
|
|
|
}
|
2017-05-12 19:01:17 +03:00
|
|
|
#ifndef HAVE_LGAMMA_R
|
2008-02-07 04:43:43 +03:00
|
|
|
return exp(loggamma(x));
|
2017-05-12 19:01:17 +03:00
|
|
|
#else
|
|
|
|
x = lgamma_r(x, &sign);
|
|
|
|
return sign * exp(x);
|
2008-02-09 17:45:43 +03:00
|
|
|
#endif
|
2017-05-12 19:01:17 +03:00
|
|
|
}
|