10000 ENH, TST: Modify half-precision tests to deal with FP HW exceptions · numpy/numpy@7a84442 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7a84442

Browse files
committed
ENH, TST: Modify half-precision tests to deal with FP HW exceptions
1 parent b651406 commit 7a84442

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

numpy/_core/tests/test_half.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ def setup_method(self):
2121
# An array of all possible float16 values
2222
self.all_f16 = np.arange(0x10000, dtype=uint16)
2323
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)
2629

2730
# An array of all non-NaN float16 values, in sorted order
2831
self.nonan_f16 = np.concatenate(
@@ -44,14 +47,19 @@ def test_half_conversions(self):
4447
# value is preserved when converting to/from other floats.
4548

4649
# 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))
5056

5157
# 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))
5563

5664
# Convert float16 to longdouble and back
5765
# This doesn't necessarily preserve the extra NaN bits,

0 commit comments

Comments
 (0)
0