@@ -21,8 +21,11 @@ def setup_method(self):
21
21
# An array of all possible float16 values
22
22
self .all_f16 = np .arange (0x10000 , dtype = uint16 )
23
23
self .all_f16 .dtype = float16
24
- self .all_f32 = np .array (self .all_f16 , dtype = float32 )
25
- self .all_f64 = np .array (self .all_f16 , dtype = float64 )
24
+
25
+ # NaN value can cause an invalid FP exception if HW is been used
26
+ with np .errstate (invalid = 'ignore' ):
27
+ self .all_f32 = np .array (self .all_f16 , dtype = float32 )
28
+ self .all_f64 = np .array (self .all_f16 , dtype = float64 )
26
29
27
30
# An array of all non-NaN float16 values, in sorted order
28
31
self .nonan_f16 = np .concatenate (
@@ -44,14 +47,19 @@ def test_half_conversions(self):
44
47
# value is preserved when converting to/from other floats.
45
48
46
49
# Convert from float32 back to float16
47
- b = np .array (self .all_f32 , dtype = float16 )
48
- assert_equal (self .all_f16 .view (dtype = uint16 ),
49
- b .view (dtype = uint16 ))
50
+ with np .errstate (invalid = 'ignore' ):
51
+ b = np .array (self .all_f32 , dtype = float16 )
52
+ # avoid testing NaNs due to differ bits wither Q/SNaNs
53
+ b_nn = b == b
54
+ assert_equal (self .all_f16 [b_nn ].view (dtype = uint16 ),
55
+ b [b_nn ].view (dtype = uint16 ))
50
56
51
57
# Convert from float64 back to float16
52
- b = np .array (self .all_f64 , dtype = float16 )
53
- assert_equal (self .all_f16 .view (dtype = uint16 ),
54
- b .view (dtype = uint16 ))
58
+ with np .errstate (invalid = 'ignore' ):
59
+ b = np .array (self .all_f64 , dtype = float16 )
60
+ b_nn = b == b
61
+ assert_equal (self .all_f16 [b_nn ].view (dtype = uint16 ),
62
+ b [b_nn ].view (dtype = uint16 ))
55
63
56
64
# Convert float16 to longdouble and back
57
65
# This doesn't necessarily preserve the extra NaN bits,
0 commit comments