Esempio n. 1
0
 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)))
Esempio n. 2
0
    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)
Esempio n. 3
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")
Esempio n. 4
0
 def arith_pow_bigint(self, other_value):
     return make_int(term.BigInt(other_value.pow(self.value)))
Esempio n. 5
0
 def arith_floordiv_number(self, other_num):
     return make_int(term.BigInt(rbigint.fromint(other_num).div(self.value)))
Esempio n. 6
0
 def arith_unarysub(self):
     try:
         res = rarithmetic.ovfcheck(-self.num)
     except OverflowError:
         return term.BigInt(rbigint.fromint(self.num).neg())
     return term.Number(res)
Esempio n. 7
0
 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))
Esempio n. 8
0
 def arith_not(self):
     return make_int(term.BigInt(self.value.invert()))
Esempio n. 9
0
 def arith_xor_bigint(self, other_value):
     return make_int(term.BigInt(other_value.xor(self.value)))
Esempio n. 10
0
 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)
Esempio n. 11
0
 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))
Esempio n. 12
0
 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))))
Esempio n. 13
0
 def arith_xor_bigint(self, other_value):
     return make_int(term.BigInt(rbigint.fromint(self.num).xor(other_value)))
Esempio n. 14
0
 def arith_shl_bigint(self, other_value):
     return make_int(term.BigInt(other_value.lshift(self.num)))
Esempio n. 15
0
 def arith_pow_bigint(self, other_value):
     return make_int(term.BigInt(other_value.pow(rbigint.fromint(self.num))))
Esempio n. 16
0
 def arith_and_bigint(self, other_value):
     return make_int(term.BigInt(other_value.and_(self.value)))
Esempio n. 17
0
 def arith_xor_number(self, other_num):
     return make_int(term.BigInt(rbigint.fromint(other_num).xor(self.value)))
Esempio n. 18
0
 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)
Esempio n. 19
0
 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")
Esempio n. 20
0
 def arith_sub_bigint(self, other_value):
     return make_int(term.BigInt(other_value.sub(self.value)))
Esempio n. 21
0
 def arith_abs(self):
     return make_int(term.BigInt(self.value.abs()))
Esempio n. 22
0
 def arith_unarysub(self):
     return term.BigInt(self.value.neg())
Esempio n. 23
0
 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))
Esempio n. 24
0
 def arith_mul_bigint(self, other_value):
     return make_int(term.BigInt(other_value.mul(self.value)))