From b5d383a4be25ea517fa932c29406166495725495 Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand Date: Sat, 13 Feb 2016 19:47:35 +0300 Subject: [PATCH] Parse float and create Explicable_float --- pymath/calculus/explicable.py | 20 ++++++++++++++++++++ pymath/calculus/expression.py | 6 +++++- pymath/calculus/str2tokens.py | 13 ++++++++++++- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/pymath/calculus/explicable.py b/pymath/calculus/explicable.py index 8f8b9e2..d97b918 100644 --- a/pymath/calculus/explicable.py +++ b/pymath/calculus/explicable.py @@ -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) + # ----------------------------- diff --git a/pymath/calculus/expression.py b/pymath/calculus/expression.py index 90c09cf..e8a0e26 100644 --- a/pymath/calculus/expression.py +++ b/pymath/calculus/expression.py @@ -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 diff --git a/pymath/calculus/str2tokens.py b/pymath/calculus/str2tokens.py index fd7713d..4456ea3 100644 --- a/pymath/calculus/str2tokens.py +++ b/pymath/calculus/str2tokens.py @@ -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))