Beispiel #1
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")
Beispiel #2
0
 def arith_max_float(self, other_float):
     return term.Float(max(other_float, float(self.num)))
Beispiel #3
0
 def arith_pow_float(self, other_float):
     return term.Float(math.pow(other_float, self.value.tofloat()))
Beispiel #4
0
 def arith_mul_float(self, other_float):
     return term.Float(other_float * self.value.tofloat())
Beispiel #5
0
 def arith_add_float(self, other_float):
     return term.Float(other_float + self.value.tofloat())
Beispiel #6
0
 def arith_min_float(self, other_float):
     return term.Float(min(other_float, self.floatval))
Beispiel #7
0
 def arith_min_number(self, other_num):
     return term.Float(min(float(other_num), self.floatval))
Beispiel #8
0
 def arith_abs(self):
     return term.Float(abs(self.floatval))
Beispiel #9
0
 def arith_sub_float(self, other_float):
     return term.Float(other_float - self.floatval)
Beispiel #10
0
 def arith_sub_bigint(self, other_value):
     return term.Float(other_value.tofloat() - self.floatval)
Beispiel #11
0
 def arith_sub_number(self, other_num):
     return term.Float(float(other_num) - self.floatval)
Beispiel #12
0
 def arith_add_float(self, other_float):
     return term.Float(other_float + self.floatval)
Beispiel #13
0
 def arith_add_bigint(self, other_value):
     return term.Float(other_value.tofloat() + self.floatval)
Beispiel #14
0
 def arith_add_number(self, other_num):
     return term.Float(float(other_num) + self.floatval)
Beispiel #15
0
 def arith_min_float(self, other_float):
     return term.Float(min(other_float, float(self.num)))
Beispiel #16
0
 def arith_pow_bigint(self, other_value):
     return term.Float(math.pow(other_value.tofloat(), self.floatval))
Beispiel #17
0
 def arith_pow_float(self, other_float):
     return term.Float(math.pow(other_float, self.floatval))
Beispiel #18
0
 def arith_unarysub(self):
     return term.Float(-self.floatval)
Beispiel #19
0
 def arith_max_float(self, other_float):
     return term.Float(max(other_float, self.floatval))
Beispiel #20
0
 def arith_mul_number(self, other_num):
     return term.Float(float(other_num) * self.floatval)
Beispiel #21
0
 def arith_min_bigint(self, other_value):
     return term.Float(min(other_value.tofloat(), self.floatval))
Beispiel #22
0
 def arith_mul_bigint(self, other_value):
     return term.Float(other_value.tofloat() * self.floatval)
Beispiel #23
0
 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))
Beispiel #24
0
 def arith_mul_float(self, other_float):
     return term.Float(other_float * self.floatval)
Beispiel #25
0
 def arith_sub_float(self, other_float):
     return term.Float(other_float - self.value.tofloat())
Beispiel #26
0
 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)
Beispiel #27
0
 def arith_div_float(self, other_float):
     return term.Float(other_float / self.value.tofloat())
Beispiel #28
0
 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)
Beispiel #29
0
 def arith_min_float(self, other_float):
     return term.Float(min(other_float, self.value.tofloat()))
Beispiel #30
0
 def arith_pow_number(self, other_num):
     return term.Float(math.pow(float(other_num), self.floatval))