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

ruby-changes:30283

From: akr <ko1@a...>
Date: Fri, 2 Aug 2013 23:53:35 +0900 (JST)
Subject: [ruby-changes:30283] akr:r42335 (trunk): * bignum.c (power_cache_get_power): Appry bigtrunc to the result of

akr	2013-08-02 23:53:22 +0900 (Fri, 02 Aug 2013)

  New Revision: 42335

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

  Log:
    * bignum.c (power_cache_get_power): Appry bigtrunc to the result of
      bigsq.
      (big2str_karatsuba): Fix number of leading zero characters.

  Modified files:
    trunk/ChangeLog
    trunk/bignum.c
    trunk/test/ruby/test_bignum.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 42334)
+++ ChangeLog	(revision 42335)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Aug  2 23:50:53 2013  Tanaka Akira  <akr@f...>
+
+	* bignum.c (power_cache_get_power): Appry bigtrunc to the result of
+	  bigsq.
+	  (big2str_karatsuba): Fix number of leading zero characters.
+
 Fri Aug  2 23:48:36 2013  Nobuyoshi Nakada  <nobu@r...>
 
 	* parse.y (parser_yylex): calculate denominator directly as powers of
Index: bignum.c
===================================================================
--- bignum.c	(revision 42334)
+++ bignum.c	(revision 42335)
@@ -4156,7 +4156,7 @@ power_cache_get_power(int base, int powe https://github.com/ruby/ruby/blob/trunk/bignum.c#L4156
             numdigits = numdigits0;
         }
         else {
-            power = bigsq(power_cache_get_power(base, power_level - 1, &numdigits));
+            power = bigtrunc(bigsq(power_cache_get_power(base, power_level - 1, &numdigits)));
             numdigits *= 2;
         }
         rb_obj_hide(power);
@@ -4358,21 +4358,26 @@ big2str_karatsuba(struct big2str_struct https://github.com/ruby/ruby/blob/trunk/bignum.c#L4358
         bds = BDIGITS(b);
     }
 
-    if (lower_power_level != power_level-1 && b2s->ptr) {
-        len = (half_numdigits - lower_numdigits) * 2;
-        memset(b2s->ptr, '0', len);
-        b2s->ptr += len;
-    }
-
     if (lower_power_level == 0 &&
             (xn < bn ||
              (xn == bn && bary_cmp(xds, xn, bds, bn) < 0))) {
+        if (b2s->ptr) {
+            len = half_numdigits * 2 - lower_numdigits;
+            memset(b2s->ptr, '0', len);
+            b2s->ptr += len;
+        }
 	big2str_orig(b2s, x, taillen);
     }
     else {
+        if (lower_power_level != power_level-1 && b2s->ptr) {
+            len = (half_numdigits - lower_numdigits) * 2;
+            memset(b2s->ptr, '0', len);
+            b2s->ptr += len;
+        }
         bigdivmod(x, b, &q, &r);
         bigtrunc(q);
         bigtrunc(r);
+        assert(RBIGNUM_LEN(q) <= RBIGNUM_LEN(b));
         rb_obj_hide(q);
         rb_obj_hide(r);
         big2str_karatsuba(b2s, q, lower_power_level, lower_numdigits+taillen);
@@ -4454,8 +4459,14 @@ rb_big2str1(VALUE x, int base) https://github.com/ruby/ruby/blob/trunk/bignum.c#L4459
 
 #ifndef NDEBUG
     if (0 < power_level) {
-        VALUE power0 = power_cache_get_power(base, power_level-1, NULL);
-        assert(FIX2LONG(bary_cmp(BDIGITS(x), RBIGNUM_LEN(x), BDIGITS(power0), RBIGNUM_LEN(power0))) >= 0);
+        VALUE power1 = power_cache_get_power(base, power_level-1, NULL);
+        assert(FIX2LONG(bary_cmp(BDIGITS(x), RBIGNUM_LEN(x), BDIGITS(power1), RBIGNUM_LEN(power1))) >= 0);
+        /*
+        {
+            VALUE power0 = power_cache_get_power(base, power_level, NULL);
+            assert(FIX2LONG(bary_cmp(BDIGITS(x), RBIGNUM_LEN(x), BDIGITS(power0), RBIGNUM_LEN(power0))) < 0);
+        }
+        */
     }
 #endif
 
Index: test/ruby/test_bignum.rb
===================================================================
--- test/ruby/test_bignum.rb	(revision 42334)
+++ test/ruby/test_bignum.rb	(revision 42335)
@@ -106,6 +106,8 @@ class TestBignum < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_bignum.rb#L106
     assert_equal("nd075ib45k86g" ,18446744073709551616.to_s(31), "[ruby-core:10686]")
     assert_equal("1777777777777777777777" ,18446744073709551615.to_s(8))
     assert_equal("-1777777777777777777777" ,-18446744073709551615.to_s(8))
+    assert_match(/\A10{99}1\z/, (10**100+1).to_s)
+    assert_match(/\A10{900}9{100}\z/, (10**1000+(10**100-1)).to_s)
   end
 
   b = 2**64

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

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