Skip to content

Commit

Permalink
Allow passing both float and precision in BigDecimal#div
Browse files Browse the repository at this point in the history
Fix GH-212.
  • Loading branch information
mrkn committed Dec 9, 2021
1 parent 3cbc0c3 commit c4f1c60
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion ext/bigdecimal/bigdecimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -1938,11 +1938,15 @@ BigDecimal_div2(VALUE self, VALUE b, VALUE n)
Real *res = NULL;
Real *av = NULL, *bv = NULL, *cv = NULL;
size_t mx = ix + VpBaseFig()*2;
size_t b_prec = ix;
size_t pl = VpSetPrecLimit(0);

GUARD_OBJ(cv, VpCreateRbObject(mx + VpBaseFig(), "0", true));
GUARD_OBJ(av, GetVpValue(self, 1));
GUARD_OBJ(bv, GetVpValue(b, 1));
if (RB_FLOAT_TYPE_P(b) && b_prec > BIGDECIMAL_DOUBLE_FIGURES) {
b_prec = BIGDECIMAL_DOUBLE_FIGURES;
}
GUARD_OBJ(bv, GetVpValueWithPrec(b, b_prec, 1));
mx = av->Prec + bv->Prec + 2;
if (mx <= cv->MaxPrec) mx = cv->MaxPrec + 1;
GUARD_OBJ(res, VpCreateRbObject((mx * 2 + 2)*VpBaseFig(), "#0", true));
Expand Down
10 changes: 10 additions & 0 deletions test/bigdecimal/test_bigdecimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,16 @@ def test_div_bigdecimal
end
end

def test_div_bigdecimal_with_float_and_precision
x = BigDecimal(5)
y = 5.1
assert_equal(x.div(BigDecimal(y, 0), 8),
x.div(y, 8))

assert_equal(x.div(BigDecimal(y, 0), 100),
x.div(y, 100))
end

def test_abs_bigdecimal
x = BigDecimal((2**100).to_s)
assert_equal(1267650600228229401496703205376, x.abs)
Expand Down

0 comments on commit c4f1c60

Please sign in to comment.