ruby-changes:70478
From: Kenta <ko1@a...>
Date: Fri, 24 Dec 2021 02:30:05 +0900 (JST)
Subject: [ruby-changes:70478] 98918209b7 (master): [ruby/bigdecimal] Allow passing both float and precision in BigDecimal#div
https://git.ruby-lang.org/ruby.git/commit/?id=98918209b7 From 98918209b7260b4ecb7ea503728ae3f0853866e0 Mon Sep 17 00:00:00 2001 From: Kenta Murata <mrkn@m...> Date: Thu, 9 Dec 2021 21:35:33 +0900 Subject: [ruby/bigdecimal] Allow passing both float and precision in BigDecimal#div Fix GH-212. https://github.com/ruby/bigdecimal/commit/900bb7fcf5 --- ext/bigdecimal/bigdecimal.c | 6 +++++- test/bigdecimal/test_bigdecimal.rb | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c index 47b10d6a491..36173f35532 100644 --- a/ext/bigdecimal/bigdecimal.c +++ b/ext/bigdecimal/bigdecimal.c @@ -1938,11 +1938,15 @@ BigDecimal_div2(VALUE self, VALUE b, VALUE n) https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L1938 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)); diff --git a/test/bigdecimal/test_bigdecimal.rb b/test/bigdecimal/test_bigdecimal.rb index 273abf78858..9e8f0d593b5 100644 --- a/test/bigdecimal/test_bigdecimal.rb +++ b/test/bigdecimal/test_bigdecimal.rb @@ -1091,6 +1091,16 @@ class TestBigDecimal < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/bigdecimal/test_bigdecimal.rb#L1091 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) -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/