Add 2 fractions works!
This commit is contained in:
@@ -15,6 +15,7 @@ from ..operator import OPERATORS
|
||||
from ..MO.mo import MO, MOnumber, MOstr
|
||||
from ..MO.fraction import MOFraction
|
||||
from .exceptions import AddError
|
||||
from .arithmetic import lcm
|
||||
|
||||
|
||||
def add(left, right):
|
||||
@@ -106,11 +107,109 @@ def mofraction_monumber(left, right):
|
||||
right_fraction = MOFraction(right, MOnumber(1))
|
||||
return Tree("+", left, right_fraction)
|
||||
|
||||
def mofraction_mofraction(left, right):
|
||||
""" Adding two mofractions
|
||||
|
||||
:param left: a mofraction
|
||||
:param right: a mofraction
|
||||
:returns: Tree
|
||||
|
||||
>>> a = MOFraction(1, 5)
|
||||
>>> b = MOFraction(4, 5)
|
||||
>>> print(mofraction_mofraction(a, b))
|
||||
/
|
||||
> +
|
||||
| > 1
|
||||
| > 4
|
||||
> 5
|
||||
>>> a = MOFraction(1, 5, True)
|
||||
>>> b = MOFraction(4, 5)
|
||||
>>> print(mofraction_mofraction(a, b))
|
||||
/
|
||||
> +
|
||||
| > -
|
||||
| | > None
|
||||
| | > 1
|
||||
| > 4
|
||||
> 5
|
||||
>>> a = MOFraction(1, 5)
|
||||
>>> b = MOFraction(4, 5, True)
|
||||
>>> print(mofraction_mofraction(a, b))
|
||||
/
|
||||
> +
|
||||
| > 1
|
||||
| > -
|
||||
| | > None
|
||||
| | > 4
|
||||
> 5
|
||||
>>> a = MOFraction(1, 2)
|
||||
>>> b = MOFraction(1, 4)
|
||||
>>> print(mofraction_mofraction(a, b))
|
||||
+
|
||||
> /
|
||||
| > *
|
||||
| | > 1
|
||||
| | > 2
|
||||
| > *
|
||||
| | > 2
|
||||
| | > 2
|
||||
> /
|
||||
| > 1
|
||||
| > 4
|
||||
>>> a = MOFraction(1, 2)
|
||||
>>> b = MOFraction(1, 5)
|
||||
>>> print(mofraction_mofraction(a, b))
|
||||
+
|
||||
> /
|
||||
| > *
|
||||
| | > 1
|
||||
| | > 5
|
||||
| > *
|
||||
| | > 2
|
||||
| | > 5
|
||||
> /
|
||||
| > *
|
||||
| | > 1
|
||||
| | > 2
|
||||
| > *
|
||||
| | > 5
|
||||
| | > 2
|
||||
"""
|
||||
|
||||
if not isinstance(left, MOFraction) or not isinstance(right, MOFraction):
|
||||
raise AddError(f"Wrong type for left (got {left.__class__.__name__})"
|
||||
f"or right (got {right.__class__.__name__})")
|
||||
|
||||
if left.denominator == right.denominator:
|
||||
num = Tree("+", left.numerator, right.numerator)
|
||||
return Tree("/", num, left.denominator)
|
||||
|
||||
denom_lcm = lcm(left.denominator, right.denominator)
|
||||
|
||||
if left.denominator == denom_lcm:
|
||||
left_frac = left
|
||||
else:
|
||||
multiply_by = denom_lcm // left.denominator
|
||||
left_num = Tree("*", left.numerator, MO.factory(multiply_by))
|
||||
left_denom = Tree("*", left.denominator, MO.factory(multiply_by))
|
||||
left_frac = Tree("/", left_num, left_denom)
|
||||
|
||||
if right.denominator == denom_lcm:
|
||||
right_frac = right
|
||||
else:
|
||||
multiply_by = denom_lcm // right.denominator
|
||||
right_num = Tree("*", right.numerator, MO.factory(multiply_by))
|
||||
right_denom = Tree("*", right.denominator, MO.factory(multiply_by))
|
||||
right_frac = Tree("/", right_num, right_denom)
|
||||
|
||||
return Tree("+", left_frac, right_frac)
|
||||
|
||||
# TODO: Faire un décorateur pour un enregistrement automatique |dim. mars 11 18:24:32 EAT 2018
|
||||
ADDFUNCTIONS = {
|
||||
(MOnumber, MOnumber): monumber_monumber,
|
||||
(MOnumber, MOFraction): monumber_mofraction,
|
||||
(MOFraction, MOnumber): mofraction_monumber,
|
||||
(MOFraction, MOFraction): mofraction_mofraction,
|
||||
}
|
||||
|
||||
# -----------------------------
|
||||
|
||||
Reference in New Issue
Block a user