Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def __add__(self, other): # enables +
- x = lcm(self.denom, other.denom)
- f1 = x // self.denom
- f2 = x // other.denom
- return Rational(self.number * f1 + other.numer * f2, x).reduce()
- def __sub__(self, other): # enables -
- x = lcm(self.denom, other.denom)
- f1 = x // self.denom
- f2 = x // other.denom
- return Rational(self.number * f1 - other.numer * f2, x).reduce()
- def __mul__(self, other): # enables *
- return Rational(other.numer * self.numer, other.denom * self.denom)
- def __truediv__(self, other): # enables /
- return Rational(self.numer * other.denom, self.denom * other.numer)
- def reduce(self):
- x = gcd(self.numer, self.denom)
- return Rational(self.numer // x, self.denom // x)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement