-
Notifications
You must be signed in to change notification settings - Fork 3
/
rationalNumber.py
24 lines (18 loc) · 1.04 KB
/
rationalNumber.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from fractions import Fraction
class CustomRational:
def __init__(self, numerator, denominator):
self.value = Fraction(numerator, denominator)
def __add__(self, other):
return CustomRational(self.value.numerator * other.value.denominator + other.value.numerator * self.value.denominator,
self.value.denominator * other.value.denominator)
def __sub__(self, other):
return CustomRational(self.value.numerator * other.value.denominator - other.value.numerator * self.value.denominator,
self.value.denominator * other.value.denominator)
def __mul__(self, other):
return CustomRational(self.value.numerator * other.value.numerator,
self.value.denominator * other.value.denominator)
def __truediv__(self, other):
return CustomRational(self.value.numerator * other.value.denominator,
self.value.denominator * other.value.numerator)
def __str__(self):
return str(self.value)