Пример #1
0
    def test_float80_exact(self):
        s = []
        ieee.pack_float80(s, -1., 16, False)
        assert repr(s[-1]) == repr('\x00\x00\x00\x00\x00\x00\x00\x80\xff\xbf\x00\x00\x00\x00\x00\x00')
        ieee.pack_float80(s, -1., 16, True)
        assert repr(s[-1]) == repr('\x00\x00\x00\x00\x00\x00\xbf\xff\x80\x00\x00\x00\x00\x00\x00\x00')
        ieee.pack_float80(s, -123.456, 16, False)
        assert repr(s[-1]) == repr('\x00\xb8\xf3\xfd\xd4x\xe9\xf6\x05\xc0\x00\x00\x00\x00\x00\x00')
        ieee.pack_float80(s, -123.456, 16, True)
        assert repr(s[-1]) == repr('\x00\x00\x00\x00\x00\x00\xc0\x05\xf6\xe9x\xd4\xfd\xf3\xb8\x00')

        x = ieee.unpack_float80('\x00\x00\x00\x00\x00\x00\x00\x80\xff?\xc8\x01\x00\x00\x00\x00', False)
        assert x == 1.0
        x = ieee.unpack_float80('\x00\x00\x7f\x83\xe1\x91?\xff\x80\x00\x00\x00\x00\x00\x00\x00', True)
        assert x == 1.0
Пример #2
0
    def test_float80_exact(self):
        s = []
        ieee.pack_float80(s, -1., 16, False)
        assert repr(s[-1]) == repr(
            '\x00\x00\x00\x00\x00\x00\x00\x80\xff\xbf\x00\x00\x00\x00\x00\x00')
        ieee.pack_float80(s, -1., 16, True)
        assert repr(s[-1]) == repr(
            '\x00\x00\x00\x00\x00\x00\xbf\xff\x80\x00\x00\x00\x00\x00\x00\x00')
        ieee.pack_float80(s, -123.456, 16, False)
        assert repr(s[-1]) == repr(
            '\x00\xb8\xf3\xfd\xd4x\xe9\xf6\x05\xc0\x00\x00\x00\x00\x00\x00')
        ieee.pack_float80(s, -123.456, 16, True)
        assert repr(s[-1]) == repr(
            '\x00\x00\x00\x00\x00\x00\xc0\x05\xf6\xe9x\xd4\xfd\xf3\xb8\x00')

        x = ieee.unpack_float80(
            '\x00\x00\x00\x00\x00\x00\x00\x80\xff?\xc8\x01\x00\x00\x00\x00',
            False)
        assert x == 1.0
        x = ieee.unpack_float80(
            '\x00\x00\x7f\x83\xe1\x91?\xff\x80\x00\x00\x00\x00\x00\x00\x00',
            True)
        assert x == 1.0
Пример #3
0
    def check_float(self, x):
        # check roundtrip
        for size in [10, 12, 16]:
            for be in [False, True]:
                Q = []
                ieee.pack_float80(Q, x, size, be)
                Q = Q[0]
                y = ieee.unpack_float80(Q, be)
                assert repr(x) == repr(y), '%r != %r, Q=%r' % (x, y, Q)

        for be in [False, True]:
            Q = []
            ieee.pack_float(Q, x, 8, be)
            Q = Q[0]
            y = ieee.unpack_float(Q, be)
            assert repr(x) == repr(y), '%r != %r, Q=%r' % (x, y, Q)

        # check that packing agrees with the struct module
        struct_pack8 = struct.unpack('<Q', struct.pack('<d', x))[0]
        float_pack8 = ieee.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 = ieee.float_pack(x, 4)
        except OverflowError:
            float_pack4 = "overflow"
        assert struct_pack4 == float_pack4

        if float_pack4 == "overflow":
            return

        # if we didn't overflow, try round-tripping the binary32 value
        roundtrip = ieee.float_pack(ieee.float_unpack(float_pack4, 4), 4)
        assert float_pack4 == roundtrip

        try:
            float_pack2 = ieee.float_pack(x, 2)
        except OverflowError:
            return

        roundtrip = ieee.float_pack(ieee.float_unpack(float_pack2, 2), 2)
        assert (float_pack2, x) == (roundtrip, x)
Пример #4
0
    def check_float(self, x):
        # check roundtrip
        for size in [10, 12, 16]:
            for be in [False, True]:
                Q = []
                ieee.pack_float80(Q, x, size, be)
                Q = Q[0]
                y = ieee.unpack_float80(Q, be)
                assert repr(x) == repr(y), '%r != %r, Q=%r' % (x, y, Q)

        for be in [False, True]:
            Q = []
            ieee.pack_float(Q, x, 8, be)
            Q = Q[0]
            y = ieee.unpack_float(Q, be)
            assert repr(x) == repr(y), '%r != %r, Q=%r' % (x, y, Q)

        # check that packing agrees with the struct module
        struct_pack8 = struct.unpack('<Q', struct.pack('<d', x))[0]
        float_pack8 = ieee.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 = ieee.float_pack(x, 4)
        except OverflowError:
            float_pack4 = "overflow"
        assert struct_pack4 == float_pack4

        if float_pack4 == "overflow":
            return

        # if we didn't overflow, try round-tripping the binary32 value
        roundtrip = ieee.float_pack(ieee.float_unpack(float_pack4, 4), 4)
        assert float_pack4 == roundtrip

        try:
            float_pack2 = ieee.float_pack(x, 2)
        except OverflowError:
            return

        roundtrip = ieee.float_pack(ieee.float_unpack(float_pack2, 2), 2)
        assert (float_pack2, x) == (roundtrip, x)