def test_nans(self): Q = float_pack(float('nan'), 8) y = float_unpack(Q, 8) assert repr(y) == 'nan' L = float_pack(float('nan'), 4) z = float_unpack(L, 4) assert repr(z) == 'nan'
def check_float(self, x): # check roundtrip Q = float_pack(x, 8) y = float_unpack(Q, 8) assert repr(x) == repr(y) # check that packing agrees with the struct module struct_pack8 = struct.unpack('<Q', struct.pack('<d', x))[0] float_pack8 = float_pack(x, 8) assert struct_pack8 == float_pack8 # check that packing agrees with the struct module try: struct_pack4 = struct.unpack('<L', struct.pack('<f', x))[0] except OverflowError: struct_pack4 = "overflow" try: float_pack4 = float_pack(x, 4) except OverflowError: float_pack4 = "overflow" assert struct_pack4 == float_pack4 # if we didn't overflow, try round-tripping the binary32 value if float_pack4 != "overflow": roundtrip = float_pack(float_unpack(float_pack4, 4), 4) assert float_pack4 == roundtrip