def test_fromfloat(self): x = 1234567890.1234567890 f1 = rbigint.fromfloat(x) y = f1.tofloat() assert f1.tolong() == long(x) # check overflow #x = 12345.6789e10000000000000000000000000000 # XXX don't use such consts. marshal doesn't handle them right. x = 12345.6789e200 x *= x assert py.test.raises(OverflowError, rbigint.fromfloat, x) assert py.test.raises(ValueError, rbigint.fromfloat, NAN) # f1 = rbigint.fromfloat(9007199254740991.0) assert f1.tolong() == 9007199254740991
def get_stat_ns_as_bigint(st, name): """'name' is one of the strings "atime", "mtime" or "ctime". Returns a bigint that represents the number of nanoseconds stored inside the RPython-level os.stat_result 'st'. Note that when running untranslated, the os.stat_result type is from Python 2.7, which doesn't store more precision than a float anyway. You will only get more after translation. """ from rpython.rlib.rbigint import rbigint if not we_are_translated(): as_float = getattr(st, "st_" + name) return rbigint.fromfloat(as_float * 1e9) if name == "atime": i, j = 7, -3 elif name == "mtime": i, j = 8, -2 elif name == "ctime": i, j = 9, -1 else: raise AssertionError(name) sec = st[i] nsec = st[j] result = rbigint.fromrarith_int(sec).int_mul(1000000000) result = result.int_add(nsec) return result
def test_tofloat_precision(self): assert rbigint.fromlong(0).tofloat() == 0.0 for sign in [1, -1]: for p in xrange(100): x = long(2**p * (2**53 + 1) + 1) * sign y = long(2**p * (2**53+ 2)) * sign rx = rbigint.fromlong(x) rxf = rx.tofloat() assert rxf == float(y) assert rbigint.fromfloat(rxf).tolong() == y # x = long(2**p * (2**53 + 1)) * sign y = long(2**p * 2**53) * sign rx = rbigint.fromlong(x) rxf = rx.tofloat() assert rxf == float(y) assert rbigint.fromfloat(rxf).tolong() == y
def test_fromfloat(self): x = 1234567890.1234567890 f1 = rbigint.fromfloat(x) y = f1.tofloat() assert f1.tolong() == long(x) # check overflow #x = 12345.6789e10000000000000000000000000000 # XXX don't use such consts. marshal doesn't handle them right. x = 12345.6789e200 x *= x assert py.test.raises(OverflowError, rbigint.fromfloat, x) assert py.test.raises(ValueError, rbigint.fromfloat, NAN) # f1 = rbigint.fromfloat(9007199254740991.0) assert f1.tolong() == 9007199254740991 null = rbigint.fromfloat(-0.0) assert null.int_eq(0)
def do_compare_bigint(f1, b2): """f1 is a float. b2 is a bigint.""" if not isfinite(f1) or math.floor(f1) != f1: return opname == 'ne' b1 = rbigint.fromfloat(f1) res = b1.eq(b2) if opname == 'ne': res = not res return res
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 do_compare_bigint(f1, b2): """f1 is a float. b2 is a bigint.""" if not isfinite(f1): return op(f1, 0.0) if opname == 'gt' or opname == 'le': # 'float > long' <==> 'ceil(float) > long' # 'float <= long' <==> 'ceil(float) <= long' f1 = math.ceil(f1) else: # 'float < long' <==> 'floor(float) < long' # 'float >= long' <==> 'floor(float) >= long' f1 = math.floor(f1) b1 = rbigint.fromfloat(f1) return getattr(b1, opname)(b2)
def float_as_rbigint_ratio(value): from rpython.rlib.rbigint import rbigint if isinf(value): raise OverflowError("cannot pass infinity to as_integer_ratio()") elif isnan(value): raise ValueError("cannot pass nan to as_integer_ratio()") float_part, exp_int = math.frexp(value) for i in range(300): if float_part == math.floor(float_part): break float_part *= 2.0 exp_int -= 1 num = rbigint.fromfloat(float_part) den = rbigint.fromint(1) exp = den.lshift(abs(exp_int)) if exp_int > 0: num = num.mul(exp) else: den = exp return num, den
def fromfloat(space, f): return newlong(space, rbigint.fromfloat(f))
def floorDivideDouble(self, other): # Of the two ways to lose precision, we had to choose one. ~ C. return rbigint.fromfloat(self.bi.tofloat() / other)
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 fromfloat(value): try: val = rarithmetic.ovfcheck_float_to_int(value) except OverflowError: return W_Bignum(rbigint.fromfloat(value)) return W_Fixnum(val)
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_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_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 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 bigint_w(self, space): return rbigint.fromfloat(self.floatvalue)
def newbigint_fromfloat(space, floatvalue): return W_BignumObject(space, rbigint.fromfloat(floatvalue))