@@ -637,62 +637,65 @@ def assert_op_raises_fpe(self, fpeerr, flop, sc1, sc2):
637637 self .assert_raises_fpe (fpeerr , flop , sc1 , sc2 [()])
638638 self .assert_raises_fpe (fpeerr , flop , sc1 [()], sc2 [()])
639639
640- def test_floating_exceptions (self ):
640+ # Test for all real and complex float types
641+ @pytest .mark .parametrize ("typecode" , np .typecodes ["AllFloat" ])
642+ def test_floating_exceptions (self , typecode ):
641643 # Test basic arithmetic function errors
642644 with np .errstate (all = 'raise' ):
643- # Test for all real and complex float types
644- for typecode in np .typecodes ['AllFloat' ]:
645- ftype = np .obj2sctype (typecode )
646- if np .dtype (ftype ).kind == 'f' :
647- # Get some extreme values for the type
648- fi = np .finfo (ftype )
649- ft_tiny = fi .machar .tiny
650- ft_max = fi .max
651- ft_eps = fi .eps
652- underflow = 'underflow'
653- divbyzero = 'divide by zero'
654- else :
655- # 'c', complex, corresponding real dtype
656- rtype = type (ftype (0 ).real )
657- fi = np .finfo (rtype )
658- ft_tiny = ftype (fi .machar .tiny )
659- ft_max = ftype (fi .max )
660- ft_eps = ftype (fi .eps )
661- # The complex types raise different exceptions
662- underflow = ''
663- divbyzero = ''
664- overflow = 'overflow'
665- invalid = 'invalid'
666-
667- # The value of tiny for double double is NaN, so we need to
668- # pass the assert
669- if not np .isnan (ft_tiny ):
670- self .assert_raises_fpe (underflow ,
671- lambda a , b : a / b , ft_tiny , ft_max )
672- self .assert_raises_fpe (underflow ,
673- lambda a , b : a * b , ft_tiny , ft_tiny )
674- self .assert_raises_fpe (overflow ,
675- lambda a , b : a * b , ft_max , ftype (2 ))
676- self .assert_raises_fpe (overflow ,
677- lambda a , b : a / b , ft_max , ftype (0.5 ))
678- self .assert_raises_fpe (overflow ,
679- lambda a , b : a + b , ft_max , ft_max * ft_eps )
680- self .assert_raises_fpe (overflow ,
681- lambda a , b : a - b , - ft_max , ft_max * ft_eps )
682- self .assert_raises_fpe (overflow ,
683- np .power , ftype (2 ), ftype (2 ** fi .nexp ))
684- self .assert_raises_fpe (divbyzero ,
685- lambda a , b : a / b , ftype (1 ), ftype (0 ))
686- self .assert_raises_fpe (invalid ,
687- lambda a , b : a / b , ftype (np .inf ), ftype (np .inf ))
688- self .assert_raises_fpe (invalid ,
689- lambda a , b : a / b , ftype (0 ), ftype (0 ))
690- self .assert_raises_fpe (invalid ,
691- lambda a , b : a - b , ftype (np .inf ), ftype (np .inf ))
692- self .assert_raises_fpe (invalid ,
693- lambda a , b : a + b , ftype (np .inf ), ftype (- np .inf ))
694- self .assert_raises_fpe (invalid ,
695- lambda a , b : a * b , ftype (0 ), ftype (np .inf ))
645+ ftype = np .obj2sctype (typecode )
646+ if np .dtype (ftype ).kind == 'f' :
647+ # Get some extreme values for the type
648+ fi = np .finfo (ftype )
649+ ft_tiny = fi .machar .tiny
650+ ft_max = fi .max
651+ ft_eps = fi .eps
652+ underflow = 'underflow'
653+ divbyzero = 'divide by zero'
654+ else :
655+ # 'c', complex, corresponding real dtype
656+ rtype = type (ftype (0 ).real )
657+ fi = np .finfo (rtype )
658+ ft_tiny = ftype (fi .machar .tiny )
659+ ft_max = ftype (fi .max )
660+ ft_eps = ftype (fi .eps )
661+ # The complex types raise different exceptions
662+ underflow = ''
663+ divbyzero = ''
664+ overflow = 'overflow'
665+ invalid = 'invalid'
666+
667+ # The value of tiny for double double is NaN, so we need to
668+ # pass the assert
669+ if not np .isnan (ft_tiny ):
670+ self .assert_raises_fpe (underflow ,
671+ lambda a , b : a / b , ft_tiny , ft_max )
672+ self .assert_raises_fpe (underflow ,
673+ lambda a , b : a * b , ft_tiny , ft_tiny )
674+ self .assert_raises_fpe (overflow ,
675+ lambda a , b : a * b , ft_max , ftype (2 ))
676+ self .assert_raises_fpe (overflow ,
677+ lambda a , b : a / b , ft_max , ftype (0.5 ))
678+ self .assert_raises_fpe (overflow ,
679+ lambda a , b : a + b , ft_max , ft_max * ft_eps )
680+ self .assert_raises_fpe (overflow ,
681+ lambda a , b : a - b , - ft_max , ft_max * ft_eps )
682+ self .assert_raises_fpe (overflow ,
683+ np .power , ftype (2 ), ftype (2 ** fi .nexp ))
684+ self .assert_raises_fpe (divbyzero ,
685+ lambda a , b : a / b , ftype (1 ), ftype (0 ))
686+ self .assert_raises_fpe (
687+ invalid , lambda a , b : a / b , ftype (np .inf ), ftype (np .inf )
688+ )
689+ self .assert_raises_fpe (invalid ,
690+ lambda a , b : a / b , ftype (0 ), ftype (0 ))
691+ self .assert_raises_fpe (
692+ invalid , lambda a , b : a - b , ftype (np .inf ), ftype (np .inf )
693+ )
694+ self .assert_raises_fpe (
695+ invalid , lambda a , b : a + b , ftype (np .inf ), ftype (- np .inf )
696+ )
697+ self .assert_raises_fpe (invalid ,
698+ lambda a , b : a * b , ftype (0 ), ftype (np .inf ))
696699
697700 def test_warnings (self ):
698701 # test warning code path
0 commit comments