Project

General

Profile

Bug #15537 ยป fix-numeric-step-for-float.patch

shuujii (Shuji KOBAYASHI), 01/15/2019 10:13 AM

View differences:

numeric.c
ruby_float_step(VALUE from, VALUE to, VALUE step, int excl, int allow_endless)
{
if (RB_TYPE_P(from, T_FLOAT) || RB_TYPE_P(to, T_FLOAT) || RB_TYPE_P(step, T_FLOAT)) {
double beg = NUM2DBL(from);
double end = (allow_endless && NIL_P(to)) ? HUGE_VAL : NUM2DBL(to);
double unit = NUM2DBL(step);
double beg = NUM2DBL(from);
double end = (allow_endless && NIL_P(to)) ?
(unit < 0 ? -HUGE_VAL : HUGE_VAL) : NUM2DBL(to);
double n = ruby_float_step_size(beg, end, unit, excl);
long i;
test/ruby/test_numeric.rb
assert_step [10, 8, 6, 4, 2], [10, to: 1, by: -2]
assert_step [1.0, 3.0, 5.0, 7.0, 9.0], [1.0, 10.0, 2.0]
assert_step [1.0, 3.0, 5.0, 7.0, 9.0], [1.0, to: 10.0, by: 2.0]
assert_step [10.0, 8.0, 6.0, 4.0, 2.0], [10.0, 1, -2]
assert_step [10.0, 8.0, 6.0, 4.0, 2.0], [10.0, to: 1, by: -2]
assert_step [10.0, 8.0, 6.0, 4.0, 2.0], [10.0, 1, -2]
assert_step [10.0, 8.0, 6.0, 4.0, 2.0], [10, to: 1.0, by: -2]
assert_step [10.0, 8.0, 6.0, 4.0, 2.0], [10, 1.0, -2]
assert_step [1], [1, 10, bignum]
assert_step [1], [1, to: 10, by: bignum]
......
assert_step [10, 11, 12, 13], [10], inf: true
assert_step [10, 9, 8, 7], [10, by: -1], inf: true
assert_step [10, 9, 8, 7], [10, by: -1, to: nil], inf: true
assert_step [10.0, 9.0, 8.0, 7.0], [10, by: -1.0], inf: true
assert_step [10.0, 9.0, 8.0, 7.0], [10, by: -1.0, to: nil], inf: true
assert_step [10.0, 9.0, 8.0, 7.0], [10, nil, -1.0], inf: true
assert_step [10.0, 9.0, 8.0, 7.0], [10.0, by: -1], inf: true
assert_step [10.0, 9.0, 8.0, 7.0], [10.0, nil, -1], inf: true
assert_step [42, 42, 42, 42], [42, by: 0, to: -Float::INFINITY], inf: true
assert_step [42, 42, 42, 42], [42, by: 0, to: 42.5], inf: true
    (1-1/1)