def float_to_w_int(space, floatvalue): try: # the extra case makes sure that this method returns # bignums for the same numbers as the parser does. # this is checked in rubyspecs if floatvalue < 0: return space.newint(-ovfcheck_float_to_int(-floatvalue)) else: return space.newint(ovfcheck_float_to_int(floatvalue)) except OverflowError: return space.newbigint_fromfloat(floatvalue)
def descr_trunc(self, space): try: value = ovfcheck_float_to_int(self.floatval) except OverflowError: return self.descr_long(space) else: return space.newint(value)
def arith_pow_same(self, other): assert isinstance(other, values.W_Fixnum) # XXX nonsense try: res = rarithmetic.ovfcheck_float_to_int(math.pow(self.value, other.value)) except OverflowError: return self.arith_pow(values.W_Bignum(rbigint.fromint(other.value))) return values.W_Fixnum(res)
def descr_trunc(self, space): whole = math.modf(self.floatval)[1] try: value = ovfcheck_float_to_int(whole) except OverflowError: return self.descr_long(space) else: return space.newint(value)
def newint_from_float(space, floatval): """This is also used from module/math/interp_math.py""" try: value = ovfcheck_float_to_int(floatval) except OverflowError: return newlong_from_float(space, floatval) 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 int(self, space): if (type(self) is not W_FloatObject and space.is_overloaded(self, space.w_float, '__int__')): return W_Root.int(self, space) try: value = ovfcheck_float_to_int(self.floatval) except OverflowError: return space.long(self) else: return space.newint(value)
def visit_DECIMALLITERAL(self, node): try: f = float(node.additional_info) i = ovfcheck_float_to_int(f) if i != f: return operations.ConstantFloat(f) else: return operations.ConstantInt(i) except (ValueError, OverflowError): return operations.ConstantFloat(float(node.additional_info))
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 visit_numericliteral(self, node): number = "" for node in node.children: number += node.additional_info try: f = float(number) i = ovfcheck_float_to_int(f) if i != f: return operations.ConstantFloat(f) else: return operations.ConstantInt(i) except (ValueError, OverflowError): return operations.ConstantFloat(float(node.additional_info))
def arith_round(self): fval = self.floatval if fval >= 0: factor = 1 else: factor = -1 fval = fval * factor try: val = ovfcheck_float_to_int(math.floor(fval + 0.5) * factor) except OverflowError: return term.BigInt(rbigint.fromfloat(math.floor(self.floatval + 0.5) * factor)) return term.Number(val)
def enable(space, fileno, period=0.01): # default 100 Hz from pypy.module._vmprof import Module mod_vmprof = space.getbuiltinmodule('_vmprof') assert isinstance(mod_vmprof, Module) # try: period_usec = ovfcheck_float_to_int(period * 1000000.0 + 0.5) if period_usec <= 0 or period_usec >= 1e6: # we don't want seconds here at all raise ValueError except (ValueError, OverflowError): raise OperationError(space.w_ValueError, space.wrap("'period' too large or non positive")) # mod_vmprof.vmprof.enable(space, fileno, period_usec)
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 _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_none(w_seconds): seconds = pytime.time() else: seconds = space.float_w(w_seconds) try: seconds = ovfcheck_float_to_int(seconds) t = rffi.r_time_t(seconds) if rffi.cast(lltype.Signed, t) != seconds: raise OverflowError except OverflowError: raise OperationError(space.w_ValueError, space.wrap("time argument too large")) return t
def visit_numericliteral(self, node): number = "" for node in node.children: number += node.additional_info try: f = float(number) i = ovfcheck_float_to_int(f) if i != f: index = self.declare_constant_float(f) return operations.ConstantFloat(f, index) else: index = self.declare_constant_int(i) return operations.ConstantInt(i, index) except (ValueError, OverflowError): f = float(node.additional_info) index = self.declare_constant_float(f) return operations.ConstantFloat(f, index)
def _hash_float(space, v): 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, ValueError): # 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 arith_float_fractional_part(self): try: val = rarithmetic.ovfcheck_float_to_int(self.value) except OverflowError: val = rbigint.fromfloat(self.value).tofloat() return values.W_Flonum(float(self.value - val))
def _arith_float_fractional_part(self): try: val = rarithmetic.ovfcheck_float_to_int(self.value) except OverflowError: val = rbigint.fromfloat(self.value).tofloat() return float(self.value - val)
def _parse_int(string, radix): assert isinstance(string, unicode) NUMERALS = u'0123456789abcdefghijklmnopqrstuvwxyz' input_string = string s = _strip(input_string) sign = 1 if s.startswith(u'-'): sign = -1 if s.startswith(u'-') or s.startswith(u'+'): s = s[1:] r = radix strip_prefix = True if r != 0: if r < 2 or r > 36: return NAN if r != 16: strip_prefix = False else: r = 10 if strip_prefix: if len(s) >= 2 and (s.startswith(u'0x') or s.startswith(u'0X')): s = s[2:] r = 16 # TODO this is not specified in ecma 5 but tests expect it and it's implemented in v8! elif len(s) > 1 and s.startswith(u'0'): r = 8 numerals = NUMERALS[:r] z = [] for char in s: uni_ord = unicodedb.tolower(ord(char)) if uni_ord > 128: break c = chr(uni_ord) if c not in numerals: break z.append(c) if not z: return NAN num_str = ''.join(z) try: number = int(num_str, r) try: from rpython.rlib.rarithmetic import ovfcheck_float_to_int ovfcheck_float_to_int(number) except OverflowError: number = float(number) return sign * number except OverflowError: return INFINITY except ValueError: pass return NAN
def fromfloat(value): try: val = rarithmetic.ovfcheck_float_to_int(value) except OverflowError: return W_Bignum(rbigint.fromfloat(value)) return W_Fixnum(val)
def _time_primitive(primitive, activation_record, interpreter): # Returns an integer representing a point in time. Should be used for benching activation_record.push(interpreter.space.new_integer(ovfcheck_float_to_int(time.time() * 1000000)))
def func(interp, s_frame, f): try: return interp.space.wrap_int(ovfcheck_float_to_int(f)) except OverflowError: raise PrimitiveFailedError
def arith_ceiling(self): try: val = ovfcheck_float_to_int(math.ceil(self.floatval)) except OverflowError: return term.BigInt(rbigint.fromfloat(math.ceil(self.floatval))) return term.Number(val)
def arith_pow_number(self, other_num): try: res = ovfcheck_float_to_int(math.pow(other_num, self.num)) except OverflowError: return self.arith_pow_bigint(rbigint.fromint(other_num)) return term.Number(res)
def arith_float_fractional_part(self): try: val = ovfcheck_float_to_int(self.floatval) except OverflowError: val = rbigint.fromfloat(self.floatval).tofloat() return term.Float(float(self.floatval - val))
def arith_float_integer_part(self): try: val = ovfcheck_float_to_int(self.floatval) except OverflowError: return term.BigInt(rbigint.fromfloat(self.floatval)) return term.Number(val)
def func(fl): try: return ovfcheck_float_to_int(fl) except OverflowError: return -666