Skip to content

Commit 4b8572d

Browse files
mrzasamrkn
authored andcommitted
Handle correctly #remainder with infinity. Fixes #187
1 parent 2be5031 commit 4b8572d

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-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, 1));
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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2260,6 +2260,17 @@ def test_llong_min_gh_200
22602260
assert_equal(BigDecimal(minus_ullong_max.to_s), BigDecimal(minus_ullong_max), "[GH-200]")
22612261
end
22622262

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

0 commit comments

Comments
 (0)