From ac7daa5f15c4583b3cdf7db046e0dcad772360af Mon Sep 17 00:00:00 2001 From: Kenta Murata Date: Fri, 24 Dec 2021 09:43:22 +0900 Subject: [PATCH 1/5] Remove unused variable --- ext/bigdecimal/bigdecimal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c index 4111fbd7..f4dcb2ee 100644 --- a/ext/bigdecimal/bigdecimal.c +++ b/ext/bigdecimal/bigdecimal.c @@ -1618,7 +1618,7 @@ BigDecimal_divide(VALUE self, VALUE r, Real **c, Real **res, Real **div) /* For c = self.div(r): with round operation */ { ENTER(5); - Real *a, *b, *d; + Real *a, *b; ssize_t a_prec, b_prec; size_t mx; From 79c09b4dac66042810463e2a91e803ec4cd82d68 Mon Sep 17 00:00:00 2001 From: Kenta Murata Date: Wed, 19 Jan 2022 14:31:17 +0900 Subject: [PATCH 2/5] Fix typo --- test/bigdecimal/test_bigdecimal.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/bigdecimal/test_bigdecimal.rb b/test/bigdecimal/test_bigdecimal.rb index 825d7ec9..11e69282 100644 --- a/test/bigdecimal/test_bigdecimal.rb +++ b/test/bigdecimal/test_bigdecimal.rb @@ -973,7 +973,7 @@ def test_div assert_raise_with_message(FloatDomainError, "Computation results in '-Infinity'") { BigDecimal("-1") / 0 } end - def test_dev_precision + def test_div_precision bug13754 = '[ruby-core:82107] [Bug #13754]' a = BigDecimal('101') b = BigDecimal('0.9163472602589686') From 127a1b5a31c4290945035ec7e07bb7b185a81ee2 Mon Sep 17 00:00:00 2001 From: Kenta Murata Date: Wed, 19 Jan 2022 15:53:36 +0900 Subject: [PATCH 3/5] Fix the maximum precision of the quotient Fixes GH-220 --- ext/bigdecimal/bigdecimal.c | 15 +++++++-------- test/bigdecimal/test_bigdecimal.rb | 7 +++++++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c index f4dcb2ee..fc74c0be 100644 --- a/ext/bigdecimal/bigdecimal.c +++ b/ext/bigdecimal/bigdecimal.c @@ -1647,18 +1647,16 @@ BigDecimal_divide(VALUE self, VALUE r, Real **c, Real **res, Real **div) SAVE(b); *div = b; - mx = (a->Prec > b->Prec) ? a->Prec : b->Prec; - mx *= BASE_FIG; - BigDecimal_count_precision_and_scale(self, &a_prec, NULL); BigDecimal_count_precision_and_scale(rr, &b_prec, NULL); mx = (a_prec > b_prec) ? a_prec : b_prec; + mx *= 2; if (2*BIGDECIMAL_DOUBLE_FIGURES > mx) mx = 2*BIGDECIMAL_DOUBLE_FIGURES; GUARD_OBJ((*c), VpCreateRbObject(mx + 2*BASE_FIG, "#0", true)); - GUARD_OBJ((*res), VpCreateRbObject(mx*2 + 2*BASE_FIG, "#0", true)); + GUARD_OBJ((*res), VpCreateRbObject((mx + 1)*2 + 2*BASE_FIG, "#0", true)); VpDivd(*c, *res, a, b); return Qnil; @@ -1808,6 +1806,8 @@ BigDecimal_DoDivmod(VALUE self, VALUE r, Real **div, Real **mod) BigDecimal_count_precision_and_scale(rr, &b_prec, NULL); mx = (a_prec > b_prec) ? a_prec : b_prec; + mx *= 2; + if (2*BIGDECIMAL_DOUBLE_FIGURES > mx) mx = 2*BIGDECIMAL_DOUBLE_FIGURES; @@ -5931,18 +5931,17 @@ VpDivd(Real *c, Real *r, Real *a, Real *b) word_c = c->MaxPrec; word_r = r->MaxPrec; - ind_c = 0; - ind_r = 1; - if (word_a >= word_r) goto space_error; + ind_r = 1; r->frac[0] = 0; while (ind_r <= word_a) { r->frac[ind_r] = a->frac[ind_r - 1]; ++ind_r; } - while (ind_r < word_r) r->frac[ind_r++] = 0; + + ind_c = 0; while (ind_c < word_c) c->frac[ind_c++] = 0; /* initial procedure */ diff --git a/test/bigdecimal/test_bigdecimal.rb b/test/bigdecimal/test_bigdecimal.rb index 11e69282..0cd85249 100644 --- a/test/bigdecimal/test_bigdecimal.rb +++ b/test/bigdecimal/test_bigdecimal.rb @@ -973,6 +973,13 @@ def test_div assert_raise_with_message(FloatDomainError, "Computation results in '-Infinity'") { BigDecimal("-1") / 0 } end + def test_div_gh220 + x = BigDecimal("1.0") + y = BigDecimal("3672577333.6608990499165058135986328125") + c = BigDecimal("0.272288343892592687909520102748926752911779209181321744700032723729015151607289998e-9") + assert_equal(c, x / y, "[GH-220]") + end + def test_div_precision bug13754 = '[ruby-core:82107] [Bug #13754]' a = BigDecimal('101') From 57e2194135f38db8320db08e7bc8dcc32f7d7e42 Mon Sep 17 00:00:00 2001 From: Kenta Murata Date: Wed, 19 Jan 2022 16:53:26 +0900 Subject: [PATCH 4/5] Version 3.1.2 --- bigdecimal.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bigdecimal.gemspec b/bigdecimal.gemspec index fd49c1b0..2ed7d093 100644 --- a/bigdecimal.gemspec +++ b/bigdecimal.gemspec @@ -2,7 +2,7 @@ Gem::Specification.new do |s| s.name = "bigdecimal" - s.version = "3.1.1" + s.version = "3.1.2" s.authors = ["Kenta Murata", "Zachary Scott", "Shigeo Kobayashi"] s.email = ["mrkn@mrkn.jp"] From 9899d86d86b38473347a5c04d3fcdd1a0e4589df Mon Sep 17 00:00:00 2001 From: Kenta Murata Date: Wed, 19 Jan 2022 16:55:12 +0900 Subject: [PATCH 5/5] CHANGES: Add 3.1.2 entries --- CHANGES.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index a30fcac2..f94d6602 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,11 @@ # CHANGES +## 3.1.2 + +* Fix the maximum precision of the quotient. [GH-220] + + Reported by @grk + ## 3.1.1 * Fix the result precision of `BigDecimal#divmod`. [GH-219]