[ruby/bigdecimal] Handle correctly #remainder with infinity. Fixes

https://github.com/ruby/bigdecimal/pull/187

https://github.com/ruby/bigdecimal/commit/4b8572d452
This commit is contained in:
Maciej Rzasa 2022-11-30 22:38:25 +01:00 committed by git
parent 81dc3a1780
commit 36e3d46d35
2 changed files with 18 additions and 0 deletions

View File

@ -2082,6 +2082,13 @@ BigDecimal_divremain(VALUE self, VALUE r, Real **dv, Real **rv)
if (!b) return DoSomeOne(self, r, rb_intern("remainder"));
SAVE(b);
if (VpIsPosInf(b) || VpIsNegInf(b)) {
GUARD_OBJ(*dv, NewZeroWrapLimited(1, 1));
VpSetZero(*dv, 1);
*rv = a;
return Qnil;
}
mx = (a->MaxPrec + b->MaxPrec) *VpBaseFig();
GUARD_OBJ(c, NewZeroWrapLimited(1, mx));
GUARD_OBJ(res, NewZeroWrapNolimit(1, (mx+1) * 2 + (VpBaseFig() + 1)));

View File

@ -2260,6 +2260,17 @@ class TestBigDecimal < Test::Unit::TestCase
assert_equal(BigDecimal(minus_ullong_max.to_s), BigDecimal(minus_ullong_max), "[GH-200]")
end
def test_reminder_infinity_gh_187
# https://github.com/ruby/bigdecimal/issues/187
BigDecimal.save_exception_mode do
BigDecimal.mode(BigDecimal::EXCEPTION_INFINITY, false)
BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false)
bd = BigDecimal("4.2")
assert_equal(bd.remainder(BigDecimal("+Infinity")), bd)
assert_equal(bd.remainder(BigDecimal("-Infinity")), bd)
end
end
def assert_no_memory_leak(code, *rest, **opt)
code = "8.times {20_000.times {begin #{code}; rescue NoMemoryError; end}; GC.start}"
super(["-rbigdecimal"],