Skip to content

Commit a8976b6

Browse files
committed
Use common EXCEPTION_INFINITY check in sqrt
1 parent 31172fa commit a8976b6

File tree

2 files changed

+5
-10
lines changed

2 files changed

+5
-10
lines changed

lib/bigdecimal.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,8 @@ def power(y, prec = nil)
183183
#
184184
def sqrt(prec)
185185
Internal.validate_prec(prec, :sqrt, accept_zero: true)
186-
if infinite? == 1
187-
exception_mode = BigDecimal.mode(BigDecimal::EXCEPTION_ALL)
188-
raise FloatDomainError, "Computation results in 'Infinity'" if exception_mode.anybits?(BigDecimal::EXCEPTION_INFINITY)
189-
return INFINITY
190-
end
186+
return Internal.infinity_computation_result if infinite? == 1
187+
191188
raise FloatDomainError, 'sqrt of negative value' if self < 0
192189
raise FloatDomainError, "sqrt of 'NaN'(Not a Number)" if nan?
193190
return self if zero?

test/bigdecimal/test_bigdecimal.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,15 +1395,13 @@ def test_sqrt_bigdecimal
13951395

13961396
assert_in_delta(BigDecimal("4.0000000000000000000125"), BigDecimal("16.0000000000000000001").sqrt(100), BigDecimal("1e-40"))
13971397

1398-
BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false)
1399-
BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false)
1400-
assert_raise_with_message(FloatDomainError, "sqrt of 'NaN'(Not a Number)") { BigDecimal("NaN").sqrt(1) }
1401-
assert_raise_with_message(FloatDomainError, "sqrt of negative value") { BigDecimal("-Infinity").sqrt(1) }
1398+
assert_raise_with_message(FloatDomainError, "sqrt of 'NaN'(Not a Number)") { BigDecimal::NAN.sqrt(1) }
1399+
assert_raise_with_message(FloatDomainError, "sqrt of negative value") { NEGATIVE_INFINITY.sqrt(1) }
14021400

14031401
assert_equal(0, BigDecimal("0").sqrt(1))
14041402
assert_equal(0, BigDecimal("-0").sqrt(1))
14051403
assert_equal(1, BigDecimal("1").sqrt(1))
1406-
assert_positive_infinite(BigDecimal("Infinity").sqrt(1))
1404+
assert_positive_infinite_calculation { BigDecimal::INFINITY.sqrt(1) }
14071405

14081406
# Out of float range
14091407
assert_equal(BigDecimal('12e1024'), BigDecimal('144e2048').sqrt(10))

0 commit comments

Comments
 (0)