summaryrefslogtreecommitdiffstats
path: root/missing
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-02-09 03:07:34 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-02-09 03:07:34 +0000
commit0a2a4ecaebcbc70315e5e70374d049e4d8bbe5ce (patch)
tree80eca27e1477715a79d5f0b8e0ec7ed1050fc58c /missing
parentdf7200f2c194816ca70867a1ac4764822a98d7a1 (diff)
downloadruby-0a2a4ecaebcbc70315e5e70374d049e4d8bbe5ce.tar.gz
ruby-0a2a4ecaebcbc70315e5e70374d049e4d8bbe5ce.tar.xz
ruby-0a2a4ecaebcbc70315e5e70374d049e4d8bbe5ce.zip
* missing/tgamma.c (tgamma): add error check.
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@15414 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'missing')
-rw-r--r--missing/tgamma.c19
1 files changed, 17 insertions, 2 deletions
diff --git a/missing/tgamma.c b/missing/tgamma.c
index a30108e0b..4ace1bcc5 100644
--- a/missing/tgamma.c
+++ b/missing/tgamma.c
@@ -10,6 +10,7 @@ reference - Haruhiko Okumura: C-gengo niyoru saishin algorithm jiten
gamma.c -- Gamma function
***********************************************************/
#include <math.h>
+#include <errno.h>
#define PI 3.14159265358979324 /* $\pi$ */
#define LOG_2PI 1.83787706640934548 /* $\log 2\pi$ */
#define N 8
@@ -42,8 +43,22 @@ loggamma(double x) /* the natural logarithm of the Gamma function. */
double tgamma(double x) /* Gamma function */
{
- if (x < 0)
- return PI / (sin(PI * x) * exp(loggamma(1 - x)));
+ 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;
+ }
+ sign = (fmod(i, 2.0) != 0.0) ? 1 : -1;
+ return sign * PI / (sin(PI * f) * exp(loggamma(1 - x)));
+ }
return exp(loggamma(x));
}