1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58366 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2017-04-15 15:29:09 +00:00
parent 5fb0f3f022
commit df76f366a7
2 changed files with 15 additions and 0 deletions

View file

@ -6824,6 +6824,9 @@ estimate_initial_sqrt(VALUE *xp, const size_t xn, const BDIGIT *nds, size_t len)
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) {

View file

@ -488,5 +488,17 @@ class TestInteger < Test::Unit::TestCase
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