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

ruby-changes:37898

From: gogotanaka <ko1@a...>
Date: Mon, 16 Mar 2015 17:39:52 +0900 (JST)
Subject: [ruby-changes:37898] gogotanaka:r49979 (trunk): * math.c (math_log1, math_log2, math_log10): refactoring

gogotanaka	2015-03-16 17:39:29 +0900 (Mon, 16 Mar 2015)

  New Revision: 49979

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

  Log:
    * math.c (math_log1, math_log2, math_log10): refactoring
      and tests for this.

  Modified files:
    trunk/math.c
    trunk/test/ruby/test_math.rb
Index: math.c
===================================================================
--- math.c	(revision 49978)
+++ math.c	(revision 49979)
@@ -495,7 +495,7 @@ math_log(int argc, const VALUE *argv, VA https://github.com/ruby/ruby/blob/trunk/math.c#L495
 static double
 math_log1(VALUE x)
 {
-    double d0, d;
+    double d;
     size_t numbits;
 
     if (RB_BIGNUM_TYPE_P(x) && BIGNUM_POSITIVE_P(x) &&
@@ -507,15 +507,13 @@ math_log1(VALUE x) https://github.com/ruby/ruby/blob/trunk/math.c#L507
 	numbits = 0;
     }
 
-    d0 = Get_Double(x);
+    d = Get_Double(x);
     /* check for domain error */
-    if (d0 < 0.0) domain_error("log");
+    if (d < 0.0) domain_error("log");
     /* check for pole error */
-    if (d0 == 0.0) return -INFINITY;
-    d = log(d0);
-    if (numbits)
-        d += numbits * log(2); /* log(2**numbits) */
-    return d;
+    if (d == 0.0) return -INFINITY;
+
+    return log(d) + numbits * log(2); /* log(d * 2 ** numbits) */
 }
 
 #ifndef log2
@@ -550,7 +548,7 @@ extern double log2(double); https://github.com/ruby/ruby/blob/trunk/math.c#L548
 static VALUE
 math_log2(VALUE obj, VALUE x)
 {
-    double d0, d;
+    double d;
     size_t numbits;
 
     if (RB_BIGNUM_TYPE_P(x) && BIGNUM_POSITIVE_P(x) &&
@@ -562,14 +560,13 @@ math_log2(VALUE obj, VALUE x) https://github.com/ruby/ruby/blob/trunk/math.c#L560
 	numbits = 0;
     }
 
-    d0 = Get_Double(x);
+    d = Get_Double(x);
     /* check for domain error */
-    if (d0 < 0.0) domain_error("log2");
+    if (d < 0.0) domain_error("log2");
     /* check for pole error */
-    if (d0 == 0.0) return DBL2NUM(-INFINITY);
-    d = log2(d0);
-    d += numbits;
-    return DBL2NUM(d);
+    if (d == 0.0) return DBL2NUM(-INFINITY);
+
+    return DBL2NUM(log2(d) + numbits); /* log2(d * 2 ** numbits) */
 }
 
 /*
@@ -591,7 +588,7 @@ math_log2(VALUE obj, VALUE x) https://github.com/ruby/ruby/blob/trunk/math.c#L588
 static VALUE
 math_log10(VALUE obj, VALUE x)
 {
-    double d0, d;
+    double d;
     size_t numbits;
 
     if (RB_BIGNUM_TYPE_P(x) && BIGNUM_POSITIVE_P(x) &&
@@ -603,15 +600,13 @@ math_log10(VALUE obj, VALUE x) https://github.com/ruby/ruby/blob/trunk/math.c#L600
 	numbits = 0;
     }
 
-    d0 = Get_Double(x);
+    d = Get_Double(x);
     /* check for domain error */
-    if (d0 < 0.0) domain_error("log10");
+    if (d < 0.0) domain_error("log10");
     /* check for pole error */
-    if (d0 == 0.0) return DBL2NUM(-INFINITY);
-    d = log10(d0);
-    if (numbits)
-        d += numbits * log10(2); /* log10(2**numbits) */
-    return DBL2NUM(d);
+    if (d == 0.0) return DBL2NUM(-INFINITY);
+
+    return DBL2NUM(log10(d) + numbits * log10(2)); /* log10(d * 2 ** numbits) */
 }
 
 /*
Index: test/ruby/test_math.rb
===================================================================
--- test/ruby/test_math.rb	(revision 49978)
+++ test/ruby/test_math.rb	(revision 49979)
@@ -150,6 +150,7 @@ class TestMath < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_math.rb#L150
     check(1, Math.log(10, 10))
     check(2, Math.log(100, 10))
     check(Math.log(2.0 ** 64), Math.log(1 << 64))
+    check(Math.log(2) * 1024.0, Math.log(2 ** 1024))
     assert_nothing_raised { assert_infinity(Math.log(1.0/0)) }
     assert_nothing_raised { assert_infinity(-Math.log(+0.0)) }
     assert_nothing_raised { assert_infinity(-Math.log(-0.0)) }
@@ -164,6 +165,7 @@ class TestMath < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_math.rb#L165
     check(1, Math.log2(2))
     check(2, Math.log2(4))
     check(Math.log2(2.0 ** 64), Math.log2(1 << 64))
+    check(1024.0, Math.log2(2 ** 1024))
     assert_nothing_raised { assert_infinity(Math.log2(1.0/0)) }
     assert_nothing_raised { assert_infinity(-Math.log2(+0.0)) }
     assert_nothing_raised { assert_infinity(-Math.log2(-0.0)) }
@@ -175,6 +177,7 @@ class TestMath < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_math.rb#L177
     check(1, Math.log10(10))
     check(2, Math.log10(100))
     check(Math.log10(2.0 ** 64), Math.log10(1 << 64))
+    check(Math.log10(2) * 1024, Math.log10(2 ** 1024))
     assert_nothing_raised { assert_infinity(Math.log10(1.0/0)) }
     assert_nothing_raised { assert_infinity(-Math.log10(+0.0)) }
     assert_nothing_raised { assert_infinity(-Math.log10(-0.0)) }

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

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