[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:
parent
d0897e3f3a
commit
98918209b7
@ -1938,11 +1938,15 @@ BigDecimal_div2(VALUE self, VALUE b, VALUE n)
|
|||||||
Real *res = NULL;
|
Real *res = NULL;
|
||||||
Real *av = NULL, *bv = NULL, *cv = NULL;
|
Real *av = NULL, *bv = NULL, *cv = NULL;
|
||||||
size_t mx = ix + VpBaseFig()*2;
|
size_t mx = ix + VpBaseFig()*2;
|
||||||
|
size_t b_prec = ix;
|
||||||
size_t pl = VpSetPrecLimit(0);
|
size_t pl = VpSetPrecLimit(0);
|
||||||
|
|
||||||
GUARD_OBJ(cv, VpCreateRbObject(mx + VpBaseFig(), "0", true));
|
GUARD_OBJ(cv, VpCreateRbObject(mx + VpBaseFig(), "0", true));
|
||||||
GUARD_OBJ(av, GetVpValue(self, 1));
|
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;
|
mx = av->Prec + bv->Prec + 2;
|
||||||
if (mx <= cv->MaxPrec) mx = cv->MaxPrec + 1;
|
if (mx <= cv->MaxPrec) mx = cv->MaxPrec + 1;
|
||||||
GUARD_OBJ(res, VpCreateRbObject((mx * 2 + 2)*VpBaseFig(), "#0", true));
|
GUARD_OBJ(res, VpCreateRbObject((mx * 2 + 2)*VpBaseFig(), "#0", true));
|
||||||
|
@ -1091,6 +1091,16 @@ class TestBigDecimal < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
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
|
def test_abs_bigdecimal
|
||||||
x = BigDecimal((2**100).to_s)
|
x = BigDecimal((2**100).to_s)
|
||||||
assert_equal(1267650600228229401496703205376, x.abs)
|
assert_equal(1267650600228229401496703205376, x.abs)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user