ruby-changes:46252
From: nobu <ko1@a...>
Date: Sun, 16 Apr 2017 00:29:17 +0900 (JST)
Subject: [ruby-changes:46252] nobu:r58366 (trunk): bignum.c: fix inexact estimation
nobu 2017-04-16 00:29:09 +0900 (Sun, 16 Apr 2017) New Revision: 58366 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=58366 Log: bignum.c: fix inexact estimation * bignum.c (estimate_initial_sqrt): estimated square root is inexact if it is not equal to its ceil, needs Newton's method. [ruby-core:80696] [Bug #13440] Modified files: trunk/bignum.c trunk/test/ruby/test_integer.rb Index: bignum.c =================================================================== --- bignum.c (revision 58365) +++ bignum.c (revision 58366) @@ -6824,6 +6824,9 @@ estimate_initial_sqrt(VALUE *xp, const s https://github.com/ruby/ruby/blob/trunk/bignum.c#L6824 if (lowbits || (lowbits = !bary_zero_p(nds, len-dbl_per_bdig))) ++d; } + else { + lowbits = 1; + } rshift /= 2; rshift += (2-(len&1))*BITSPERDIG/2; if (rshift >= 0) { Index: test/ruby/test_integer.rb =================================================================== --- test/ruby/test_integer.rb (revision 58365) +++ test/ruby/test_integer.rb (revision 58366) @@ -488,5 +488,17 @@ class TestInteger < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_integer.rb#L488 assert_equal(exact, Integer.sqrt(x+1), "10**#{i}+1") assert_equal(exact-1, Integer.sqrt(x-1), "10**#{i}-1") end + + bug13440 = '[ruby-core:80696] [Bug #13440]' + too_big = [] + too_small = [] + 0.step(to: 50, by: 0.001) do |i| + n = (10**i).to_i + int_root = Integer.sqrt(n) + too_big << n if int_root*int_root > n + too_small << n if (int_root+1)*(int_root+1) <= n + end + assert_empty(too_big, bug13440) + assert_empty(too_small, bug13440) end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/