type filter for operations
This commit is contained in:
@@ -15,6 +15,7 @@ from ..MO.mo import MO, MOnumber
|
||||
from ..MO.fraction import MOFraction
|
||||
from .exceptions import AddError
|
||||
from .arithmetic import lcm
|
||||
from .type_filter import args_are
|
||||
|
||||
|
||||
def add(left, right):
|
||||
@@ -40,6 +41,7 @@ def add(left, right):
|
||||
|
||||
return ADDFUNCTIONS[(type(left), type(right))](left, right)
|
||||
|
||||
@args_are(MOnumber, MOnumber)
|
||||
def monumber_monumber(left, right):
|
||||
""" Adding 2 monumbers
|
||||
|
||||
@@ -55,6 +57,7 @@ def monumber_monumber(left, right):
|
||||
"""
|
||||
return MO.factory(left.value + right.value)
|
||||
|
||||
@args_are(MOnumber, MOFraction)
|
||||
def monumber_mofraction(left, right):
|
||||
""" Adding a monumber and a mofraction
|
||||
|
||||
@@ -72,14 +75,11 @@ def monumber_mofraction(left, right):
|
||||
> /
|
||||
| > 6
|
||||
| > 5
|
||||
|
||||
"""
|
||||
if not isinstance(left, MOnumber) or not isinstance(right, MOFraction):
|
||||
raise AddError(f"Wrong type for left (got {left.__class__.__name__}) \
|
||||
or right (got {right.__class__.__name__})")
|
||||
left_fraction = MOFraction(left, MOnumber(1))
|
||||
return Tree("+", left_fraction, right)
|
||||
|
||||
@args_are(MOFraction, MOnumber)
|
||||
def mofraction_monumber(left, right):
|
||||
""" Adding a monumber and a mofraction
|
||||
|
||||
@@ -98,14 +98,10 @@ def mofraction_monumber(left, right):
|
||||
| > 4
|
||||
| > 1
|
||||
"""
|
||||
|
||||
if not isinstance(left, MOFraction) or not isinstance(right, MOnumber):
|
||||
raise AddError(f"Wrong type for left (got {left.__class__.__name__})"
|
||||
f"or right (got {right.__class__.__name__})")
|
||||
|
||||
right_fraction = MOFraction(right, MOnumber(1))
|
||||
return Tree("+", left, right_fraction)
|
||||
|
||||
@args_are(MOFraction, MOFraction)
|
||||
def mofraction_mofraction(left, right):
|
||||
""" Adding two mofractions
|
||||
|
||||
@@ -174,11 +170,6 @@ def mofraction_mofraction(left, right):
|
||||
| | > 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)
|
||||
|
||||
Reference in New Issue
Block a user