Skip to content
Closed
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
19 changes: 11 additions & 8 deletions ext/bigdecimal/bigdecimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -7655,7 +7655,7 @@ VpFrac(Real *y, Real *x)
VP_EXPORT int
VpPowerByInt(Real *y, Real *x, SIGNED_VALUE n)
{
size_t s, ss;
size_t s;
ssize_t sign;
Real *w1 = NULL;
Real *w2 = NULL;
Expand Down Expand Up @@ -7721,17 +7721,20 @@ VpPowerByInt(Real *y, Real *x, SIGNED_VALUE n)
/* calculation start */

VpAsgn(y, x, 1);
VpAsgn(w1, x, 1);
--n;
while (n > 0) {
VpAsgn(w1, x, 1);
s = 1;
while (ss = s, (s += s) <= (size_t)n) {
s = 1;

while (s <= (size_t)n) {
if (s != 1) {
VpMult(w2, w1, w1);
VpAsgn(w1, w2, 1);
}
n -= (SIGNED_VALUE)ss;
VpMult(w2, y, w1);
VpAsgn(y, w2, 1);
if (s & n) {
VpMult(w2, y, w1);
VpAsgn(y, w2, 1);
}
s <<= 1;
}
if (sign < 0) {
VpDivd(w1, w2, VpConstOne, y);
Expand Down
3 changes: 1 addition & 2 deletions test/bigdecimal/test_bigdecimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1660,8 +1660,7 @@ def test_limit
BigDecimal.save_limit do
BigDecimal.limit(1)
x = BigDecimal("3")
assert_equal(90, x ** 4) # OK? must it be 80?
# 3 * 3 * 3 * 3 = 10 * 3 * 3 = 30 * 3 = 90 ???

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before: 3**4(3 * (3**2)) * 3(3 * 9) * 3round(27) * 390
After: 3**4(3 * 3) * (3**2)9 * 9round(81)80

Although this test case seems to be improved, just a coincidence.
This pull request is not a bug fix. Large error still exist.

BigDecimal.limit 1
(BigDecimal('2')**100).to_i / 2**100 #=> 1577

assert_equal(80, x ** 4)
assert_raise(ArgumentError) { BigDecimal.limit(-1) }

bug7458 = '[ruby-core:50269] [#7458]'
Expand Down