MCPcopy Index your code
hub / github.com/numpy/numpy / test_floating_exceptions

Method test_floating_exceptions

numpy/_core/tests/test_numeric.py:983–1047  ·  view source on GitHub ↗
(self, typecode)

Source from the content-addressed store, hash-verified

981 @pytest.mark.skipif(IS_WASM, reason="no wasm fp exception support")
982 @pytest.mark.parametrize("typecode", np.typecodes["AllFloat"])
983 def test_floating_exceptions(self, typecode):
984 if 'bsd' in sys.platform and typecode in 'gG':
985 pytest.skip(reason="Fallback impl for (c)longdouble may not raise "
986 "FPE errors as expected on BSD OSes, "
987 "see gh-24876, gh-23379")
988
989 # Test basic arithmetic function errors
990 with np.errstate(all='raise'):
991 ftype = obj2sctype(typecode)
992 if np.dtype(ftype).kind == 'f':
993 # Get some extreme values for the type
994 fi = np.finfo(ftype)
995 ft_tiny = fi.tiny
996 ft_max = fi.max
997 ft_eps = fi.eps
998 underflow = 'underflow'
999 divbyzero = 'divide by zero'
1000 else:
1001 # 'c', complex, corresponding real dtype
1002 rtype = type(ftype(0).real)
1003 fi = np.finfo(rtype)
1004 ft_tiny = ftype(fi.tiny)
1005 ft_max = ftype(fi.max)
1006 ft_eps = ftype(fi.eps)
1007 # The complex types raise different exceptions
1008 underflow = ''
1009 divbyzero = ''
1010 overflow = 'overflow'
1011 invalid = 'invalid'
1012
1013 # The value of tiny for double double is NaN, so we need to
1014 # pass the assert
1015 if not np.isnan(ft_tiny):
1016 self.assert_raises_fpe(underflow,
1017 lambda a, b: a / b, ft_tiny, ft_max)
1018 self.assert_raises_fpe(underflow,
1019 lambda a, b: a * b, ft_tiny, ft_tiny)
1020 self.assert_raises_fpe(overflow,
1021 lambda a, b: a * b, ft_max, ftype(2))
1022 self.assert_raises_fpe(overflow,
1023 lambda a, b: a / b, ft_max, ftype(0.5))
1024 self.assert_raises_fpe(overflow,
1025 lambda a, b: a + b, ft_max, ft_max * ft_eps)
1026 self.assert_raises_fpe(overflow,
1027 lambda a, b: a - b, -ft_max, ft_max * ft_eps)
1028 # On AIX, pow() with double does not raise the overflow exception,
1029 # it returns inf. Long double is the same as double.
1030 if sys.platform != 'aix' or typecode not in 'dDgG':
1031 self.assert_raises_fpe(overflow,
1032 np.power, ftype(2), ftype(2**fi.nexp))
1033 self.assert_raises_fpe(divbyzero,
1034 lambda a, b: a / b, ftype(1), ftype(0))
1035 self.assert_raises_fpe(
1036 invalid, lambda a, b: a / b, ftype(np.inf), ftype(np.inf)
1037 )
1038 self.assert_raises_fpe(invalid,
1039 lambda a, b: a / b, ftype(0), ftype(0))
1040 self.assert_raises_fpe(

Callers

nothing calls this directly

Calls 3

assert_raises_fpeMethod · 0.95
obj2sctypeFunction · 0.90
dtypeMethod · 0.45

Tested by

no test coverage detected