Overload operators for MOnumbers in order to use gcd

This commit is contained in:
Bertrand Benjamin 2018-03-13 18:57:38 +03:00
parent 3c133250ec
commit d1c6e2d3da
3 changed files with 132 additions and 3 deletions

View File

@ -154,6 +154,128 @@ class MOnumber(MO):
return f"- {abs(self.value)}"
def __add__(self, other):
""" Adding a MOnumber """
try:
return self.value + other.value
except AttributeError:
return self.value + other
def __radd__(self, other):
""" rAdding a MOnumber """
try:
return self.value + other.value
except AttributeError:
return self.value + other
def __sub__(self, other):
""" Subing a MOnumber """
try:
return self.value - other.value
except AttributeError:
return self.value - other
def __rsub__(self, other):
""" rSubing a MOnumber """
try:
return self.value - other.value
except AttributeError:
return self.value - other
def __mul__(self, other):
""" Multiply a MOnumber """
try:
return self.value * other.value
except AttributeError:
return self.value * other
def __rmul__(self, other):
""" rMultiply a MOnumber """
try:
return self.value * other.value
except AttributeError:
return self.value * other
def __truediv__(self, other):
""" Divide a MOnumber """
try:
return self.value / other.value
except AttributeError:
return self.value / other
def __rtruediv__(self, other):
""" rDivide a MOnumber """
try:
return self.value / other.value
except AttributeError:
return self.value / other
def __floordiv__(self, other):
""" Integer Division a MOnumber """
try:
return self.value // other.value
except AttributeError:
return self.value // other
def __rfloordiv__(self, other):
""" rInteger Division a MOnumber """
try:
return other.value // self.value
except AttributeError:
return other // self.value
def __mod__(self, other):
""" Moduling a MOnumber """
try:
return self.value % other.value
except AttributeError:
return self.value % other
def __rmod__(self, other):
""" rModuling a MOnumber """
try:
return other.value % self.value
except AttributeError:
return other % self.value
def __eq__(self, other):
""" == a MOnumber """
try:
return self.value == other.value
except AttributeError:
return self.value == other
def __lt__(self, other):
""" < a MOnumber """
try:
return self.value < other.value
except AttributeError:
return self.value < other
def __le__(self, other):
""" <= a MOnumber """
try:
return self.value <= other.value
except AttributeError:
return self.value <= other
def __gt__(self, other):
""" > a MOnumber """
try:
return self.value > other.value
except AttributeError:
return self.value > other
def __ge__(self, other):
""" >= a MOnumber """
try:
return self.value >= other.value
except AttributeError:
return self.value >= other
def __hash__(self):
return self.value.__hash__()
class MOstr(MO):
""" Unknown math object like x or n"""

View File

@ -14,11 +14,13 @@ from .exceptions import ComputeError
from .add import add
from .minus import minus
from .multiply import multiply
from .divide import divide
OPERATIONS = {
"+": add,
"-": minus,
"*": multiply,
"/": divide,
}
def compute(node, left_v, right_v):

View File

@ -24,10 +24,15 @@ def gcd(a, b):
2
"""
pos_a, _a = (a >= 0), abs(a)
pos_b, _b = (b >= 0), abs(b)
try:
pos_a, _a = (a >= 0), abs(a)
pos_b, _b = (b >= 0), abs(b)
gcd_sgn = (-1 + 2 * (pos_a or pos_b))
gcd_sgn = (-1 + 2 * (pos_a or pos_b))
except TypeError:
_a = a
_b = b
gcd_sgn = 1
if _a > _b:
c = _a % _b