diff --git a/numeric.c b/numeric.c index 5cb1aad8a1..697d692f0b 100644 --- a/numeric.c +++ b/numeric.c @@ -2514,9 +2514,10 @@ int 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; diff --git a/test/ruby/test_numeric.rb b/test/ruby/test_numeric.rb index e48c3448e8..5947a33fd6 100644 --- a/test/ruby/test_numeric.rb +++ b/test/ruby/test_numeric.rb @@ -308,6 +308,11 @@ def test_step 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] @@ -324,6 +329,11 @@ def test_step 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