Skip to content

Commit ddf7d76

Browse files
committed
Add underflow check to BigMath.erfc
1 parent b468843 commit ddf7d76

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

lib/bigdecimal/math.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ def erfc(x, prec)
637637
return BigDecimal::Internal.nan_computation_result if x.nan?
638638
return BigDecimal(1 - x.infinite?) if x.infinite?
639639
return BigDecimal(1).sub(erf(x, prec + BigDecimal::Internal::EXTRA_PREC), prec) if x < 0.5
640-
return BigDecimal(0) if x > 5000000000 # erfc(5000000000) < 1e-10000000000000000000 (underflow)
640+
return BigDecimal::Internal.underflow_computation_result if x > 5000000000 # erfc(5000000000) < 1e-10000000000000000000 (underflow)
641641

642642
if x >= 8
643643
y = _erfc_asymptotic(x, prec)

test/bigdecimal/helper.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,16 @@ def assert_negative_infinite_calculation(&block)
8383
assert_infinite_calculation(positive: false, &block)
8484
end
8585

86-
def assert_underflow_calculation
86+
def assert_underflow_calculation(accept_overflow: false)
8787
BigDecimal.save_exception_mode do
8888
BigDecimal.mode(BigDecimal::EXCEPTION_UNDERFLOW, false)
89+
BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false)
8990
assert_equal(BigDecimal(0), yield)
9091
BigDecimal.mode(BigDecimal::EXCEPTION_UNDERFLOW, true)
91-
assert_raise_with_message(FloatDomainError, /underflow/i) { yield }
92+
BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, true)
93+
# Accept internal overflow (e.g. overflow calculating denominator part)
94+
pattern = accept_overflow ? /underflow|overflow/i : /underflow/i
95+
assert_raise_with_message(FloatDomainError, pattern) { yield }
9296
end
9397
end
9498

test/bigdecimal/test_bigmath.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,8 @@ def test_erfc
521521
end
522522
assert_equal(0, BigMath.erfc(PINF, N))
523523
assert_equal(2, BigMath.erfc(MINF, N))
524-
assert_equal(0, BigMath.erfc(BigDecimal('1e400'), 10))
524+
assert_underflow_calculation(accept_overflow: true) { BigMath.erfc(4999999999, 10) }
525+
assert_underflow_calculation { BigMath.erfc(BigDecimal('1e400'), 10) }
525526
assert_equal(2, BigMath.erfc(BigDecimal('-1e400'), 10))
526527
assert_equal(1, BigMath.erfc(BigDecimal('1e-400'), N))
527528

0 commit comments

Comments
 (0)