def descr_long(self, space): try: return W_LongObject.fromfloat(space, self.floatval) except OverflowError: raise oefmt(space.w_OverflowError, "cannot convert float infinity to integer") except ValueError: raise oefmt(space.w_ValueError, "cannot convert float NaN to integer")
def long__Float(space, w_floatobj): try: return W_LongObject.fromfloat(w_floatobj.floatval) except OverflowError: raise OperationError( space.w_OverflowError, space.wrap("cannot convert float infinity to long"))
def descr_long(self, space): try: return W_LongObject.fromfloat(space, self.floatval) except OverflowError: raise oefmt(space.w_OverflowError, "no puede convertir flot infinito a entero") except ValueError: raise oefmt(space.w_ValueError, "no puede convertir flot NuN a entero")
def eq__Float_Long(space, w_float1, w_long2): # XXX naive implementation x = w_float1.floatval if isinf(x) or math.floor(x) != x: return space.w_False try: w_long1 = W_LongObject.fromfloat(x) except OverflowError: return space.w_False return space.eq(w_long1, w_long2)
def long__Float(space, w_floatobj): try: return W_LongObject.fromfloat(space, w_floatobj.floatval) except OverflowError: if isnan(w_floatobj.floatval): raise OperationError( space.w_ValueError, space.wrap("cannot convert float NaN to integer")) raise OperationError(space.w_OverflowError, space.wrap("cannot convert float infinity to long"))
def lt__Float_Long(space, w_float1, w_long2): # XXX naive implementation x = w_float1.floatval if isinf(x): return space.newbool(x < 0.0) x_floor = math.floor(x) try: w_long1 = W_LongObject.fromfloat(x_floor) except OverflowError: return space.newbool(x < 0.0) return space.lt(w_long1, w_long2)
def long__Float(space, w_floatobj): try: return W_LongObject.fromfloat(space, w_floatobj.floatval) except OverflowError: if isnan(w_floatobj.floatval): raise OperationError( space.w_ValueError, space.wrap("cannot convert float NaN to integer")) raise OperationError( space.w_OverflowError, space.wrap("cannot convert float infinity to long"))
def _hash_float(space, v): if math.isnan(v): return 0 # This is designed so that Python numbers of different types # that compare equal hash to the same value; otherwise comparisons # of mapping keys will turn out weird. fractpart, intpart = math.modf(v) if fractpart == 0.0: # This must return the same hash as an equal int or long. try: x = ovfcheck_float_to_int(intpart) except OverflowError: # Convert to long and use its hash. try: w_lval = W_LongObject.fromfloat(space, v) except (OverflowError, ValueError): # can't convert to long int -- arbitrary if v < 0: return -271828 else: return 314159 return space.int_w(space.hash(w_lval)) else: # Fits in a C long == a Python int. from pypy.objspace.std.intobject import _hash_int return _hash_int(x) # The fractional part is non-zero, so we don't have to worry about # making this match the hash of some other type. # Use frexp to get at the bits in the double. # Since the VAX D double format has 56 mantissa bits, which is the # most of any double format in use, each of these parts may have as # many as (but no more than) 56 significant bits. # So, assuming sizeof(long) >= 4, each part can be broken into two # longs; frexp and multiplication are used to do that. # Also, since the Cray double format has 15 exponent bits, which is # the most of any double format in use, shifting the exponent field # left by 15 won't overflow a long (again assuming sizeof(long) >= 4). v, expo = math.frexp(v) v *= 2147483648.0 # 2**31 hipart = int(v) # take the top 32 bits v = (v - hipart) * 2147483648.0 # get the next 32 bits x = intmask(hipart + int(v) + (expo << 15)) x -= (x == -1) return x
def _hash_float(space, v): from pypy.objspace.std.longobject import hash__Long if isnan(v): return 0 # This is designed so that Python numbers of different types # that compare equal hash to the same value; otherwise comparisons # of mapping keys will turn out weird. fractpart, intpart = math.modf(v) if fractpart == 0.0: # This must return the same hash as an equal int or long. try: x = ovfcheck_float_to_int(intpart) # Fits in a C long == a Python int, so is its own hash. return x except OverflowError: # Convert to long and use its hash. try: w_lval = W_LongObject.fromfloat(space, v) except OverflowError: # can't convert to long int -- arbitrary if v < 0: return -271828 else: return 314159 return space.int_w(space.hash(w_lval)) # The fractional part is non-zero, so we don't have to worry about # making this match the hash of some other type. # Use frexp to get at the bits in the double. # Since the VAX D double format has 56 mantissa bits, which is the # most of any double format in use, each of these parts may have as # many as (but no more than) 56 significant bits. # So, assuming sizeof(long) >= 4, each part can be broken into two # longs; frexp and multiplication are used to do that. # Also, since the Cray double format has 15 exponent bits, which is # the most of any double format in use, shifting the exponent field # left by 15 won't overflow a long (again assuming sizeof(long) >= 4). v, expo = math.frexp(v) v *= 2147483648.0 # 2**31 hipart = int(v) # take the top 32 bits v = (v - hipart) * 2147483648.0 # get the next 32 bits x = intmask(hipart + int(v) + (expo << 15)) return x
def float_as_integer_ratio__Float(space, w_float): value = w_float.floatval if isinf(value): w_msg = space.wrap("cannot pass infinity to as_integer_ratio()") raise OperationError(space.w_OverflowError, w_msg) elif isnan(value): w_msg = space.wrap("cannot pass nan to as_integer_ratio()") raise OperationError(space.w_ValueError, w_msg) float_part, exp = math.frexp(value) for i in range(300): if float_part == math.floor(float_part): break float_part *= 2.0 exp -= 1 w_num = W_LongObject.fromfloat(space, float_part) w_den = space.newlong(1) w_exp = space.newlong(abs(exp)) w_exp = space.lshift(w_den, w_exp) if exp > 0: w_num = space.mul(w_num, w_exp) else: w_den = w_exp # Try to return int. return space.newtuple([space.int(w_num), space.int(w_den)])
def long__Float(space, w_floatobj): try: return W_LongObject.fromfloat(w_floatobj.floatval) except OverflowError: raise OperationError(space.w_OverflowError, space.wrap("cannot convert float infinity to long"))