MCPcopy Index your code
hub / github.com/numpy/numpy / test_half_correctness

Method test_half_correctness

numpy/_core/tests/test_half.py:278–309  ·  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

276 assert_equal(b, rounded)
277
278 def test_half_correctness(self):
279 """Take every finite float16, and check the casting functions with
280 a manual conversion."""
281 finite_f16, finite_f32, finite_f64 = self._create_arrays_finite()
282
283 # Create an array of all finite float16s
284 a_bits = finite_f16.view(dtype=uint16)
285
286 # Convert to 64-bit float manually
287 a_sgn = (-1.0)**((a_bits & 0x8000) >> 15)
288 a_exp = np.array((a_bits & 0x7c00) >> 10, dtype=np.int32) - 15
289 a_man = (a_bits & 0x03ff) * 2.0**(-10)
290 # Implicit bit of normalized floats
291 a_man[a_exp != -15] += 1
292 # Denormalized exponent is -14
293 a_exp[a_exp == -15] = -14
294
295 a_manual = a_sgn * a_man * 2.0**a_exp
296
297 a32_fail = np.nonzero(finite_f32 != a_manual)[0]
298 if len(a32_fail) != 0:
299 bad_index = a32_fail[0]
300 assert_equal(finite_f32, a_manual,
301 f"First non-equal is half value 0x{a_bits[bad_index]:x} -> "
302 f"{finite_f32[bad_index]:g} != {a_manual[bad_index]:g}")
303
304 a64_fail = np.nonzero(finite_f64 != a_manual)[0]
305 if len(a64_fail) != 0:
306 bad_index = a64_fail[0]
307 assert_equal(finite_f64, a_manual,
308 f"First non-equal is half value 0x{a_bits[bad_index]:x} -> "
309 f"{finite_f64[bad_index]:g} != {a_manual[bad_index]:g}")
310
311 def test_half_ordering(self):
312 """Make sure comparisons are working right"""

Callers

nothing calls this directly

Calls 4

_create_arrays_finiteMethod · 0.95
assert_equalFunction · 0.90
nonzeroMethod · 0.80
viewMethod · 0.45

Tested by

no test coverage detected