Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions ext/bigdecimal/bigdecimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -5489,7 +5489,7 @@ VpAsgn(Real *c, Real *a, int isw)
VP_EXPORT size_t
VpAddSub(Real *c, Real *a, Real *b, int operation)
{
short sw, isw;
short sw, isw, sign;
Real *a_ptr, *b_ptr;
size_t n, na, nb, i;
DECDIG mrv;
Expand Down Expand Up @@ -5590,19 +5590,20 @@ VpAddSub(Real *c, Real *a, Real *b, int operation)
if (isw) { /* addition */
VpSetSign(c, 1);
mrv = VpAddAbs(a_ptr, b_ptr, c);
VpSetSign(c, isw / 2);
sign = isw / 2;
}
else { /* subtraction */
VpSetSign(c, 1);
mrv = VpSubAbs(a_ptr, b_ptr, c);
if (a_ptr == a) {
VpSetSign(c,VpGetSign(a));
}
else {
VpSetSign(c, VpGetSign(a_ptr) * sw);
}
sign = a_ptr == a ? VpGetSign(a) : VpGetSign(a_ptr) * sw;
}
if (VpIsInf(c)) {
VpSetInf(c, sign);
}
else {
VpSetSign(c, sign);
VpInternalRound(c, 0, (c->Prec > 0) ? c->frac[c->Prec-1] : 0, mrv);
}
VpInternalRound(c, 0, (c->Prec > 0) ? c->frac[c->Prec-1] : 0, mrv);

#ifdef BIGDECIMAL_DEBUG
if (gfDebug) {
Expand Down Expand Up @@ -5995,11 +5996,11 @@ VpMult(Real *c, Real *a, Real *b)
/* set LHSV c info */

c->exponent = a->exponent; /* set exponent */
VpSetSign(c, VpGetSign(a) * VpGetSign(b)); /* set sign */
if (!AddExponent(c, b->exponent)) {
if (w) rbd_free_struct(c);
return 0;
}
VpSetSign(c, VpGetSign(a) * VpGetSign(b)); /* set sign */
carry = 0;
nc = ind_c = MxIndAB;
memset(c->frac, 0, (nc + 1) * sizeof(DECDIG)); /* Initialize c */
Expand Down Expand Up @@ -6246,10 +6247,10 @@ VpDivd(Real *c, Real *r, Real *a, Real *b)
out_side:
c->Prec = word_c;
c->exponent = a->exponent;
VpSetSign(c, VpGetSign(a) * VpGetSign(b));
if (!AddExponent(c, 2)) return 0;
if (!AddExponent(c, -(b->exponent))) return 0;

VpSetSign(c, VpGetSign(a) * VpGetSign(b));
VpNmlz(c); /* normalize c */
r->Prec = word_r;
r->exponent = a->exponent;
Expand Down
39 changes: 39 additions & 0 deletions test/bigdecimal/test_bigdecimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,45 @@ def test_exception_overflow
end
end

def test_mult_div_overflow_underflow_sign
BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false)
BigDecimal.mode(BigDecimal::EXCEPTION_UNDERFLOW, false)

large_x = BigDecimal("10")
100.times do
x2 = large_x * large_x
break if x2.infinite?
large_x = x2
end

small_x = BigDecimal("0.1")
100.times do
x2 = small_x * small_x
break if x2.zero?
small_x = x2
end

assert_positive_infinite(large_x * large_x)
assert_negative_infinite(large_x * (-large_x))
assert_negative_infinite((-large_x) * large_x)
assert_positive_infinite((-large_x) * (-large_x))

assert_positive_zero(small_x * small_x)
assert_negative_zero(small_x * (-small_x))
assert_negative_zero((-small_x) * small_x)
assert_positive_zero((-small_x) * (-small_x))

assert_positive_infinite(large_x.div(small_x, 10))
assert_negative_infinite(large_x.div(-small_x, 10))
assert_negative_infinite((-large_x).div(small_x, 10))
assert_positive_infinite((-large_x).div(-small_x, 10))

assert_positive_zero(small_x.div(large_x, 10))
assert_negative_zero(small_x.div(-large_x, 10))
assert_negative_zero((-small_x).div(large_x, 10))
assert_positive_zero((-small_x).div(-large_x, 10))
end

def test_exception_zerodivide
BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false)
_test_mode(BigDecimal::EXCEPTION_ZERODIVIDE) { 1 / BigDecimal("0") }
Expand Down