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

ruby-changes:31001

From: nobu <ko1@a...>
Date: Sat, 28 Sep 2013 23:26:05 +0900 (JST)
Subject: [ruby-changes:31001] nobu:r43080 (trunk): math.c: fix for Bignum argument

nobu	2013-09-28 23:25:59 +0900 (Sat, 28 Sep 2013)

  New Revision: 43080

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

  Log:
    math.c: fix for Bignum argument
    
    * math.c (math_log, math_log2, math_log10): fix for Bignum argument.
      numbits should be add only when right shifted.

  Modified files:
    trunk/ChangeLog
    trunk/math.c
    trunk/test/ruby/test_math.rb
Index: math.c
===================================================================
--- math.c	(revision 43079)
+++ math.c	(revision 43080)
@@ -442,7 +442,7 @@ math_log(int argc, VALUE *argv) https://github.com/ruby/ruby/blob/trunk/math.c#L442
 {
     VALUE x, base;
     double d0, d;
-    size_t numbits = 0;
+    size_t numbits;
 
     rb_scan_args(argc, argv, "11", &x, &base);
 
@@ -451,6 +451,9 @@ math_log(int argc, VALUE *argv) https://github.com/ruby/ruby/blob/trunk/math.c#L451
         numbits -= DBL_MANT_DIG;
         x = rb_big_rshift(x, SIZET2NUM(numbits));
     }
+    else {
+	numbits = 0;
+    }
 
     Need_Float(x);
     d0 = RFLOAT_VALUE(x);
@@ -501,13 +504,16 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/math.c#L504
 math_log2(VALUE obj, VALUE x)
 {
     double d0, d;
-    size_t numbits = 0;
+    size_t numbits;
 
     if (RB_BIGNUM_TYPE_P(x) && RBIGNUM_POSITIVE_P(x) &&
             DBL_MAX_EXP <= (numbits = rb_absint_numwords(x, 1, NULL))) {
         numbits -= DBL_MANT_DIG;
         x = rb_big_rshift(x, SIZET2NUM(numbits));
     }
+    else {
+	numbits = 0;
+    }
 
     Need_Float(x);
     d0 = RFLOAT_VALUE(x);
@@ -540,13 +546,16 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/math.c#L546
 math_log10(VALUE obj, VALUE x)
 {
     double d0, d;
-    size_t numbits = 0;
+    size_t numbits;
 
     if (RB_BIGNUM_TYPE_P(x) && RBIGNUM_POSITIVE_P(x) &&
             DBL_MAX_EXP <= (numbits = rb_absint_numwords(x, 1, NULL))) {
         numbits -= DBL_MANT_DIG;
         x = rb_big_rshift(x, SIZET2NUM(numbits));
     }
+    else {
+	numbits = 0;
+    }
 
     Need_Float(x);
     d0 = RFLOAT_VALUE(x);
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 43079)
+++ ChangeLog	(revision 43080)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Sep 28 23:25:56 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* math.c (math_log, math_log2, math_log10): fix for Bignum argument.
+	  numbits should be add only when right shifted.
+
 Sat Sep 28 14:30:29 2013  KOSAKI Motohiro  <kosaki.motohiro@g...>
 
 	* test/dl/test_base.rb: {libc, libm} detection now handle GNU/Hurd
Index: test/ruby/test_math.rb
===================================================================
--- test/ruby/test_math.rb	(revision 43079)
+++ test/ruby/test_math.rb	(revision 43080)
@@ -136,6 +136,7 @@ class TestMath < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_math.rb#L136
     check(0, Math.log(1, 10))
     check(1, Math.log(10, 10))
     check(2, Math.log(100, 10))
+    check(Math.log(2.0 ** 64), Math.log(1 << 64))
     assert_equal(1.0/0, Math.log(1.0/0))
     assert_nothing_raised { assert_infinity(-Math.log(+0.0)) }
     assert_nothing_raised { assert_infinity(-Math.log(-0.0)) }
@@ -147,6 +148,7 @@ class TestMath < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_math.rb#L148
     check(0, Math.log2(1))
     check(1, Math.log2(2))
     check(2, Math.log2(4))
+    check(Math.log2(2.0 ** 64), Math.log2(1 << 64))
     assert_equal(1.0/0, Math.log2(1.0/0))
     assert_nothing_raised { assert_infinity(-Math.log2(+0.0)) }
     assert_nothing_raised { assert_infinity(-Math.log2(-0.0)) }
@@ -157,6 +159,7 @@ class TestMath < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_math.rb#L159
     check(0, Math.log10(1))
     check(1, Math.log10(10))
     check(2, Math.log10(100))
+    check(Math.log10(2.0 ** 64), Math.log10(1 << 64))
     assert_equal(1.0/0, 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/

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