From 14373fc4db7440d39db4fd39297f2ed44cc990f2 Mon Sep 17 00:00:00 2001 From: akr Date: Thu, 7 Feb 2008 01:43:43 +0000 Subject: * math.c (math_gamma): new method Math.gamma. (math_lgamma): new method Math.lgamma. * include/ruby/missing.h (tgamma): declared unless HAVE_TGAMMA. (lgamma_r): declared unless HAVE_LGAMMA_R. * configure.in (tgamma): check for replacement funtions. (lgamma_r): ditto. * missing/tgamma.c: new file. based on gamma.c from "C-gengo niyoru saishin algorithm jiten" (New Algorithm handbook in C language) (Gijyutsu hyouron sha, Tokyo, 1991) by Haruhiko Okumura. * missing/lgamma_r.c: ditto. * LEGAL (missing/tgamma.c): describe as public domain. (missing/lgamma_r.c): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15388 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- missing/tgamma.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 missing/tgamma.c (limited to 'missing/tgamma.c') diff --git a/missing/tgamma.c b/missing/tgamma.c new file mode 100644 index 0000000000..a30108e0b5 --- /dev/null +++ b/missing/tgamma.c @@ -0,0 +1,49 @@ +/* tgamma.c - public domain implementation of error function tgamma(3m) + +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 +***********************************************************/ +#include +#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); +} + +double tgamma(double x) /* Gamma function */ +{ + if (x < 0) + return PI / (sin(PI * x) * exp(loggamma(1 - x))); + return exp(loggamma(x)); +} + -- cgit v1.2.3