Skip to content

Commit a831065

Browse files
authored
Fix modulo/remainder of negative zero (#441)
1 parent 0aa97bb commit a831065

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

ext/bigdecimal/bigdecimal.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1735,7 +1735,8 @@ BigDecimal_DoDivmod(VALUE self, VALUE r, NULLABLE_BDVALUE *div, NULLABLE_BDVALUE
17351735
}
17361736
if (VpIsZero(a.real)) {
17371737
VALUE zero = BigDecimal_positive_zero();
1738-
*div = *mod = (NULLABLE_BDVALUE) { zero, DATA_PTR(zero) };
1738+
*div = (NULLABLE_BDVALUE) { zero, DATA_PTR(zero) };
1739+
*mod = bdvalue_nullable(a);
17391740
goto Done;
17401741
}
17411742
if (VpIsInf(b.real)) {

test/bigdecimal/test_bigdecimal.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2740,6 +2740,16 @@ def test_divmod_modulo_remainder_infinity
27402740
end
27412741
end
27422742

2743+
def test_signed_zero_modulo_remainder
2744+
[-0.0, +0.0].each do |a|
2745+
[-1.0, +1.0].each do |b|
2746+
assert_equal(BigDecimal(a.remainder(b)).to_s, BigDecimal(a).remainder(BigDecimal(b)).to_s)
2747+
assert_equal(BigDecimal(a.modulo(b)).to_s, BigDecimal(a).modulo(BigDecimal(b)).to_s)
2748+
assert_equal(a.divmod(b).map {|x| BigDecimal(x)}.inspect, BigDecimal(a).divmod(BigDecimal(b)).inspect)
2749+
end
2750+
end
2751+
end
2752+
27432753
def test_bsearch_for_bigdecimal
27442754
assert_raise(TypeError) {
27452755
(BigDecimal('0.5')..BigDecimal('2.25')).bsearch

0 commit comments

Comments
 (0)