Exemple #1
0
    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
Exemple #2
0
 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'
Exemple #3
0
 def pack_float(packer, repetitions):
     space = packer.space
     if repetitions > len(packer.args_w) - packer.args_index:
         raise space.error(space.w_ArgumentError, "too few arguments")
     for i in xrange(packer.args_index, repetitions + packer.args_index):
         w_item = packer.args_w[i]
         if not (isinstance(w_item, W_FloatObject)
                 or isinstance(w_item, W_FixnumObject)):
             raise space.error(
                 space.w_TypeError, "can't convert %s into Float" %
                 space.getclass(w_item).name)
         doubleval = space.float_w(w_item)
         l = ["\0"] * size
         unsigned = float_pack(doubleval, size)
         for i in xrange(size):
             l[i] = chr((unsigned >> (i * 8)) & 0xff)
         if bigendian:
             l.reverse()
         packer.result.extend(l)
     packer.args_index += repetitions
Exemple #4
0
 def pack_float(packer, repetitions):
     space = packer.space
     if repetitions > len(packer.args_w) - packer.args_index:
         raise space.error(space.w_ArgumentError, "too few arguments")
     for i in xrange(packer.args_index, repetitions + packer.args_index):
         w_item = packer.args_w[i]
         if not (isinstance(w_item, W_FloatObject) or isinstance(w_item, W_FixnumObject)):
             raise space.error(
                 space.w_TypeError,
                 "can't convert %s into Float" % space.getclass(w_item).name
             )
         doubleval = space.float_w(w_item)
         l = ["\0"] * size
         unsigned = float_pack(doubleval, size)
         for i in xrange(size):
             l[i] = chr((unsigned >> (i * 8)) & 0xff)
         if bigendian:
             l.reverse()
         packer.result.extend(l)
     packer.args_index += repetitions