Skip to content

Commit 5675a01

Browse files
committed
Handle correctly #remainder with infinity. Fixes #187
1 parent d305e87 commit 5675a01

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

ext/bigdecimal/bigdecimal.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2082,6 +2082,13 @@ BigDecimal_divremain(VALUE self, VALUE r, Real **dv, Real **rv)
20822082
if (!b) return DoSomeOne(self, r, rb_intern("remainder"));
20832083
SAVE(b);
20842084

2085+
if (VpIsPosInf(b) || VpIsNegInf(b)) {
2086+
GUARD_OBJ(*dv, NewZeroWrapLimited(1, mx));
2087+
VpSetZero(*dv, 1);
2088+
*rv = a;
2089+
return Qnil;
2090+
}
2091+
20852092
mx = (a->MaxPrec + b->MaxPrec) *VpBaseFig();
20862093
GUARD_OBJ(c, NewZeroWrapLimited(1, mx));
20872094
GUARD_OBJ(res, NewZeroWrapNolimit(1, (mx+1) * 2 + (VpBaseFig() + 1)));

test/bigdecimal/test_bigdecimal.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2256,6 +2256,18 @@ def test_llong_min_gh_200
22562256
assert_equal(BigDecimal(minus_ullong_max.to_s), BigDecimal(minus_ullong_max), "[GH-200]")
22572257
end
22582258

2259+
def test_reminder_infinity_gh_187
2260+
# https://github.com/ruby/bigdecimal/issues/187
2261+
BigDecimal.save_exception_mode do
2262+
BigDecimal.mode(BigDecimal::EXCEPTION_INFINITY, false)
2263+
BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false)
2264+
bd = BigDecimal("4.2")
2265+
bd.remainder(2)
2266+
assert_equal(bd.remainder(BigDecimal("+Infinity")), bd)
2267+
assert_equal(bd.remainder(BigDecimal("-Infinity")), bd)
2268+
end
2269+
end
2270+
22592271
def assert_no_memory_leak(code, *rest, **opt)
22602272
code = "8.times {20_000.times {begin #{code}; rescue NoMemoryError; end}; GC.start}"
22612273
super(["-rbigdecimal"],

0 commit comments

Comments
 (0)