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

Method test_half_correctness

numpy/core/tests/test_half.py:255–289  ·  view source on GitHub ↗

Take every finite float16, and check the casting functions with a manual conversion.

(self)

Source from the content-addressed store, hash-verified

253 assert_equal(b, rounded)
254
255 def test_half_correctness(self):
256 """Take every finite float16, and check the casting functions with
257 a manual conversion."""
258
259 # Create an array of all finite float16s
260 a_bits = self.finite_f16.view(dtype=uint16)
261
262 # Convert to 64-bit float manually
263 a_sgn = (-1.0)**((a_bits & 0x8000) >> 15)
264 a_exp = np.array((a_bits & 0x7c00) >> 10, dtype=np.int32) - 15
265 a_man = (a_bits & 0x03ff) * 2.0**(-10)
266 # Implicit bit of normalized floats
267 a_man[a_exp != -15] += 1
268 # Denormalized exponent is -14
269 a_exp[a_exp == -15] = -14
270
271 a_manual = a_sgn * a_man * 2.0**a_exp
272
273 a32_fail = np.nonzero(self.finite_f32 != a_manual)[0]
274 if len(a32_fail) != 0:
275 bad_index = a32_fail[0]
276 assert_equal(self.finite_f32, a_manual,
277 "First non-equal is half value 0x%x -> %g != %g" %
278 (a_bits[bad_index],
279 self.finite_f32[bad_index],
280 a_manual[bad_index]))
281
282 a64_fail = np.nonzero(self.finite_f64 != a_manual)[0]
283 if len(a64_fail) != 0:
284 bad_index = a64_fail[0]
285 assert_equal(self.finite_f64, a_manual,
286 "First non-equal is half value 0x%x -> %g != %g" %
287 (a_bits[bad_index],
288 self.finite_f64[bad_index],
289 a_manual[bad_index]))
290
291 def test_half_ordering(self):
292 """Make sure comparisons are working right"""

Callers

nothing calls this directly

Calls 3

assert_equalFunction · 0.90
nonzeroMethod · 0.80
viewMethod · 0.45

Tested by

no test coverage detected