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

ruby-changes:37887

From: gogotanaka <ko1@a...>
Date: Sat, 14 Mar 2015 20:08:04 +0900 (JST)
Subject: [ruby-changes:37887] gogotanaka:r49968 (trunk): * math.c (math_gamma): optimization for passed small integer.

gogotanaka	2015-03-14 20:07:49 +0900 (Sat, 14 Mar 2015)

  New Revision: 49968

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

  Log:
    * math.c (math_gamma): optimization for passed small integer.

  Modified files:
    trunk/ChangeLog
    trunk/math.c
    trunk/test/ruby/test_math.rb
Index: math.c
===================================================================
--- math.c	(revision 49967)
+++ math.c	(revision 49968)
@@ -836,6 +836,8 @@ mingw_tgamma(const double d) https://github.com/ruby/ruby/blob/trunk/math.c#L836
  *
  */
 
+#define NGAMMA_TABLE 23
+
 static VALUE
 math_gamma(VALUE obj, VALUE x)
 {
@@ -868,16 +870,13 @@ math_gamma(VALUE obj, VALUE x) https://github.com/ruby/ruby/blob/trunk/math.c#L870
          * 53bit mantissa. */
     };
     double d;
-    double intpart, fracpart;
     d = Get_Double(x);
     /* check for domain error */
     if (isinf(d) && signbit(d)) domain_error("gamma");
-    fracpart = modf(d, &intpart);
-    if (fracpart == 0.0) {
-	if (intpart < 0) domain_error("gamma");
-	if (0 < intpart &&
-	    intpart - 1 < (double)numberof(fact_table)) {
-	    return DBL2NUM(fact_table[(int)intpart - 1]);
+    if (d == floor(d)) {
+	if (d < 0.0) domain_error("gamma");
+	if (1.0 <= d && d <= NGAMMA_TABLE) {
+	    return DBL2NUM(fact_table[(int)d - 1]);
 	}
     }
     return DBL2NUM(tgamma(d));
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 49967)
+++ ChangeLog	(revision 49968)
@@ -1,3 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Mar 14 20:05:23 2015  Kazuki Tanaka  <gogotanaka@r...>
+
+	* math.c (math_gamma): optimization for passed small integer.
+
 Sat Mar 14 18:07:23 2015  Kazuki Tanaka  <gogotanaka@r...>
 
 	* enum.c: [DOC] Fixes Enumerable#member? documentation
Index: test/ruby/test_math.rb
===================================================================
--- test/ruby/test_math.rb	(revision 49967)
+++ test/ruby/test_math.rb	(revision 49968)
@@ -240,6 +240,8 @@ class TestMath < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_math.rb#L240
     check(2, Math.gamma(3))
     check(15 * sqrt_pi / 8, Math.gamma(3.5))
     check(6, Math.gamma(4))
+    check(1.1240007277776077e+21, Math.gamma(23))
+    check(2.5852016738885062e+22, Math.gamma(24))
 
     # no SEGV [ruby-core:25257]
     31.upto(65) do |i|

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

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