def test_add(self): f1 = Float(5.1) f2 = Float(10.1) assert f1.arith_add(f2).floatval == 15.2 n0 = Number(1) n1 = Number(2) assert n0.arith_add(n1).num == 3 n2 = Number(2) f3 = Float(3.2) assert n2.arith_add(f3).floatval == 5.2 assert f3.arith_add(n2).floatval == 5.2 b1 = BigInt(rbigint.fromdecimalstr('50000000000000000000000')) b2 = BigInt(rbigint.fromdecimalstr('10000000000000000000001')) assert b1.arith_add(b2).value.str() == '60000000000000000000001' n3 = Number(sys.maxint) assert n3.arith_add(n0).value.str() == str(sys.maxint + 1) b = BigInt(rbigint.fromdecimalstr('100000000000000000000000000000')) f = Float(1.5) assert b.arith_add(f).floatval == 100000000000000000000000000001.5 assert f.arith_add(b).floatval == 100000000000000000000000000001.5 assert b.arith_add(n0).value.tofloat() == 100000000000000000000000000001.0 assert n0.arith_add(b).value.tofloat() == 100000000000000000000000000001.0
def test_min(self): assert Number(5).arith_min(Number(1)).num == 1 assert Float(-1.32).arith_min(Float(4.5)).floatval == -1.32 assert BigInt(rbigint.fromdecimalstr('111111111111111111111111111')).arith_min(BigInt(rbigint.fromdecimalstr('222222222222222222222222222222'))).value.str() == '111111111111111111111111111' assert Number(-1000).arith_min(BigInt(rbigint.fromint(-1001))).num == -1001 assert BigInt(rbigint.fromint(-1001)).arith_min(Number(-1000)).num == -1001 assert BigInt(rbigint.fromdecimalstr('10000')).arith_min(Float(20000)).floatval == 10000.0 assert Float(20000).arith_min(BigInt(rbigint.fromdecimalstr('10000'))).floatval == 10000.0
def test_power(self): assert Number(5).arith_pow(Number(2)).num == 25 assert Float(2.3).arith_pow(Float(3.1)).floatval == 13.223800591254721 assert BigInt(rbigint.fromdecimalstr('1000000')).arith_pow(Number(4)).value.str() == '1000000000000000000000000' assert Float(10.0).arith_pow(BigInt(rbigint.fromdecimalstr('10'))).floatval == 10000000000.0 assert BigInt(rbigint.fromdecimalstr('10')).arith_pow(Float(10.0)).floatval == 10000000000.0 assert BigInt(rbigint.fromdecimalstr('1000000000000000')).arith_pow(Number(0)).num == 1 assert Float(10000000000).arith_pow(BigInt(rbigint.fromdecimalstr('0'))).floatval == 1.0
def test_mul(self): assert Number(5).arith_mul(Number(100)).num == 500 assert Number(5).arith_mul(BigInt(rbigint.fromdecimalstr('1000000000000000000000000000000'))).value.tofloat() == 5000000000000000000000000000000.0 assert Number(-10).arith_mul(Float(-7.3)).floatval == 73 assert BigInt(rbigint.fromdecimalstr('-1000000000000000000000000000')).arith_mul(BigInt(rbigint.fromdecimalstr('100000000000000000000'))).value.str() == '-100000000000000000000000000000000000000000000000' assert Float(6.7).arith_mul(Float(-2.4)).floatval == -16.08 assert Float(6.7).arith_mul(BigInt(rbigint.fromdecimalstr('100000000000000000000000000000000000'))).floatval == 670000000000000000000000000000000000.0 assert BigInt(rbigint.fromdecimalstr('100000000000000000000000000000000000')).arith_mul(Float(6.7)).floatval == 670000000000000000000000000000000000.0 assert Number(2).arith_mul(Float(2.5)).floatval == 5 assert Float(2.5).arith_mul(Number(2)).floatval == 5
def test_floordiv(self): assert Number(5).arith_floordiv(Number(2)).num == 2 assert Number(15).arith_floordiv(Number(5)).num == 3 py.test.raises(error.CatchableError, "Number(5).arith_floordiv(Float(2.5))") py.test.raises(error.CatchableError, "Float(2.5).arith_floordiv(Number(5))") py.test.raises(error.CatchableError, "Float(-10).arith_floordiv(Float(2.5))") assert BigInt(rbigint.fromdecimalstr('50000000000000000')).arith_floordiv(BigInt(rbigint.fromdecimalstr('25000000000000000'))).num == 2 py.test.raises(error.CatchableError, "BigInt(rbigint.fromdecimalstr('100000000000000000000')).arith_floordiv(Float(100000000000000000000.0))") py.test.raises(error.CatchableError, "Float(100000000000000000000).arith_floordiv(BigInt(rbigint.fromdecimalstr('100000000000000000000')))") assert Number(5).arith_floordiv(BigInt(rbigint.fromdecimalstr('5'))).num == 1 assert BigInt(rbigint.fromdecimalstr('5')).arith_floordiv(Number(5)).num == 1 py.test.raises(error.CatchableError, 'BigInt(rbigint.fromdecimalstr(\'1\')).arith_floordiv(BigInt(rbigint.fromdecimalstr(\'0\')))') py.test.raises(error.CatchableError, 'BigInt(rbigint.fromdecimalstr(\'1\')).arith_floordiv(Number(0))') py.test.raises(error.CatchableError, 'Number(1).arith_floordiv(Number(0))')
def read_number_or_id(f, init): sofar = StringBuilder(64) sofar.append(init) while True: c = f.peek() if c == "": break if idchar(c): v = f.read(1) assert v == c sofar.append(v) else: break got = sofar.build() try: val = string_to_int(got) return values.W_Fixnum.make_or_interned(val) except ParseStringOverflowError: val = rbigint.fromdecimalstr(got) return values.W_Bignum(val) except ParseStringError: try: return values.W_Flonum(float(got)) except: return values.W_Symbol.make(got)
def _to_num(json): assert json.is_object obj = json.value_object() if "real" in obj: r = obj["real"] return values.W_Flonum.make(r.value_float()) if "real-part" in obj: r = obj["real-part"] i = obj["imag-part"] return values.W_Complex.make(_to_num(r), _to_num(i)) if "numerator" in obj: n = obj["numerator"] d = obj["denominator"] return values.W_Rational.make(_to_num(n), _to_num(d)) if "extended-real" in obj: rs = obj["extended-real"].value_string() if rs == "+inf.0": return values.W_Flonum.INF if rs == "-inf.0": return values.W_Flonum.NEGINF if rs == "+nan.0": return values.W_Flonum.NAN if "integer" in obj: rs = obj["integer"].value_string() try: return values.W_Fixnum.make(string_to_int(rs)) except ParseStringOverflowError: val = rbigint.fromdecimalstr(rs) return values.W_Bignum(val) assert False
def test_fromdecimalstr(self): x = rbigint.fromdecimalstr("12345678901234567890523897987") assert x.tolong() == 12345678901234567890523897987L assert x.tobool() is True x = rbigint.fromdecimalstr("+12345678901234567890523897987") assert x.tolong() == 12345678901234567890523897987L assert x.tobool() is True x = rbigint.fromdecimalstr("-12345678901234567890523897987") assert x.tolong() == -12345678901234567890523897987L assert x.tobool() is True x = rbigint.fromdecimalstr("+0") assert x.tolong() == 0 assert x.tobool() is False x = rbigint.fromdecimalstr("-0") assert x.tolong() == 0 assert x.tobool() is False
def to_num(value): if value[0] == "+": value = value[1:] if value == "inf.0": return W_Flonum.INF elif value == "-inf.0": return W_Flonum.NEGINF elif value == "nan.0": return W_Flonum.NAN if "/" in value: if len(re.findall('/', value)) > 1: raise Exception( "Something's wrong with this rational number : %s" % value) sides = value.split("/") numer = sides[0] denom = sides[1] return W_Rational.make(to_num(numer), to_num(denom)) v = float(value) if "." in value: return W_Flonum(v) else: try: return W_Fixnum.make(string_to_int(value)) except ParseStringOverflowError: val = rbigint.fromdecimalstr(value) return W_Bignum(val) raise Exception("Didn't know what to do with this number : %s" % value)
def _to_num(json): assert json.is_object obj = json.value_object() if "real" in obj: r = obj["real"] return values.W_Flonum.make(r.value_float()) if "real-part" in obj: r = obj["real-part"] i = obj["imag-part"] return values.W_Complex.make(_to_num(r), _to_num(i)) if "numerator" in obj: n = obj["numerator"] d = obj["denominator"] return values.W_Rational.make(_to_num(n), _to_num(d)) if "extended-real" in obj: rs = obj["extended-real"].value_string() if rs == "+inf.0": return INF if rs == "-inf.0": return NEGINF if rs == "+nan.0": return NAN if "integer" in obj: rs = obj["integer"].value_string() try: return values.W_Fixnum.make(string_to_int(rs)) except ParseStringOverflowError: val = rbigint.fromdecimalstr(rs) return values.W_Bignum(val) assert False
def test_data_types_32_bit(self): if is_64_bit(): py.test.skip("only test on 32 bit") assert BigInt(rbigint.fromdecimalstr('348765738456378457436537854637845')).arith_mod(BigInt(rbigint.fromdecimalstr('845763478537534095'))).value.str() == '738607793931799615' assert BigInt(rbigint.fromdecimalstr('10')).arith_pow(BigInt(rbigint.fromdecimalstr('10'))).value.str() == '10000000000' assert BigInt(rbigint.fromdecimalstr('34876573845637845')).arith_xor(BigInt(rbigint.fromdecimalstr('845763478537534095'))).value.str() == '848692582328774746' assert BigInt(rbigint.fromdecimalstr('34876573845637845')).arith_and(BigInt(rbigint.fromdecimalstr('845763478537534095'))).value.str() == '15973735027198597'
def test_convert_to_str(): assert "a" == convert_to_str(Callable.build("a")) assert "100" == convert_to_str(Callable.build("100")) assert "1000.111" == convert_to_str(Callable.build("1000.111")) assert ("100000000000000000000" == convert_to_str(Callable.build("100000000000000000000"))) assert "1" == convert_to_str(BigInt(rbigint.fromint(1))) assert ("-1000000000000000" == convert_to_str(BigInt(rbigint.fromdecimalstr("-1000000000000000"))))
def exact_from_node(self, node, radix): s = node.token.source i = 0 # Skip prefixes while s[i] == "#": i += 2 s = s[i:] src_pos = self.make_src_pos(node) try: return kt.Fixnum(string_to_int(s, radix), src_pos) except rstring.ParseStringOverflowError: if radix == 10: bi = rbigint.fromdecimalstr(s) else: bi = rbigint.fromstr(s, radix) return kt.Bignum(bi, src_pos)
def read_number_or_id(f, init): sofar = [init] while True: (count, c) = f.peek() if c == "": break c = c[0] if c.isalnum(): sofar.append(f.read(1)) else: break got = "".join(sofar) try: return NumberToken(values.W_Fixnum.make(string_to_int(got))) except ParseStringOverflowError: val = rbigint.fromdecimalstr(got) return NumberToken(values.W_Bignum(val)) except ParseStringError: try: return NumberToken(values.W_Flonum.make(float(got))) except: return SymbolToken(values.W_Symbol.make(got))
def read_number_or_id(f, init): sofar = [init] while True: c = f.peek() if c == "": break if idchar(c): v = f.read(1) assert v == c sofar.append(v) else: break got = "".join(sofar) try: return NumberToken(values.W_Fixnum.make(string_to_int(got))) except ParseStringOverflowError: val = rbigint.fromdecimalstr(got) return NumberToken(values.W_Bignum(val)) except ParseStringError: try: return NumberToken(values.W_Flonum.make(float(got))) except: return SymbolToken(values.W_Symbol.make(got))
def test_div(self): assert Number(5).arith_div(Number(2)).num == 2 assert Number(15).arith_div(Number(5)).num == 3 assert Number(5).arith_div(Float(2.5)).floatval == 2.0 assert Float(2.5).arith_div(Number(5)).floatval == 0.5 assert Float(-10).arith_div(Float(2.5)).floatval == -4.0 assert BigInt(rbigint.fromdecimalstr('50000000000000000')).arith_div(BigInt(rbigint.fromdecimalstr('25000000000000000'))).num == 2 assert BigInt(rbigint.fromdecimalstr('100000000000000000000')).arith_div(Float(100000000000000000000.0)).floatval == 1.0 assert Float(100000000000000000000).arith_div(BigInt(rbigint.fromdecimalstr('100000000000000000000'))).floatval == 1.0 assert Number(5).arith_div(BigInt(rbigint.fromdecimalstr('5'))).num == 1 assert BigInt(rbigint.fromdecimalstr('5')).arith_div(Number(5)).num == 1 py.test.raises(error.CatchableError, 'BigInt(rbigint.fromdecimalstr(\'1\')).arith_div(BigInt(rbigint.fromdecimalstr(\'0\')))') py.test.raises(error.CatchableError, 'BigInt(rbigint.fromdecimalstr(\'1\')).arith_div(Number(0))') py.test.raises(error.CatchableError, 'BigInt(rbigint.fromdecimalstr(\'1\')).arith_div(Float(0))') py.test.raises(error.CatchableError, 'Float(1).arith_div(Number(0))') py.test.raises(error.CatchableError, 'Number(1).arith_div(Number(0))') py.test.raises(error.CatchableError, 'Number(1).arith_div(Float(0))')
def cons_to_num(charlist): from prolog.interpreter.helper import unwrap_list, unwrap_atom unwrapped = unwrap_list(charlist) numlist = [] saw_dot = False first = True i = 0 for elem in unwrapped: if not isinstance(elem, term.Atom): error.throw_type_error("text", charlist) digit = elem.name() if digit not in digits: if digit == ".": if saw_dot or first or (i == 1 and numlist[0] == "-"): error.throw_syntax_error("Illegal number") else: saw_dot = True elif digit == "-": if not first: error.throw_syntax_error("Illegal number") else: error.throw_syntax_error("Illegal number") numlist.append(digit) i += 1 first = False numstr = "".join(numlist) if numstr.find(".") == -1: # no float try: return term.Number(string_to_int(numstr)) except ParseStringOverflowError: return term.BigInt(rbigint.fromdecimalstr(numstr)) try: return term.Float(float(numstr)) except ValueError: error.throw_syntax_error("Illegal number")
def test_sub(self): n1 = Number(5) n2 = Number(10) assert n1.arith_sub(n2).num == -5 assert n2.arith_sub(n1).num == 5 f1 = Float(10.5) f2 = Float(30.6) assert f1.arith_sub(f2).floatval == -20.1 assert f2.arith_sub(f1).floatval == 20.1 b1 = BigInt(rbigint.fromdecimalstr('10000000000000000000000000000000000000')) b2 = BigInt(rbigint.fromdecimalstr('20000000000000000000000000000000000000')) assert b1.arith_sub(b2).value.tofloat() == -10000000000000000000000000000000000000.0 assert b2.arith_sub(b1).value.tofloat() == 10000000000000000000000000000000000000.0 assert BigInt(rbigint.fromdecimalstr('100000000000000000000')).arith_sub(Number(1)).value.str() == '99999999999999999999' assert Number(5).arith_sub(BigInt(rbigint.fromdecimalstr('5'))).num == 0 assert BigInt(rbigint.fromdecimalstr('1000000000000000')).arith_sub(Float(500000000000000)).floatval == 500000000000000.0 assert Float(2000000000000000).arith_sub(BigInt(rbigint.fromdecimalstr('1000000000000000'))).floatval == 1000000000000000.0
def test_invert(self): assert Number(2345).arith_not().num == -2346 assert BigInt(rbigint.fromdecimalstr('37846578346543875674385')).arith_not().value.str() == '-37846578346543875674386'
def test_abs(self): assert Number(-345345345).arith_abs().num == 345345345 assert Float(345345.435).arith_abs().floatval == 345345.435 assert Float(-345345.435).arith_abs().floatval == 345345.435 assert BigInt(rbigint.fromdecimalstr('-123123123123123123123123123')).arith_abs().value.str() == '123123123123123123123123123'
def fromstr(s): return Integer(RBigInt.fromdecimalstr(s))
def test_or(self): assert Number(8).arith_or(Number(2)).num == 10 assert BigInt(rbigint.fromint(256)).arith_or(Number(128)).num == 384 assert BigInt(rbigint.fromdecimalstr('18446744073709551616')).arith_or(BigInt(rbigint.fromdecimalstr('9223372036854775808'))).value.str() == '27670116110564327424' assert Number(128).arith_or(BigInt(rbigint.fromint(256))).num == 384