-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFraction.py
96 lines (75 loc) · 2.48 KB
/
Fraction.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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# gdc(a,b) = gcd(b, a%b) OBS: Positive integer only
def gcd(m, n):
while(m % n != 0):
aux = m
aux2 = n
m = aux2
n = aux % aux2
return n
class Fraction(object):
""" Represents a fraction, which consists of two integer numbers:
The first one is called the numerator, and the second one is
the denominator.
To instantiate an object of this class you need to provide a
default numerator and denominator to the init method.
"""
def __init__(self, num, den):
if type(num) != int or type(den) != int:
raise RuntimeError("Only integers values supported for fraction!")
if den == 0:
raise RuntimeError("ERROR: denominator equals zero!")
if den < 0:
num *= -1
den *= -1
common = gcd(num, den)
self.num = num//common
self.den = den//common
# Overriding string conversion method
def __str__(self):
return str(self.num) + " / " + str(self.den)
# Overriding the arithmetic operation for Fractions
def __add__(self, other):
den = other.den * self.den
num = (self.num*den//self.den)+(other.num*den//other.den)
return Fraction(num, den)
def __sub__(self, other):
other.num *= -1
return self.__add__(other)
def __mul__(self, other):
num = self.num * other.num
den = self.den * other.den
return Fraction(num, den)
def __div__(self, other):
other.num, other.den = other.den, other.num
if other.den == 0:
raise RuntimeError("ERROR: denominator equals zero!")
return self.__mul__(other)
# Right way to check if two fractions are equal
def __eq__(self, other):
# Cross product
num1 = self.num*other.den
num2 = self.den*other.num
return num1 == num2
def __lt__(self, other):
return(self.num/self.den < other.num/other.den)
def __gt__(self, other):
return(other.__lt__(self))
def getNum(self):
return self.num
def getDen(self):
return self.den
def __truediv__(self, other):
divA = self.num / self.den
divB = other.num / other.den
return divA / divB
def __iadd__(self, other):
ret = self.__add__(other)
ret = Fraction(ret.num, ret.den)
self = ret
def main():
myf = Fraction(1, 2)
myf2 = Fraction(1, 2)
myf += myf2
print(myf+myf2)
if __name__ == '__main__':
main()