Skip to content

Commit d6a3b69

Browse files
committed
Use common EXCEPTION_INFINITY check in sqrt
1 parent 7c29f6e commit d6a3b69

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
@@ -1441,15 +1441,13 @@ def test_sqrt_bigdecimal
14411441

14421442
assert_in_delta(BigDecimal("4.0000000000000000000125"), BigDecimal("16.0000000000000000001").sqrt(100), BigDecimal("1e-40"))
14431443

1444-
BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false)
1445-
BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false)
1446-
assert_raise_with_message(FloatDomainError, "sqrt of 'NaN'(Not a Number)") { BigDecimal("NaN").sqrt(1) }
1447-
assert_raise_with_message(FloatDomainError, "sqrt of negative value") { BigDecimal("-Infinity").sqrt(1) }
1444+
assert_raise_with_message(FloatDomainError, "sqrt of 'NaN'(Not a Number)") { BigDecimal::NAN.sqrt(1) }
1445+
assert_raise_with_message(FloatDomainError, "sqrt of negative value") { NEGATIVE_INFINITY.sqrt(1) }
14481446

14491447
assert_equal(0, BigDecimal("0").sqrt(1))
14501448
assert_equal(0, BigDecimal("-0").sqrt(1))
14511449
assert_equal(1, BigDecimal("1").sqrt(1))
1452-
assert_positive_infinite(BigDecimal("Infinity").sqrt(1))
1450+
assert_positive_infinite_calculation { BigDecimal::INFINITY.sqrt(1) }
14531451

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

0 commit comments

Comments
 (0)