Skip to content

Commit 9a45a00

Browse files
authored
Fix incorrect exception when exponent is fractional for Infinity base (#453)
* Handle Infinity base in power’s precision estimator * Apply precision path only when x > 1 && x.finite?
1 parent a6d0db1 commit 9a45a00

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

lib/bigdecimal.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def power(y, prec = 0)
194194
end
195195
ans.mult(1, result_prec)
196196
else
197-
if x > 1
197+
if x > 1 && x.finite?
198198
# To calculate exp(z, prec), z needs prec+max(z.exponent, 0) precision if z > 0.
199199
# Estimate (y*log(x)).exponent
200200
logx_exponent = x < 2 ? (x - 1).exponent : Math.log10(x.exponent).round

test/bigdecimal/test_bigdecimal.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,10 +1851,16 @@ def test_power_of_positive_infinity
18511851
assert_positive_infinite_calculation { BigDecimal::INFINITY ** 1.quo(1) }
18521852
assert_positive_infinite_calculation { BigDecimal::INFINITY ** 1.0 }
18531853
assert_positive_infinite_calculation { BigDecimal::INFINITY ** BigDecimal(1) }
1854+
assert_positive_infinite_calculation { BigDecimal::INFINITY ** 1.quo(2) }
1855+
assert_positive_infinite_calculation { BigDecimal::INFINITY ** 0.5 }
1856+
assert_positive_infinite_calculation { BigDecimal::INFINITY ** BigDecimal(0.5) }
18541857
assert_equal(1, BigDecimal::INFINITY ** 0)
18551858
assert_equal(1, BigDecimal::INFINITY ** 0.quo(1))
18561859
assert_equal(1, BigDecimal::INFINITY ** 0.0)
18571860
assert_equal(1, BigDecimal::INFINITY ** BigDecimal(0))
1861+
assert_positive_zero(BigDecimal::INFINITY ** -1.quo(2))
1862+
assert_positive_zero(BigDecimal::INFINITY ** -0.5)
1863+
assert_positive_zero(BigDecimal::INFINITY ** BigDecimal(-0.5))
18581864
assert_positive_zero(BigDecimal::INFINITY ** -1)
18591865
assert_positive_zero(BigDecimal::INFINITY ** -1.quo(1))
18601866
assert_positive_zero(BigDecimal::INFINITY ** -1.0)

0 commit comments

Comments
 (0)