diff --git a/ChangeLog b/ChangeLog index 079640fff9..2a6aee983e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Sat Feb 9 23:44:29 2008 Tanaka Akira + + * missing/tgamma.c (tgamma): use lgamma_r if available. + Sat Feb 9 23:22:52 2008 Nobuyoshi Nakada * ext/bigdecimal/extconf.rb: simplified the condition. diff --git a/missing/tgamma.c b/missing/tgamma.c index 4ace1bcc5f..91f83fcb82 100644 --- a/missing/tgamma.c +++ b/missing/tgamma.c @@ -10,6 +10,35 @@ reference - Haruhiko Okumura: C-gengo niyoru saishin algorithm jiten gamma.c -- Gamma function ***********************************************************/ #include +#include +#include "ruby/config.h" + +#ifdef HAVE_LGAMMA_R + +double tgamma(double x) +{ + int sign; + double d; + if (x == 0.0) { /* Pole Error */ + errno = ERANGE; + return 1/x < 0 ? -HUGE_VAL : HUGE_VAL; + } + if (x < 0) { + int sign; + static double zero = 0.0; + double i, f; + f = modf(-x, &i); + if (f == 0.0) { /* Domain Error */ + errno = EDOM; + return zero/zero; + } + } + d = lgamma_r(x, &sign); + return sign * exp(d); +} + +#else + #include #define PI 3.14159265358979324 /* $\pi$ */ #define LOG_2PI 1.83787706640934548 /* $\log 2\pi$ */ @@ -61,4 +90,4 @@ double tgamma(double x) /* Gamma function */ } return exp(loggamma(x)); } - +#endif