def pack(x, size): buf = MutableStringBuffer(size) ieee.pack_float(buf, 0, x, size, False) l = [] for c in buf.finish(): l.append(str(ord(c))) return ','.join(l)
def wdouble(fd, obj): assert isinstance(obj, Float) result = MutableStringBuffer(8) ieee.pack_float(result, 0, obj.number, 8, True) val = result.finish() assert val is not None fd.write(val)
def pack(x, size): result = [] ieee.pack_float(result, x, size, False) l = [] for x in result: for c in x: l.append(str(ord(c))) return ','.join(l)
def TSETM(self, args, space): """ A: base, D: *num (A-1)[D], (A-1)[D+1], ... = A, A+1, ... *num is the index to a num constant that's a float only use first 32 bit of mantissa """ w_table = self.registers[args[0] - 1] index = self.get_num_constant(args[1]) packed = [] pack_float(packed, index, 8, True) index = runpack('>i', packed[0][4:]) for i in xrange(0, len(self.multires)): w_table.set(W_Num(index + i), self.multires[i])
def TSETM(self, args, space): """ A: base, D: *num (A-1)[D], (A-1)[D+1], ... = A, A+1, ... *num is the index to a num constant that's a float only use first 32 bit of mantissa """ w_table = self.registers[args[0]-1] index = self.get_num_constant(args[1]) packed = [] pack_float(packed, index, 8, True) index = runpack('>i', packed[0][4:]) for i in xrange(0, len(self.multires)): w_table.set(W_Num(index+i), self.multires[i])
def packer(fmtiter): fl = fmtiter.accept_float_arg() try: return ieee.pack_float(fmtiter.result, fl, size, fmtiter.bigendian) except OverflowError: assert size == 4 raise StructOverflowError("float too large for format 'f'")
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)
def pack_halffloat(fmtiter): size = 2 fl = fmtiter.accept_float_arg() try: result = ieee.pack_float(fmtiter.wbuf, fmtiter.pos, fl, size, fmtiter.bigendian) except OverflowError: raise StructOverflowError("float too large for format 'e'") else: fmtiter.advance(size) return result
def packer(fmtiter): fl = fmtiter.accept_float_arg() if TYPE is not rffi.FLOAT and pack_fastpath(TYPE)(fmtiter, fl): return # slow path try: result = ieee.pack_float(fmtiter.wbuf, fmtiter.pos, fl, size, fmtiter.bigendian) except OverflowError: assert size == 4 raise StructOverflowError("float too large for format 'f'") else: fmtiter.advance(size) return result
def wdouble(fd, obj): assert isinstance(obj, Float) result = [] ieee.pack_float(result, obj.number, 8, True) fd.write(''.join(result))
def pack_float(f): result = StringBuilder(8) ieee.pack_float(result, f, 8, False) return result.build()
def recv(self, atom, args): # Doubles can be compared. if atom is OP__CMP_1: other = promoteToDouble(args[0]) # NaN cannot compare equal to any float. if math.isnan(self._d) or math.isnan(other): return Incomparable return polyCmp(self._d, other) # Doubles are related to zero. if atom is ABOVEZERO_0: return wrapBool(self._d > 0.0) if atom is ATLEASTZERO_0: return wrapBool(self._d >= 0.0) if atom is ATMOSTZERO_0: return wrapBool(self._d <= 0.0) if atom is BELOWZERO_0: return wrapBool(self._d < 0.0) if atom is ISZERO_0: return wrapBool(self._d == 0.0) if atom is ABS_0: return DoubleObject(abs(self._d)) if atom is ADD_1: return self.add(args[0]) if atom is FLOOR_0: return IntObject(int(self._d)) if atom is MULTIPLY_1: return self.mul(args[0]) if atom is NEGATE_0: return DoubleObject(-self._d) if atom is SQRT_0: return DoubleObject(math.sqrt(self._d)) if atom is SUBTRACT_1: return self.subtract(args[0]) if atom is APPROXDIVIDE_1: divisor = promoteToDouble(args[0]) return DoubleObject(self._d / divisor) # Logarithms. if atom is LOG_0: return DoubleObject(math.log(self._d)) if atom is LOG_1: base = promoteToDouble(args[0]) return DoubleObject(math.log(self._d) / math.log(base)) # Trigonometry. if atom is SIN_0: return DoubleObject(math.sin(self._d)) if atom is COS_0: return DoubleObject(math.cos(self._d)) if atom is TAN_0: return DoubleObject(math.tan(self._d)) if atom is TOBYTES_0: result = [] pack_float(result, self._d, 8, True) return BytesObject(result[0]) raise Refused(self, atom, args)
def toBytes(self): result = [] pack_float(result, self._d, 8, True) return result[0]
def wdouble(fd, obj): assert isinstance(obj, Float) result = [] ieee.pack_float(result, obj.number, 8, True) fd.write("".join(result))
def pack_float(f): buf = MutableStringBuffer(8) ieee.pack_float(buf, 0, f, 8, False) return buf.finish()