mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
numeric.c: float overflow
* numeric.c (ruby_num_interval_step_size): get rid of float conversion overflow. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45213 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
a4a551f856
commit
99431e7355
2 changed files with 10 additions and 1 deletions
|
@ -1836,7 +1836,8 @@ ruby_num_interval_step_size(VALUE from, VALUE to, VALUE step, int excl)
|
||||||
double n = ruby_float_step_size(NUM2DBL(from), NUM2DBL(to), NUM2DBL(step), excl);
|
double n = ruby_float_step_size(NUM2DBL(from), NUM2DBL(to), NUM2DBL(step), excl);
|
||||||
|
|
||||||
if (isinf(n)) return DBL2NUM(n);
|
if (isinf(n)) return DBL2NUM(n);
|
||||||
return LONG2FIX(n);
|
if (POSFIXABLE(n)) return LONG2FIX(n);
|
||||||
|
return rb_dbl2big(n);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
VALUE result;
|
VALUE result;
|
||||||
|
|
|
@ -271,6 +271,14 @@ class TestNumeric < Test::Unit::TestCase
|
||||||
assert_equal(bignum*2+1, (-bignum).step(bignum, 1).size)
|
assert_equal(bignum*2+1, (-bignum).step(bignum, 1).size)
|
||||||
assert_equal(bignum*2, (-bignum).step(bignum-1, 1).size)
|
assert_equal(bignum*2, (-bignum).step(bignum-1, 1).size)
|
||||||
|
|
||||||
|
assert_equal(10+1, (0.0).step(10.0, 1.0).size)
|
||||||
|
|
||||||
|
i, bigflo = 1, bignum.to_f
|
||||||
|
i <<= 1 until (bigflo - i).to_i < bignum
|
||||||
|
bigflo -= i >> 1
|
||||||
|
assert_equal(bigflo.to_i, (0.0).step(bigflo-1.0, 1.0).size)
|
||||||
|
assert_operator((0.0).step(bignum.to_f, 1.0).size, :>=, bignum) # may loose precision
|
||||||
|
|
||||||
assert_step [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 10]
|
assert_step [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 10]
|
||||||
assert_step [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, to: 10]
|
assert_step [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, to: 10]
|
||||||
assert_step [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, to: 10, by: nil]
|
assert_step [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, to: 10, by: nil]
|
||||||
|
|
Loading…
Add table
Reference in a new issue