no more float ready for decimal!
This commit is contained in:
parent
ab8b80758c
commit
821810ca47
@ -2,6 +2,7 @@
|
|||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
|
|
||||||
from .render import Renderable
|
from .render import Renderable
|
||||||
|
from decimal import Decimal
|
||||||
|
|
||||||
|
|
||||||
class Explicable(Renderable):
|
class Explicable(Renderable):
|
||||||
@ -79,18 +80,18 @@ class Explicable_int(int, Explicable):
|
|||||||
def __tex__(self):
|
def __tex__(self):
|
||||||
return str(self._val)
|
return str(self._val)
|
||||||
|
|
||||||
class Explicable_float(float, Explicable):
|
class Explicable_Decimal(Decimal, Explicable):
|
||||||
""" An Explicable_float is an float which can be explain """
|
""" An Explicable_Decimal is an decimal which can be explain """
|
||||||
isNumber = True
|
isNumber = True
|
||||||
|
|
||||||
def __init__(self, val):
|
def __init__(self, val):
|
||||||
super(Explicable_float, self).__init__(val)
|
super(Explicable_Decimal, self).__init__(val)
|
||||||
self._val = val
|
self._val = val
|
||||||
self.postfix_tokens = [self]
|
self.postfix_tokens = [self]
|
||||||
self.steps = []
|
self.steps = []
|
||||||
|
|
||||||
def simplify(self):
|
def simplify(self):
|
||||||
return Explicable_float(self._val)
|
return Explicable_Decimal(self._val)
|
||||||
|
|
||||||
def __txt__(self):
|
def __txt__(self):
|
||||||
return str(self._val)
|
return str(self._val)
|
||||||
|
@ -7,7 +7,8 @@
|
|||||||
from .generic import Stack, flatten_list, expand_list, isNumber, isOperator, isNumerand
|
from .generic import Stack, flatten_list, expand_list, isNumber, isOperator, isNumerand
|
||||||
from .str2tokens import str2tokens
|
from .str2tokens import str2tokens
|
||||||
from .operator import op
|
from .operator import op
|
||||||
from .explicable import Explicable, Explicable_int, Explicable_float
|
from .explicable import Explicable, Explicable_int, Explicable_Decimal
|
||||||
|
from decimal import Decimal
|
||||||
|
|
||||||
from .random_expression import RdExpression
|
from .random_expression import RdExpression
|
||||||
|
|
||||||
@ -28,9 +29,9 @@ def pstf_factory(pstf_tokens):
|
|||||||
>>> type(pstf_factory([2]))
|
>>> type(pstf_factory([2]))
|
||||||
<class 'pymath.calculus.explicable.Explicable_int'>
|
<class 'pymath.calculus.explicable.Explicable_int'>
|
||||||
>>> pstf_factory([2.45])
|
>>> pstf_factory([2.45])
|
||||||
2.45
|
Decimal('2.45')
|
||||||
>>> type(pstf_factory([2.45]))
|
>>> type(pstf_factory([2.45]))
|
||||||
<class 'pymath.calculus.explicable.Explicable_float'>
|
<class 'pymath.calculus.explicable.Explicable_Decimal'>
|
||||||
>>> from .fraction import Fraction
|
>>> from .fraction import Fraction
|
||||||
>>> f = Fraction(1,2)
|
>>> f = Fraction(1,2)
|
||||||
>>> pstf_factory([f])
|
>>> pstf_factory([f])
|
||||||
@ -42,8 +43,10 @@ def pstf_factory(pstf_tokens):
|
|||||||
except TypeError:
|
except TypeError:
|
||||||
if isinstance(pstf_tokens[0], int):
|
if isinstance(pstf_tokens[0], int):
|
||||||
return Explicable_int(pstf_tokens[0])
|
return Explicable_int(pstf_tokens[0])
|
||||||
|
elif isinstance(pstf_tokens[0], Decimal):
|
||||||
|
return Explicable_Decimal(pstf_tokens[0])
|
||||||
elif isinstance(pstf_tokens[0], float):
|
elif isinstance(pstf_tokens[0], float):
|
||||||
return Explicable_float(pstf_tokens[0])
|
return Explicable_Decimal(Decimal(str(pstf_tokens[0])))
|
||||||
elif hasattr(pstf_tokens[0], 'STR_RENDER'):
|
elif hasattr(pstf_tokens[0], 'STR_RENDER'):
|
||||||
return pstf_tokens[0]
|
return pstf_tokens[0]
|
||||||
else:
|
else:
|
||||||
@ -52,8 +55,10 @@ def pstf_factory(pstf_tokens):
|
|||||||
if l_pstf_token == 1:
|
if l_pstf_token == 1:
|
||||||
if isinstance(pstf_tokens[0], int):
|
if isinstance(pstf_tokens[0], int):
|
||||||
return Explicable_int(pstf_tokens[0])
|
return Explicable_int(pstf_tokens[0])
|
||||||
|
elif isinstance(pstf_tokens[0], Decimal):
|
||||||
|
return Explicable_Decimal(pstf_tokens[0])
|
||||||
elif isinstance(pstf_tokens[0], float):
|
elif isinstance(pstf_tokens[0], float):
|
||||||
return Explicable_float(pstf_tokens[0])
|
return Explicable_Decimal(Decimal(str(pstf_tokens[0])))
|
||||||
elif hasattr(pstf_tokens[0], 'STR_RENDER'):
|
elif hasattr(pstf_tokens[0], 'STR_RENDER'):
|
||||||
return pstf_tokens[0]
|
return pstf_tokens[0]
|
||||||
else:
|
else:
|
||||||
@ -151,32 +156,6 @@ class Expression(Explicable):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
# if len(expression.postfix_tokens) == 1:
|
|
||||||
# token = expression.postfix_tokens[0]
|
|
||||||
|
|
||||||
# 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
|
|
||||||
|
|
||||||
# elif isinstance(token, str):
|
|
||||||
# from .polynom import Polynom
|
|
||||||
# return Polynom([0,1], letter = token)
|
|
||||||
|
|
||||||
# else:
|
|
||||||
# raise ValueError(
|
|
||||||
# "Unknow token type in Expression: {}".format(
|
|
||||||
# type(token)))
|
|
||||||
|
|
||||||
# else:
|
|
||||||
# expression._isExpression = 1
|
|
||||||
# return expression
|
|
||||||
expression._isExpression = 1
|
expression._isExpression = 1
|
||||||
return expression
|
return expression
|
||||||
|
|
||||||
@ -202,8 +181,8 @@ class Expression(Explicable):
|
|||||||
except AttributeError:
|
except AttributeError:
|
||||||
if isinstance(self.postfix_tokens[0],int):
|
if isinstance(self.postfix_tokens[0],int):
|
||||||
self.simplified = Explicable_int(self.postfix_tokens[0])
|
self.simplified = Explicable_int(self.postfix_tokens[0])
|
||||||
elif isinstance(self.postfix_tokens[0],float):
|
elif isinstance(self.postfix_tokens[0],Decimal):
|
||||||
self.simplified = Explicable_float(self.postfix_tokens[0])
|
self.simplified = Explicable_Decimal(self.postfix_tokens[0])
|
||||||
else:
|
else:
|
||||||
self.simplified = self
|
self.simplified = self
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
from .generic import Stack, isOperator, isNumber, isPolynom
|
from .generic import Stack, isOperator, isNumber, isPolynom
|
||||||
from .operator import op
|
from .operator import op
|
||||||
|
from decimal import Decimal
|
||||||
|
|
||||||
|
|
||||||
def str2tokens(exp):
|
def str2tokens(exp):
|
||||||
@ -45,12 +46,12 @@ def str2in_tokens(exp):
|
|||||||
else:
|
else:
|
||||||
tokens[-1] = tokens[-1] * 10 - int(character)
|
tokens[-1] = tokens[-1] * 10 - int(character)
|
||||||
|
|
||||||
elif isinstance(tokens[-1], float):
|
elif isinstance(tokens[-1], Decimal):
|
||||||
after_coma += 1
|
after_coma += 1
|
||||||
if tokens[-1] >= 0:
|
if tokens[-1] >= 0:
|
||||||
tokens[-1] = tokens[-1] + int(character) * 0.1 ** after_coma
|
tokens[-1] = tokens[-1] + int(character) * Decimal('0.1') ** after_coma
|
||||||
else:
|
else:
|
||||||
tokens[-1] = tokens[-1]- int(character) * 0.1 ** after_coma
|
tokens[-1] = tokens[-1]- int(character) * Decimal('0.1') ** after_coma
|
||||||
|
|
||||||
# Special case for "-" at the begining of an expression or before
|
# Special case for "-" at the begining of an expression or before
|
||||||
# "("
|
# "("
|
||||||
@ -84,10 +85,10 @@ def str2in_tokens(exp):
|
|||||||
tokens.append(Polynom([0, 1], letter=character))
|
tokens.append(Polynom([0, 1], letter=character))
|
||||||
|
|
||||||
elif character == ".":
|
elif character == ".":
|
||||||
if isinstance(tokens[-1], float):
|
if isinstance(tokens[-1], Decimal):
|
||||||
raise ValueError("A number has 2 points...!")
|
raise ValueError("A number has 2 points...!")
|
||||||
else:
|
else:
|
||||||
tokens[-1] = float(tokens[-1])
|
tokens[-1] = Decimal(tokens[-1])
|
||||||
after_coma = 0
|
after_coma = 0
|
||||||
|
|
||||||
elif character != " ":
|
elif character != " ":
|
||||||
|
@ -18,8 +18,9 @@ class TestStr2tokens(unittest.TestCase):
|
|||||||
ans = str2in_tokens("2*3+4")
|
ans = str2in_tokens("2*3+4")
|
||||||
self.assertEqual(ans, [2, "*", 3, "+", 4])
|
self.assertEqual(ans, [2, "*", 3, "+", 4])
|
||||||
|
|
||||||
|
from decimal import Decimal
|
||||||
ans = str2in_tokens("2.34")
|
ans = str2in_tokens("2.34")
|
||||||
self.assertEqual(ans, [2.34])
|
self.assertEqual(ans, [Decimal('2.34')])
|
||||||
|
|
||||||
def test_in2post_fix(self):
|
def test_in2post_fix(self):
|
||||||
in_tokens = str2in_tokens("2+3*4")
|
in_tokens = str2in_tokens("2+3*4")
|
||||||
|
Loading…
Reference in New Issue
Block a user