[ruby/bigdecimal] Allow passing both float and precision in BigDecimal#div

Fix GH-212.

https://github.com/ruby/bigdecimal/commit/900bb7fcf5
This commit is contained in:
Kenta Murata 2021-12-09 21:35:33 +09:00
parent d0897e3f3a
commit 98918209b7
No known key found for this signature in database
GPG Key ID: CEFE8AFB6081B062
2 changed files with 15 additions and 1 deletions

View File

@ -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));

View File

@ -1091,6 +1091,16 @@ class TestBigDecimal < Test::Unit::TestCase
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)