Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #220 #221

Merged
merged 2 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions ext/bigdecimal/bigdecimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 */
Expand Down
9 changes: 8 additions & 1 deletion test/bigdecimal/test_bigdecimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,14 @@ def test_div
assert_raise_with_message(FloatDomainError, "Computation results in '-Infinity'") { BigDecimal("-1") / 0 }
end

def test_dev_precision
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')
b = BigDecimal('0.9163472602589686')
Expand Down