Fix integer/float remainder with infinity argument of opposite sign
Previously, the result was incorrect: 4.remainder(-Float::INFINITY) Before: => NaN After: => 4 4.2.remainder(-Float::INFINITY) Before: => NaN After: => 4.2 Fixes [Bug #6120]
This commit is contained in:
parent
701001e36e
commit
aaab3b1de9
Notes:
git
2021-03-13 00:35:50 +09:00
11
numeric.c
11
numeric.c
@ -656,6 +656,11 @@ num_remainder(VALUE x, VALUE y)
|
|||||||
rb_num_positive_int_p(y)) ||
|
rb_num_positive_int_p(y)) ||
|
||||||
(rb_num_positive_int_p(x) &&
|
(rb_num_positive_int_p(x) &&
|
||||||
rb_num_negative_int_p(y)))) {
|
rb_num_negative_int_p(y)))) {
|
||||||
|
if (RB_TYPE_P(y, T_FLOAT)) {
|
||||||
|
if (isinf(RFLOAT_VALUE(y))) {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
}
|
||||||
return rb_funcall(z, '-', 1, y);
|
return rb_funcall(z, '-', 1, y);
|
||||||
}
|
}
|
||||||
return z;
|
return z;
|
||||||
@ -1151,11 +1156,11 @@ flodivmod(double x, double y, double *divp, double *modp)
|
|||||||
div = x;
|
div = x;
|
||||||
else {
|
else {
|
||||||
div = (x - mod) / y;
|
div = (x - mod) / y;
|
||||||
if (modp && divp) div = round(div);
|
if (modp && divp) div = round(div);
|
||||||
}
|
}
|
||||||
if (y*mod < 0) {
|
if (y*mod < 0) {
|
||||||
mod += y;
|
mod += y;
|
||||||
div -= 1.0;
|
div -= 1.0;
|
||||||
}
|
}
|
||||||
if (modp) *modp = mod;
|
if (modp) *modp = mod;
|
||||||
if (divp) *divp = div;
|
if (divp) *divp = div;
|
||||||
|
@ -384,6 +384,18 @@ class TestNumeric < Test::Unit::TestCase
|
|||||||
end;
|
end;
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_remainder_infinity
|
||||||
|
assert_equal(4, 4.remainder(Float::INFINITY))
|
||||||
|
assert_equal(4, 4.remainder(-Float::INFINITY))
|
||||||
|
assert_equal(-4, -4.remainder(Float::INFINITY))
|
||||||
|
assert_equal(-4, -4.remainder(-Float::INFINITY))
|
||||||
|
|
||||||
|
assert_equal(4.2, 4.2.remainder(Float::INFINITY))
|
||||||
|
assert_equal(4.2, 4.2.remainder(-Float::INFINITY))
|
||||||
|
assert_equal(-4.2, -4.2.remainder(Float::INFINITY))
|
||||||
|
assert_equal(-4.2, -4.2.remainder(-Float::INFINITY))
|
||||||
|
end
|
||||||
|
|
||||||
def test_comparison_comparable
|
def test_comparison_comparable
|
||||||
bug12864 = '[ruby-core:77713] [Bug #12864]'
|
bug12864 = '[ruby-core:77713] [Bug #12864]'
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user