diff options
author | Maciej Rzasa <maciejrzasa@gmail.com> | 2022-11-30 22:38:25 +0100 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2023-02-17 17:46:27 +0000 |
commit | 36e3d46d35b6a904533e58809369054b135c33d7 (patch) | |
tree | f7bd9cefc8281e5a04feb2e0ab31a9887014dea3 | |
parent | 81dc3a1780319f4bc232af407edea60a35d251ed (diff) | |
download | ruby-36e3d46d35b6a904533e58809369054b135c33d7.tar.gz |
[ruby/bigdecimal] Handle correctly #remainder with infinity. Fixes
https://github.com/ruby/bigdecimal/pull/187
https://github.com/ruby/bigdecimal/commit/4b8572d452
-rw-r--r-- | ext/bigdecimal/bigdecimal.c | 7 | ||||
-rw-r--r-- | test/bigdecimal/test_bigdecimal.rb | 11 |
2 files changed, 18 insertions, 0 deletions
diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c index ce50e780f8..637e824af5 100644 --- a/ext/bigdecimal/bigdecimal.c +++ b/ext/bigdecimal/bigdecimal.c @@ -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))); diff --git a/test/bigdecimal/test_bigdecimal.rb b/test/bigdecimal/test_bigdecimal.rb index 9c3d72ecaf..2fc22d224f 100644 --- a/test/bigdecimal/test_bigdecimal.rb +++ b/test/bigdecimal/test_bigdecimal.rb @@ -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"], |