[前][次][番号順一覧][スレッド一覧]

ruby-changes:46574

From: nobu <ko1@a...>
Date: Sat, 13 May 2017 01:01:27 +0900 (JST)
Subject: [ruby-changes:46574] nobu:r58689 (trunk): tgamma.c: unify versions with/without lgamma_r

nobu	2017-05-13 01:01:17 +0900 (Sat, 13 May 2017)

  New Revision: 58689

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=58689

  Log:
    tgamma.c: unify versions with/without lgamma_r

  Modified files:
    trunk/missing/tgamma.c
Index: missing/tgamma.c
===================================================================
--- missing/tgamma.c	(revision 58688)
+++ missing/tgamma.c	(revision 58689)
@@ -13,30 +13,7 @@ reference - Haruhiko Okumura: C-gengo ni https://github.com/ruby/ruby/blob/trunk/missing/tgamma.c#L13
 #include <math.h>
 #include <errno.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) {
-        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
+#ifndef HAVE_LGAMMA_R
 
 #include <errno.h>
 #define PI      3.14159265358979324  /* $\pi$ */
@@ -68,15 +45,16 @@ loggamma(double x)  /* the natural logar https://github.com/ruby/ruby/blob/trunk/missing/tgamma.c#L45
                 + (B4  / ( 4 *  3))) * w + (B2  / ( 2 *  1))) / x
                 + 0.5 * LOG_2PI - log(v) - x + (x - 0.5) * log(x);
 }
+#endif
 
 double tgamma(double x)  /* Gamma function */
 {
+    int sign;
     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);
@@ -84,9 +62,15 @@ double tgamma(double x)  /* Gamma functi https://github.com/ruby/ruby/blob/trunk/missing/tgamma.c#L62
             errno = EDOM;
             return zero/zero;
         }
+#ifndef HAVE_LGAMMA_R
         sign = (fmod(i, 2.0) != 0.0) ? 1 : -1;
         return sign * PI / (sin(PI * f) * exp(loggamma(1 - x)));
+#endif
     }
+#ifndef HAVE_LGAMMA_R
     return exp(loggamma(x));
-}
+#else
+    x = lgamma_r(x, &sign);
+    return sign * exp(x);
 #endif
+}

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]