Skip to content

Commit 127a1b5

Browse files
committed
Fix the maximum precision of the quotient
Fixes GH-220
1 parent 79c09b4 commit 127a1b5

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

ext/bigdecimal/bigdecimal.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,18 +1647,16 @@ BigDecimal_divide(VALUE self, VALUE r, Real **c, Real **res, Real **div)
16471647
SAVE(b);
16481648
*div = b;
16491649

1650-
mx = (a->Prec > b->Prec) ? a->Prec : b->Prec;
1651-
mx *= BASE_FIG;
1652-
16531650
BigDecimal_count_precision_and_scale(self, &a_prec, NULL);
16541651
BigDecimal_count_precision_and_scale(rr, &b_prec, NULL);
16551652
mx = (a_prec > b_prec) ? a_prec : b_prec;
1653+
mx *= 2;
16561654

16571655
if (2*BIGDECIMAL_DOUBLE_FIGURES > mx)
16581656
mx = 2*BIGDECIMAL_DOUBLE_FIGURES;
16591657

16601658
GUARD_OBJ((*c), VpCreateRbObject(mx + 2*BASE_FIG, "#0", true));
1661-
GUARD_OBJ((*res), VpCreateRbObject(mx*2 + 2*BASE_FIG, "#0", true));
1659+
GUARD_OBJ((*res), VpCreateRbObject((mx + 1)*2 + 2*BASE_FIG, "#0", true));
16621660
VpDivd(*c, *res, a, b);
16631661

16641662
return Qnil;
@@ -1808,6 +1806,8 @@ BigDecimal_DoDivmod(VALUE self, VALUE r, Real **div, Real **mod)
18081806
BigDecimal_count_precision_and_scale(rr, &b_prec, NULL);
18091807

18101808
mx = (a_prec > b_prec) ? a_prec : b_prec;
1809+
mx *= 2;
1810+
18111811
if (2*BIGDECIMAL_DOUBLE_FIGURES > mx)
18121812
mx = 2*BIGDECIMAL_DOUBLE_FIGURES;
18131813

@@ -5931,18 +5931,17 @@ VpDivd(Real *c, Real *r, Real *a, Real *b)
59315931
word_c = c->MaxPrec;
59325932
word_r = r->MaxPrec;
59335933

5934-
ind_c = 0;
5935-
ind_r = 1;
5936-
59375934
if (word_a >= word_r) goto space_error;
59385935

5936+
ind_r = 1;
59395937
r->frac[0] = 0;
59405938
while (ind_r <= word_a) {
59415939
r->frac[ind_r] = a->frac[ind_r - 1];
59425940
++ind_r;
59435941
}
5944-
59455942
while (ind_r < word_r) r->frac[ind_r++] = 0;
5943+
5944+
ind_c = 0;
59465945
while (ind_c < word_c) c->frac[ind_c++] = 0;
59475946

59485947
/* initial procedure */

test/bigdecimal/test_bigdecimal.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,13 @@ def test_div
973973
assert_raise_with_message(FloatDomainError, "Computation results in '-Infinity'") { BigDecimal("-1") / 0 }
974974
end
975975

976+
def test_div_gh220
977+
x = BigDecimal("1.0")
978+
y = BigDecimal("3672577333.6608990499165058135986328125")
979+
c = BigDecimal("0.272288343892592687909520102748926752911779209181321744700032723729015151607289998e-9")
980+
assert_equal(c, x / y, "[GH-220]")
981+
end
982+
976983
def test_div_precision
977984
bug13754 = '[ruby-core:82107] [Bug #13754]'
978985
a = BigDecimal('101')

0 commit comments

Comments
 (0)