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_max_float(self, other_float): return term.Float(max(other_float, float(self.num)))
def arith_pow_float(self, other_float): return term.Float(math.pow(other_float, self.value.tofloat()))
def arith_mul_float(self, other_float): return term.Float(other_float * self.value.tofloat())
def arith_add_float(self, other_float): return term.Float(other_float + self.value.tofloat())
def arith_min_float(self, other_float): return term.Float(min(other_float, self.floatval))
def arith_min_number(self, other_num): return term.Float(min(float(other_num), self.floatval))
def arith_abs(self): return term.Float(abs(self.floatval))
def arith_sub_float(self, other_float): return term.Float(other_float - self.floatval)
def arith_sub_bigint(self, other_value): return term.Float(other_value.tofloat() - self.floatval)
def arith_sub_number(self, other_num): return term.Float(float(other_num) - self.floatval)
def arith_add_float(self, other_float): return term.Float(other_float + self.floatval)
def arith_add_bigint(self, other_value): return term.Float(other_value.tofloat() + self.floatval)
def arith_add_number(self, other_num): return term.Float(float(other_num) + self.floatval)
def arith_min_float(self, other_float): return term.Float(min(other_float, float(self.num)))
def arith_pow_bigint(self, other_value): return term.Float(math.pow(other_value.tofloat(), self.floatval))
def arith_pow_float(self, other_float): return term.Float(math.pow(other_float, self.floatval))
def arith_unarysub(self): return term.Float(-self.floatval)
def arith_max_float(self, other_float): return term.Float(max(other_float, self.floatval))
def arith_mul_number(self, other_num): return term.Float(float(other_num) * self.floatval)
def arith_min_bigint(self, other_value): return term.Float(min(other_value.tofloat(), self.floatval))
def arith_mul_bigint(self, other_value): return term.Float(other_value.tofloat() * self.floatval)
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_mul_float(self, other_float): return term.Float(other_float * self.floatval)
def arith_sub_float(self, other_float): return term.Float(other_float - self.value.tofloat())
def arith_div_bigint(self, other_value): if self.floatval == 0.0: error.throw_evaluation_error("zero_divisor") return term.Float(other_value.tofloat() / self.floatval)
def arith_div_float(self, other_float): return term.Float(other_float / self.value.tofloat())
def arith_div_float(self, other_float): if self.floatval == 0.0: error.throw_evaluation_error("zero_divisor") return term.Float(other_float / self.floatval)
def arith_min_float(self, other_float): return term.Float(min(other_float, self.value.tofloat()))
def arith_pow_number(self, other_num): return term.Float(math.pow(float(other_num), self.floatval))