def test_float2bin(self): """ Should convert a float to binary and back """ # should take single values and return len=1 array for f0 in [9000.12345, 0.012345]: b0, pow0 = float2bin(f0, length=32) self.assertTrue(isinstance(b0[0], str)) self.assertEqual(len(b0), 1) self.assertEqual(len(pow0), 1) self.assertEqual(len(b0[0]), 32) # should take a single string instance f1 = bin2float(b0, pow0) self.assertEqual(len(f1), 1) self.assertAlmostEqual(f0, f1[0], 3)
def test_float2bin_array(self): """ Should convert arrays to binary and back """ f0 = np.asarray([999.1234, 9.1234, 0.01234, -99.123, 1.e-33]) b, s = float2bin(f0) self.assertEqual(len(b), len(f0)) self.assertEqual(len(s), len(f0)) for _b in b: self.assertTrue(isinstance(_b, str)) f1 = bin2float(b, s) for _f0, _f1 in zip(f0, f1): self.assertAlmostEqual(_f0, _f1, 4)