1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* missing/tgamma.c (tgamma): add error check.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15414 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2008-02-09 03:07:34 +00:00
parent c8aa30a958
commit 0e66903fca
2 changed files with 21 additions and 2 deletions

View file

@ -1,3 +1,7 @@
Sat Feb 9 12:06:45 2008 Tanaka Akira <akr@fsij.org>
* missing/tgamma.c (tgamma): add error check.
Sat Feb 9 11:47:03 2008 Tanaka Akira <akr@fsij.org> Sat Feb 9 11:47:03 2008 Tanaka Akira <akr@fsij.org>
* math.c (math_gamma): add error check. * math.c (math_gamma): add error check.

View file

@ -10,6 +10,7 @@ reference - Haruhiko Okumura: C-gengo niyoru saishin algorithm jiten
gamma.c -- Gamma function gamma.c -- Gamma function
***********************************************************/ ***********************************************************/
#include <math.h> #include <math.h>
#include <errno.h>
#define PI 3.14159265358979324 /* $\pi$ */ #define PI 3.14159265358979324 /* $\pi$ */
#define LOG_2PI 1.83787706640934548 /* $\log 2\pi$ */ #define LOG_2PI 1.83787706640934548 /* $\log 2\pi$ */
#define N 8 #define N 8
@ -42,8 +43,22 @@ loggamma(double x) /* the natural logarithm of the Gamma function. */
double tgamma(double x) /* Gamma function */ double tgamma(double x) /* Gamma function */
{ {
if (x < 0) if (x == 0.0) { /* Pole Error */
return PI / (sin(PI * x) * exp(loggamma(1 - x))); 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;
}
sign = (fmod(i, 2.0) != 0.0) ? 1 : -1;
return sign * PI / (sin(PI * f) * exp(loggamma(1 - x)));
}
return exp(loggamma(x)); return exp(loggamma(x));
} }