[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:
parent
81dc3a1780
commit
36e3d46d35
@ -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)));
|
||||
|
@ -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"],
|
||||
|
Loading…
x
Reference in New Issue
Block a user