Skip to content

Commit

Permalink
* ext/bigdecimal/bigdecimal.c (BigDecimal_sqrt): Fix the precision of
Browse files Browse the repository at this point in the history
  the result BigDecimal of sqrt.
  [Bug #5266] [ruby-dev:44450]

* test/bigdecimal/test_bigdecimal.rb: add tests for the above changes.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43764 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
mrkn committed Dec 25, 2015
1 parent 73ba45c commit 1f5c46d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
10 changes: 5 additions & 5 deletions ext/bigdecimal/bigdecimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -1591,10 +1591,10 @@ BigDecimal_sqrt(VALUE self, VALUE nFig)
size_t mx, n;

GUARD_OBJ(a, GetVpValue(self, 1));
mx = a->Prec *(VpBaseFig() + 1);
mx = a->Prec * (VpBaseFig() + 1);

n = GetPositiveInt(nFig) + VpDblFig() + 1;
if(mx <= n) mx = n;
n = GetPositiveInt(nFig) + VpDblFig() + BASE_FIG;
if (mx <= n) mx = n;
GUARD_OBJ(c, VpCreateRbObject(mx, "0"));
VpSqrt(c, a);
return ToValue(c);
Expand Down Expand Up @@ -5654,6 +5654,7 @@ VpSqrt(Real *y, Real *x)

n = (SIGNED_VALUE)y->MaxPrec;
if (x->MaxPrec > (size_t)n) n = (ssize_t)x->MaxPrec;

/* allocate temporally variables */
f = VpAlloc(y->MaxPrec * (BASE_FIG + 2), "#1");
r = VpAlloc((n + n) * (BASE_FIG + 2), "#1");
Expand Down Expand Up @@ -5691,8 +5692,7 @@ VpSqrt(Real *y, Real *x)
if (VpIsZero(f)) goto converge;
VpAddSub(r, f, y, 1); /* r = y + f */
VpAsgn(y, r, 1); /* y = r */
if (f->exponent <= prec) goto converge;
} while(++nr < n);
} while (++nr < n);

#ifdef BIGDECIMAL_DEBUG
if (gfDebug) {
Expand Down
14 changes: 14 additions & 0 deletions test/bigdecimal/test_bigdecimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,20 @@ def test_sqrt_bigdecimal
assert_equal(1, BigDecimal.new("1").sqrt(1))
end

def test_sqrt_5266
x = BigDecimal('2' + '0'*100)
assert_equal('0.14142135623730950488016887242096980785696718753769480731',
x.sqrt(56).to_s(56).split(' ')[0])
assert_equal('0.1414213562373095048801688724209698078569671875376948073',
x.sqrt(55).to_s(55).split(' ')[0])

x = BigDecimal('2' + '0'*200)
assert_equal('0.14142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462',
x.sqrt(110).to_s(110).split(' ')[0])
assert_equal('0.1414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641572735013846',
x.sqrt(109).to_s(109).split(' ')[0])
end

def test_fix
x = BigDecimal.new("1.1")
assert_equal(1, x.fix)
Expand Down

0 comments on commit 1f5c46d

Please sign in to comment.