def arith_shl_bigint(self, other_value): try: num = self.value.toint() except OverflowError: # XXX raise a Prolog-level error! raise ValueError('Right operand too big') return make_int(term.BigInt(other_value.lshift(num)))
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 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 arith_pow_bigint(self, other_value): return make_int(term.BigInt(other_value.pow(self.value)))
def arith_floordiv_number(self, other_num): return make_int(term.BigInt(rbigint.fromint(other_num).div(self.value)))
def arith_unarysub(self): try: res = rarithmetic.ovfcheck(-self.num) except OverflowError: return term.BigInt(rbigint.fromint(self.num).neg()) return term.Number(res)
def arith_min_number(self, other_num): other_value = rbigint.fromint(other_num) if other_value.lt(self.value): return make_int(term.BigInt(other_value)) return make_int(term.BigInt(self.value))
def arith_not(self): return make_int(term.BigInt(self.value.invert()))
def arith_xor_bigint(self, other_value): return make_int(term.BigInt(other_value.xor(self.value)))
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_min_bigint(self, other_value): self_value = rbigint.fromint(self.num) if self_value.lt(other_value): return make_int(term.BigInt(self_value)) return make_int(term.BigInt(other_value))
def arith_mod_bigint(self, other_value): if self.num == 0: error.throw_evaluation_error("zero_divisor") return make_int(term.BigInt(other_value.mod(rbigint.fromint(self.num))))
def arith_xor_bigint(self, other_value): return make_int(term.BigInt(rbigint.fromint(self.num).xor(other_value)))
def arith_shl_bigint(self, other_value): return make_int(term.BigInt(other_value.lshift(self.num)))
def arith_pow_bigint(self, other_value): return make_int(term.BigInt(other_value.pow(rbigint.fromint(self.num))))
def arith_and_bigint(self, other_value): return make_int(term.BigInt(other_value.and_(self.value)))
def arith_xor_number(self, other_num): return make_int(term.BigInt(rbigint.fromint(other_num).xor(self.value)))
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_mod_bigint(self, other_value): try: return make_int(term.BigInt(other_value.mod(self.value))) except ZeroDivisionError: error.throw_evaluation_error("zero_divisor")
def arith_sub_bigint(self, other_value): return make_int(term.BigInt(other_value.sub(self.value)))
def arith_abs(self): return make_int(term.BigInt(self.value.abs()))
def arith_unarysub(self): return term.BigInt(self.value.neg())
def arith_min_bigint(self, other_value): if other_value.lt(self.value): return make_int(term.BigInt(other_value)) return make_int(term.BigInt(self.value))
def arith_mul_bigint(self, other_value): return make_int(term.BigInt(other_value.mul(self.value)))