MCPcopy Create free account
hub / github.com/numpy/numpy / test_floating_exceptions

Method test_floating_exceptions

numpy/core/tests/test_numeric.py:654–715  ·  view source on GitHub ↗
(self, typecode)

Source from the content-addressed store, hash-verified

652 @pytest.mark.skipif(IS_WASM, reason="no wasm fp exception support")
653 @pytest.mark.parametrize("typecode", np.typecodes["AllFloat"])
654 def test_floating_exceptions(self, typecode):
655 if 'bsd' in sys.platform and typecode in 'gG':
656 pytest.skip(reason="Fallback impl for (c)longdouble may not raise "
657 "FPE errors as expected on BSD OSes, "
658 "see gh-24876, gh-23379")
659
660 # Test basic arithmetic function errors
661 with np.errstate(all='raise'):
662 ftype = np.obj2sctype(typecode)
663 if np.dtype(ftype).kind == 'f':
664 # Get some extreme values for the type
665 fi = np.finfo(ftype)
666 ft_tiny = fi._machar.tiny
667 ft_max = fi.max
668 ft_eps = fi.eps
669 underflow = 'underflow'
670 divbyzero = 'divide by zero'
671 else:
672 # 'c', complex, corresponding real dtype
673 rtype = type(ftype(0).real)
674 fi = np.finfo(rtype)
675 ft_tiny = ftype(fi._machar.tiny)
676 ft_max = ftype(fi.max)
677 ft_eps = ftype(fi.eps)
678 # The complex types raise different exceptions
679 underflow = ''
680 divbyzero = ''
681 overflow = 'overflow'
682 invalid = 'invalid'
683
684 # The value of tiny for double double is NaN, so we need to
685 # pass the assert
686 if not np.isnan(ft_tiny):
687 self.assert_raises_fpe(underflow,
688 lambda a, b: a/b, ft_tiny, ft_max)
689 self.assert_raises_fpe(underflow,
690 lambda a, b: a*b, ft_tiny, ft_tiny)
691 self.assert_raises_fpe(overflow,
692 lambda a, b: a*b, ft_max, ftype(2))
693 self.assert_raises_fpe(overflow,
694 lambda a, b: a/b, ft_max, ftype(0.5))
695 self.assert_raises_fpe(overflow,
696 lambda a, b: a+b, ft_max, ft_max*ft_eps)
697 self.assert_raises_fpe(overflow,
698 lambda a, b: a-b, -ft_max, ft_max*ft_eps)
699 self.assert_raises_fpe(overflow,
700 np.power, ftype(2), ftype(2**fi.nexp))
701 self.assert_raises_fpe(divbyzero,
702 lambda a, b: a/b, ftype(1), ftype(0))
703 self.assert_raises_fpe(
704 invalid, lambda a, b: a/b, ftype(np.inf), ftype(np.inf)
705 )
706 self.assert_raises_fpe(invalid,
707 lambda a, b: a/b, ftype(0), ftype(0))
708 self.assert_raises_fpe(
709 invalid, lambda a, b: a-b, ftype(np.inf), ftype(np.inf)
710 )
711 self.assert_raises_fpe(

Callers

nothing calls this directly

Calls 2

assert_raises_fpeMethod · 0.95
dtypeMethod · 0.45

Tested by

no test coverage detected