Fix(Fraction): debug successive division (1 to MOnumber(1))

This commit is contained in:
Bertrand Benjamin 2018-10-05 11:18:09 +02:00
parent 2489eccb74
commit 94c117151d
2 changed files with 19 additions and 3 deletions

View File

@ -174,6 +174,22 @@ class Expression(object):
10 + 1680 * 9 10 + 1680 * 9
10 + 15120 10 + 15120
15130 15130
>>> e = Expression.from_str("1+2/3/4/5")
>>> f = e.simplify()
>>> for s in f.explain():
... print(s)
1 + 2 / 3 / 4 / 5
1 + (2 / 3 * 1 / 4) / 5
1 + (2 * 1) / (3 * 4) / 5
1 + 2 / 12 / 5
1 + 2 / 12 * 1 / 5
1 + (2 * 1) / (12 * 5)
1 + 2 / 60
1 / 1 + 2 / 60
(1 * 60) / (1 * 60) + 2 / 60
60 / 60 + 2 / 60
(60 + 2) / 60
62 / 60
""" """
try: try:
yield from self._ancestor.explain() yield from self._ancestor.explain()

View File

@ -70,7 +70,7 @@ def monumber_monumber(left, right):
>>> a = MOnumber(4) >>> a = MOnumber(4)
>>> b = MOnumber(6.2) >>> b = MOnumber(6.2)
>>> monumber_monumber(a, b) >>> monumber_monumber(a, b)
Decimal('0.6451612903225806266768278939') <MOnumber 0.6451612903225806266768278939>
>>> a = MOnumber(4) >>> a = MOnumber(4)
>>> b = MOnumber(6) >>> b = MOnumber(6)
>>> monumber_monumber(a, b) >>> monumber_monumber(a, b)
@ -80,7 +80,7 @@ def monumber_monumber(left, right):
""" """
if type(left.value) in [float, Decimal] or \ if type(left.value) in [float, Decimal] or \
type(right.value) in [float, Decimal]: type(right.value) in [float, Decimal]:
return left / right return MO.factory(left / right)
else: else:
raise NotImplementedError("Can't divide 2 int. Need to create a Fraction instead") raise NotImplementedError("Can't divide 2 int. Need to create a Fraction instead")
@ -126,7 +126,7 @@ def mofraction_monumber(left, right):
| > 4 | > 4
""" """
right_fraction = MOFraction(1, right) right_fraction = MOFraction(MOnumber(1), right)
return Tree("*", left, right_fraction) return Tree("*", left, right_fraction)
@divide.register(MOFraction, MOFraction) @divide.register(MOFraction, MOFraction)