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

Method test_half_conversions

numpy/_core/tests/test_half.py:49–84  ·  view source on GitHub ↗

Checks that all 16-bit values survive conversion to/from 32-bit and 64-bit float

(self)

Source from the content-addressed store, hash-verified

47 return finite_f16, finite_f32, finite_f64
48
49 def test_half_conversions(self):
50 """Checks that all 16-bit values survive conversion
51 to/from 32-bit and 64-bit float"""
52 # Because the underlying routines preserve the NaN bits, every
53 # value is preserved when converting to/from other floats.
54 all_f16, all_f32, all_f64 = self._create_arrays_all()
55 nonan_f16, _, _ = self._create_arrays_nonan()
56
57 # Convert from float32 back to float16
58 with np.errstate(invalid='ignore'):
59 b = np.array(all_f32, dtype=float16)
60 # avoid testing NaNs due to differing bit patterns in Q/S NaNs
61 b_nn = b == b
62 assert_equal(all_f16[b_nn].view(dtype=uint16),
63 b[b_nn].view(dtype=uint16))
64
65 # Convert from float64 back to float16
66 with np.errstate(invalid='ignore'):
67 b = np.array(all_f64, dtype=float16)
68 b_nn = b == b
69 assert_equal(all_f16[b_nn].view(dtype=uint16),
70 b[b_nn].view(dtype=uint16))
71
72 # Convert float16 to longdouble and back
73 # This doesn't necessarily preserve the extra NaN bits,
74 # so exclude NaNs.
75 a_ld = np.array(nonan_f16, dtype=np.longdouble)
76 b = np.array(a_ld, dtype=float16)
77 assert_equal(nonan_f16.view(dtype=uint16),
78 b.view(dtype=uint16))
79
80 # Check the range for which all integers can be represented
81 i_int = np.arange(-2048, 2049)
82 i_f16 = np.array(i_int, dtype=float16)
83 j = np.array(i_f16, dtype=int)
84 assert_equal(i_int, j)
85
86 @pytest.mark.parametrize("string_dt", ["S", "U"])
87 def test_half_conversion_to_string(self, string_dt):

Callers

nothing calls this directly

Calls 4

_create_arrays_allMethod · 0.95
_create_arrays_nonanMethod · 0.95
assert_equalFunction · 0.90
viewMethod · 0.45

Tested by

no test coverage detected