示例#1
0
    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')
示例#2
0
    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')
示例#3
0
 def __bytes__(self):
     return float_to_bytes(self.value, self._domain, self._range, self._error)