Skip to content

Commit 7b90031

Browse files
authored
Fix x.fix and x.frac affected by prec limit, Stop -x and x.abs round with prec limit (#409)
1 parent 97ff649 commit 7b90031

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

ext/bigdecimal/bigdecimal.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,8 +1702,8 @@ static VALUE
17021702
BigDecimal_neg(VALUE self)
17031703
{
17041704
BDVALUE a = GetBDValueMust(self);
1705-
BDVALUE c = NewZeroWrapLimited(1, a.real->Prec * BASE_FIG);
1706-
VpAsgn(c.real, a.real, -1);
1705+
BDVALUE c = NewZeroWrapNolimit(1, a.real->Prec * BASE_FIG);
1706+
VpAsgn(c.real, a.real, -10);
17071707
RB_GC_GUARD(a.bigdecimal);
17081708
return CheckGetValue(c);
17091709
}
@@ -2176,8 +2176,8 @@ static VALUE
21762176
BigDecimal_abs(VALUE self)
21772177
{
21782178
BDVALUE a = GetBDValueMust(self);
2179-
BDVALUE c = NewZeroWrapLimited(1, a.real->Prec * BASE_FIG);
2180-
VpAsgn(c.real, a.real, 1);
2179+
BDVALUE c = NewZeroWrapNolimit(1, a.real->Prec * BASE_FIG);
2180+
VpAsgn(c.real, a.real, 10);
21812181
VpChangeSign(c.real, 1);
21822182
RB_GC_GUARD(a.bigdecimal);
21832183
return CheckGetValue(c);
@@ -2215,7 +2215,7 @@ static VALUE
22152215
BigDecimal_fix(VALUE self)
22162216
{
22172217
BDVALUE a = GetBDValueMust(self);
2218-
BDVALUE c = NewZeroWrapLimited(1, (a.real->Prec + 1) * BASE_FIG);
2218+
BDVALUE c = NewZeroWrapNolimit(1, (a.real->Prec + 1) * BASE_FIG);
22192219
VpActiveRound(c.real, a.real, VP_ROUND_DOWN, 0); /* 0: round off */
22202220
RB_GC_GUARD(a.bigdecimal);
22212221
return CheckGetValue(c);
@@ -2359,7 +2359,7 @@ static VALUE
23592359
BigDecimal_frac(VALUE self)
23602360
{
23612361
BDVALUE a = GetBDValueMust(self);
2362-
BDVALUE c = NewZeroWrapLimited(1, (a.real->Prec + 1) * BASE_FIG);
2362+
BDVALUE c = NewZeroWrapNolimit(1, (a.real->Prec + 1) * BASE_FIG);
23632363
VpFrac(c.real, a.real);
23642364
RB_GC_GUARD(a.bigdecimal);
23652365
return CheckGetValue(c);

test/bigdecimal/test_bigdecimal.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,6 +2051,31 @@ def test_div_mod_rem_operation_with_limit
20512051
end
20522052
end
20532053

2054+
def test_sign_operator_ignores_limit
2055+
plus_x = BigDecimal('7' * 100)
2056+
minus_x = -plus_x
2057+
BigDecimal.save_limit do
2058+
BigDecimal.limit(3)
2059+
assert_equal(plus_x, +plus_x)
2060+
assert_equal(minus_x, +minus_x)
2061+
assert_equal(minus_x, -plus_x)
2062+
assert_equal(plus_x, -minus_x)
2063+
assert_equal(plus_x, minus_x.abs)
2064+
assert_equal(plus_x, plus_x.abs)
2065+
end
2066+
end
2067+
2068+
def test_fix_frac_ignores_limit
2069+
fix = BigDecimal("#{'4' * 56}")
2070+
frac = BigDecimal("0.#{'7' * 89}")
2071+
x = fix + frac
2072+
BigDecimal.save_limit do
2073+
BigDecimal.limit(3)
2074+
assert_equal(fix, x.fix)
2075+
assert_equal(frac, x.frac)
2076+
end
2077+
end
2078+
20542079
def test_sign
20552080
BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false)
20562081
BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false)

0 commit comments

Comments
 (0)