def test_float_signed(self): from klvdata.common import float_to_bytes with self.subTest("Signed -20.0"): self.assertEqual( float_to_bytes(-20.0, _domain=(-(2**15 - 1), 2**15 - 1), _range=(-20, 20)), b'\x80\x01') with self.subTest("Signed 0.0"): self.assertEqual( float_to_bytes(0.0, _domain=(-(2**15 - 1), 2**15 - 1), _range=(-20, 20)), b'\x00\x00') with self.subTest("Signed -0.4315"): self.assertEqual( float_to_bytes(-0.4315, _domain=(-(2**15 - 1), 2**15 - 1), _range=(-20, 20)), b'\xFD\x3D') with self.subTest("Signed 20.0"): self.assertEqual( float_to_bytes(20.0, _domain=(-(2**15 - 1), 2**15 - 1), _range=(-20, 20)), b'\x7F\xFF')
def test_float_unsigned(self): from klvdata.common import float_to_bytes with self.subTest("Unsigned 0.0"): self.assertEqual( float_to_bytes(0.0, _domain=(0, 2**16 - 1), _range=(0, 360)), b'\x00\x00') with self.subTest("Unsigned 159.974"): self.assertEqual( float_to_bytes(159.974, _domain=(0, 2**16 - 1), _range=(0, 360)), b'\x71\xC2') with self.subTest("Unsigned 360.0"): self.assertEqual( float_to_bytes(360.0, _domain=(0, 2**16 - 1), _range=(0, 360)), b'\xFF\xFF')
def __bytes__(self): return float_to_bytes(self.value, self._domain, self._range, self._error)