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