Skip to content

Commit

Permalink
Revert "Use larger precision in divide for irrational or recurring re…
Browse files Browse the repository at this point in the history
…sults"

This reverts commit 74cd039.

[Reopen GH-94]
  • Loading branch information
mrkn committed Jan 20, 2021
1 parent 5a62428 commit 12cfd9a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 21 deletions.
13 changes: 7 additions & 6 deletions ext/bigdecimal/bigdecimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -1375,12 +1375,13 @@ 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;
if (2*DBLE_FIG > mx)
mx = 2*DBLE_FIG;
GUARD_OBJ((*c), VpCreateRbObject(mx + 2*BASE_FIG, "#0", true));
GUARD_OBJ((*res), VpCreateRbObject(mx*2 + 2*BASE_FIG, "#0", true));
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));
VpDivd(*c, *res, a, b);
return Qnil;
}
Expand Down
17 changes: 2 additions & 15 deletions test/bigdecimal/test_bigdecimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -936,13 +936,9 @@ def test_div
assert_equal(2, BigDecimal("2") / 1)
assert_equal(-2, BigDecimal("2") / -1)

assert_equal(BigDecimal('1486.868686869'),
(BigDecimal('1472.0') / BigDecimal('0.99')).round(9),
'[ruby-core:59365] [#9316]')
assert_equal(BigDecimal('1486.868686869'), BigDecimal('1472.0') / BigDecimal('0.99'), '[ruby-core:59365] [#9316]')

assert_in_delta(4.124045235,
(BigDecimal('0.9932') / (700 * BigDecimal('0.344045') / BigDecimal('1000.0'))).round(9, half: :up),
10**Float::MIN_10_EXP, '[#9305]')
assert_equal(4.124045235, BigDecimal('0.9932') / (700 * BigDecimal('0.344045') / BigDecimal('1000.0')), '[#9305]')

BigDecimal.mode(BigDecimal::EXCEPTION_INFINITY, false)
assert_positive_zero(BigDecimal("1.0") / BigDecimal("Infinity"))
Expand All @@ -956,15 +952,6 @@ def test_div
assert_raise_with_message(FloatDomainError, "Computation results to '-Infinity'") { BigDecimal("-1") / 0 }
end

def test_dev_precision
bug13754 = '[ruby-core:82107] [Bug #13754]'
a = BigDecimal('101')
b = BigDecimal('0.9163472602589686')
c = a/b
assert(c.precision > b.precision,
"(101/0.9163472602589686).precision >= (0.9163472602589686).precision #{bug13754}")
end

def test_div_with_float
assert_kind_of(BigDecimal, BigDecimal("3") / 1.5)
assert_equal(BigDecimal("0.5"), BigDecimal(1) / 2.0)
Expand Down

0 comments on commit 12cfd9a

Please sign in to comment.