Skip to content

Commit d35e82b

Browse files
authored
Fix fast-path of frac and _decimal_shift affected by BigDecimal.limit (#447)
1 parent 23dc06b commit d35e82b

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

ext/bigdecimal/bigdecimal.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2507,7 +2507,7 @@ BigDecimal_decimal_shift(VALUE self, VALUE v)
25072507
prec = a.real->Prec + shiftDown;
25082508
c = NewZeroWrap(1, prec * BASE_FIG);
25092509
if (shift == 0) {
2510-
VpAsgn(c.real, a.real, 1);
2510+
VpAsgn(c.real, a.real, 10);
25112511
} else if (shiftDown) {
25122512
DECDIG carry = 0;
25132513
exponentShift++;
@@ -6166,7 +6166,7 @@ VpFrac(Real *y, Real *x)
61666166
size_t my, ind_y, ind_x;
61676167

61686168
if (!VpHasVal(x)) {
6169-
VpAsgn(y, x, 1);
6169+
VpAsgn(y, x, 10);
61706170
goto Exit;
61716171
}
61726172

@@ -6175,7 +6175,7 @@ VpFrac(Real *y, Real *x)
61756175
goto Exit;
61766176
}
61776177
else if (x->exponent <= 0) {
6178-
VpAsgn(y, x, 1);
6178+
VpAsgn(y, x, 10);
61796179
goto Exit;
61806180
}
61816181

test/bigdecimal/test_bigdecimal.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,6 +1440,11 @@ def test_internal_use_decimal_shift
14401440
assert_equal(num / 10**shift, num._decimal_shift(-shift))
14411441
end
14421442
end
1443+
BigDecimal.save_limit do
1444+
BigDecimal.limit(3)
1445+
assert_equal(BigDecimal('123456789e4'), BigDecimal('123456789')._decimal_shift(4))
1446+
assert_equal(BigDecimal('123456789e9'), BigDecimal('123456789')._decimal_shift(9))
1447+
end
14431448
end
14441449

14451450
def test_fix
@@ -1454,6 +1459,11 @@ def test_frac
14541459
assert_equal(0.1, BigDecimal("0.1").frac)
14551460
BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false)
14561461
assert_nan(BigDecimal("NaN").frac)
1462+
BigDecimal.save_limit do
1463+
BigDecimal.limit(3)
1464+
assert_equal(BigDecimal('0.456789'), BigDecimal('123.456789').frac)
1465+
assert_equal(BigDecimal('0.0123456789'), BigDecimal('0.0123456789').frac)
1466+
end
14571467
end
14581468

14591469
def test_round

0 commit comments

Comments
 (0)