def _get_inttime(space, w_seconds): # w_seconds can be a wrapped None (it will be automatically wrapped # in the callers, so we never get a real None here). if space.is_w(w_seconds, space.w_None): seconds = pytime.time() else: seconds = space.float_w(w_seconds) try: ovfcheck_float_to_int(seconds) except OverflowError: raise OperationError(space.w_ValueError, space.wrap("time argument too large")) return rffi.r_time_t(seconds)
def int__Float(space, w_value): try: value = ovfcheck_float_to_int(w_value.floatval) except OverflowError: return space.long(w_value) else: return space.newint(value)
def trunc__Float(space, w_floatobj): whole = math.modf(w_floatobj.floatval)[1] try: value = ovfcheck_float_to_int(whole) except OverflowError: return long__Float(space, w_floatobj) else: return space.newint(value)
def visit_DECIMALLITERAL(self, node): pos = self.get_pos(node) try: f = float(node.additional_info) i = ovfcheck_float_to_int(f) if i != f: return operations.FloatNumber(pos, f) else: return operations.IntNumber(pos, i) except (ValueError, OverflowError): return operations.FloatNumber(pos, float(node.additional_info))
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 _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(v) except OverflowError: # can't convert to long int -- arbitrary if v < 0: return -271828 else: return 314159 return space.int_w(hash__Long(space, 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 sleep(space, w_secs): """sleep(seconds) Delay execution for a given number of seconds. The argument may be a floating point number for subsecond precision.""" secs = space.float_w(w_secs) if secs < 0.0: secs = 0.0 msecs = secs * 1000.0 try: msecs = ovfcheck_float_to_int(msecs) except OverflowError: raise OperationError(space.w_OverflowError, space.wrap("sleep length is too large")) ul_millis = c_ulong(msecs) Sleep(ul_millis)
def func(fl): try: return ovfcheck_float_to_int(fl) except OverflowError: return -666
def math_pow(args): try: return ovfcheck_float_to_int(math.pow(args[0], args[1])) except OverflowError: raise
def math_sqrt(args): try: return ovfcheck_float_to_int(math.sqrt(args[0])) except OverflowError: raise