summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authorKenta Murata <mrkn@mrkn.jp>2021-01-22 12:03:37 +0900
committerKenta Murata <mrkn@mrkn.jp>2021-12-24 02:28:55 +0900
commite1265c819870c6a4d6763529e9fbd2d70c722fe0 (patch)
treef98fb219b04b9a1879032787be059aeaf8b469ab /ext
parent8ee8ac64239626a9adea4e02ba3f0c4be4895e36 (diff)
[ruby/bigdecimal] Use larger precision in divide for irrational or recurring results
Just in case for irrational or recurring results, the precision of the quotient is set to at least more than 2*Float::DIG plus alpha. [Bug #13754] [Fix GH-94] https://github.com/ruby/bigdecimal/commit/99442c75d3
Diffstat (limited to 'ext')
-rw-r--r--ext/bigdecimal/bigdecimal.c13
1 files changed, 6 insertions, 7 deletions
diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c
index 7cec33b7c9..71499a7a19 100644
--- a/ext/bigdecimal/bigdecimal.c
+++ b/ext/bigdecimal/bigdecimal.c
@@ -1508,13 +1508,12 @@ BigDecimal_divide(VALUE self, VALUE r, Real **c, Real **res, Real **div)
SAVE(b);
*div = b;
- mx = a->Prec + vabs(a->exponent);
- if (mx < b->Prec + vabs(b->exponent)) mx = b->Prec + vabs(b->exponent);
- mx++; /* NOTE: An additional digit is needed for the compatibility to
- the version 1.2.1 and the former. */
- mx = (mx + 1) * VpBaseFig();
- GUARD_OBJ((*c), VpCreateRbObject(mx, "#0", true));
- GUARD_OBJ((*res), VpCreateRbObject((mx+1) * 2 +(VpBaseFig() + 1), "#0", true));
+ mx = (a->Prec > b->Prec) ? a->Prec : b->Prec;
+ mx *= BASE_FIG;
+ 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));
VpDivd(*c, *res, a, b);
return Qnil;
}