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

bigdecimal.c: Fix special cases of BigDecimal#** #227

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
28 changes: 18 additions & 10 deletions ext/bigdecimal/bigdecimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -2769,7 +2769,14 @@ bigdecimal_power_by_bigdecimal(Real const* x, Real const* exp, ssize_t const n)
VALUE log_x, multiplied, y;
volatile VALUE obj = exp->obj;

if (VpIsZero(exp)) {
if (VpIsNaN(exp)) {
Real *nan = VpCreateRbObject(n, "0", true);
RB_GC_GUARD(nan->obj);
VpSetNaN(nan);
return VpCheckGetValue(nan);
}

if (VpIsZero(exp) || VpIsPosOne(x)) {
return VpCheckGetValue(VpCreateRbObject(n, "1", true));
}

Expand Down Expand Up @@ -2858,15 +2865,16 @@ BigDecimal_power(int argc, VALUE*argv, VALUE self)

case T_DATA:
if (is_kind_of_BigDecimal(vexp)) {
VALUE zero = INT2FIX(0);
VALUE rounded = BigDecimal_round(1, &zero, vexp);
if (RTEST(BigDecimal_eq(vexp, rounded))) {
vexp = BigDecimal_to_i(vexp);
goto retry;
}
if (NIL_P(prec)) {
GUARD_OBJ(y, GetVpValue(vexp, 1));
n += y->Prec*VpBaseFig();
if (BigDecimal_IsFinite(vexp)) {
VALUE rounded = BigDecimal_round(0, NULL, vexp);
if (RTEST(BigDecimal_eq(vexp, rounded))) {
vexp = BigDecimal_to_i(vexp);
goto retry;
}
if (NIL_P(prec)) {
GUARD_OBJ(y, GetVpValue(vexp, 1));
n += y->Prec*VpBaseFig();
}
}
exp = DATA_PTR(vexp);
break;
Expand Down
1 change: 1 addition & 0 deletions ext/bigdecimal/bigdecimal.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ VP_EXPORT Real *VpOne(void);
#define VpSetInf(a,s) (void)(((s)>0)?VpSetPosInf(a):VpSetNegInf(a))
#define VpHasVal(a) (a->frac[0])
#define VpIsOne(a) ((a->Prec==1)&&(a->frac[0]==1)&&(a->exponent==1))
#define VpIsPosOne(a) (((a)->sign>0)&&VpIsOne(a))
#define VpExponent(a) (a->exponent)
#ifdef BIGDECIMAL_DEBUG
int VpVarCheck(Real * v);
Expand Down
33 changes: 30 additions & 3 deletions test/bigdecimal/test_bigdecimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ def test_BigDecimal_with_float
end
BigDecimal.save_exception_mode do
BigDecimal.mode(BigDecimal::EXCEPTION_INFINITY, false)
p ahi: Float::INFINITY, uhi: Float::INFINITY.infinite?
mrkn marked this conversation as resolved.
Show resolved Hide resolved
assert_positive_infinite(BigDecimal(Float::INFINITY))
assert_same(BigDecimal(Float::INFINITY), BigDecimal(Float::INFINITY))
assert_negative_infinite(BigDecimal(-Float::INFINITY))
Expand Down Expand Up @@ -1492,9 +1493,35 @@ def test_power_with_Bignum
end
end

def test_power_with_BigDecimal
assert_nothing_raised do
assert_in_delta(3 ** 3, BigDecimal(3) ** BigDecimal(3))
data(
"BigDecimal(3) ** BigDecimal(3) -> BigDecimal(3**3)" => [BigDecimal(3), BigDecimal(3), BigDecimal(3**3)],
"BigDecimal(10) ** Inf -> Inf" => [BigDecimal(10), BigDecimal::INFINITY, :pos_inf],
"BigDecimal(10) ** NaN -> NaN" => [BigDecimal(10), BigDecimal::NAN, :nan],
"Inf ** BigDecimal(0) -> BigDecimal(1)" => [BigDecimal::INFINITY, BigDecimal(0), BigDecimal(1)],
"-Inf ** BigDecimal(0) -> BigDecimal(-1)" => [-BigDecimal::INFINITY, BigDecimal(0), BigDecimal(1)],
"BigDecimal(1) ** Inf -> 1" => [BigDecimal(1), BigDecimal::INFINITY, BigDecimal(1)],
"BigDecimal(1) ** -Inf -> 1" => [BigDecimal(1), -BigDecimal::INFINITY, BigDecimal(1)],
"BigDecimal(0) ** Inf -> 0" => [BigDecimal(0), BigDecimal::INFINITY, BigDecimal(0)],
"BigDecimal(0) ** -Inf -> Inf" => [BigDecimal(0), -BigDecimal::INFINITY, :pos_inf],
"BigDecimal(-1) ** Inf -> Math::DomainError" => [BigDecimal(-1), BigDecimal::INFINITY, :math_domain_error],
"BigDecimal(-1) ** -Inf -> Math::DomainError" => [BigDecimal(-1), -BigDecimal::INFINITY, :math_domain_error]
)
def test_power_with_BigDecimal(data)
BigDecimal.save_exception_mode do
BigDecimal.mode(BigDecimal::EXCEPTION_ALL, false)
x, y, res = *data
case res
when :pos_inf
assert_nothing_raised { assert_positive_infinite(x ** y) }
when :neg_inf
assert_nothing_raised { assert_negative_infinite(x ** y) }
when :nan
assert_nothing_raised { assert_nan(x ** y) }
when :math_domain_error
assert_raise(Math::DomainError) { x ** y }
else
assert_nothing_raised { assert_in_delta(res, x ** y) }
end
end
end

Expand Down