Parse float and create Explicable_float

This commit is contained in:
Benjamin Bertrand 2016-02-13 19:47:35 +03:00
parent 97d18be64b
commit b5d383a4be
3 changed files with 37 additions and 2 deletions

View File

@ -61,6 +61,7 @@ class Explicable(Renderable):
return False
class Explicable_int(int, Explicable):
""" An Explicable_int is an int which can be explain """
isNumber = True
def __init__(self, val):
@ -78,6 +79,25 @@ class Explicable_int(int, Explicable):
def __tex__(self):
return str(self._val)
class Explicable_float(float, Explicable):
""" An Explicable_float is an float which can be explain """
isNumber = True
def __init__(self, val):
super(Explicable_float, self).__init__(val)
self._val = val
self.postfix_tokens = [self]
self.steps = []
def simplify(self):
return Explicable_float(self._val)
def __txt__(self):
return str(self._val)
def __tex__(self):
return str(self._val)
# -----------------------------

View File

@ -7,7 +7,7 @@
from .generic import Stack, flatten_list, expand_list, isNumber, isOperator, isNumerand
from .str2tokens import str2tokens
from .operator import op
from .explicable import Explicable, Explicable_int
from .explicable import Explicable, Explicable_int, Explicable_float
from .random_expression import RdExpression
@ -109,6 +109,10 @@ class Expression(Explicable):
if isinstance(token, Explicable_int) or isinstance(token, int):
return Explicable_int(token)
# TODO: J'en arrive au soucis même soucis qu'avec les fractions qui une fois simplifiée devrait être des Explicable_int. Mais comment on ne redéfini pas les opérations, ce sont les opérations des int qui se font et donc on perd toute l'historique. |sam. févr. 13 18:57:45 EAT 2016
if isinstance(token, Explicable_float) or isinstance(token, float):
return Explicable_float(token)
elif hasattr(token, 'simplify') and hasattr(token, 'explain'):
ans = expression.postfix_tokens[0]
return ans

View File

@ -45,6 +45,13 @@ def str2in_tokens(exp):
else:
tokens[-1] = tokens[-1] * 10 - int(character)
elif isinstance(tokens[-1], float):
after_coma += 1
if tokens[-1] >= 0:
tokens[-1] = tokens[-1] + int(character) * 0.1 ** after_coma
else:
tokens[-1] = tokens[-1]- int(character) * 0.1 ** after_coma
# Special case for "-" at the begining of an expression or before
# "("
elif tokens[-1] == "-" and \
@ -77,7 +84,11 @@ def str2in_tokens(exp):
tokens.append(Polynom([0, 1], letter=character))
elif character == ".":
raise ValueError("No float number please")
if isinstance(tokens[-1], float):
raise ValueError("A number has 2 points...!")
else:
tokens[-1] = float(tokens[-1])
after_coma = 0
elif character != " ":
raise ValueError("{} is an unvalid character".format(character))