-
Notifications
You must be signed in to change notification settings - Fork 0
/
fraction.py
80 lines (64 loc) · 1.97 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
import copy
def gcd(x,y):
while(y!=0):
x,y = y,x%y
return x
class Fraction:
def __init__(self,num,den=1):
g = gcd(int(num),int(den))
g = 1 if g==0 else g
self.num = int(num)/g
self.den = int(den)/g
def inverse(self):
if self.num == 0:
return Fraction(0,1)
else:
return Fraction(self.den,self.num)
def invert(self):
if not self.num == 0:
self.num, self.den = self.den, self.num
def __eq__(self,other):
if isinstance(other,int):
return self.den == 1 and self.num == other
else:
return (self.num == other.num and self.den == other.den)
def __str__(self):
return self.__repr__()
def __repr__(self):
if (self.den == 1):
return str(self.num)
else:
return "\\frac{"+str(self.num)+"}{"+str(self.den)+"}"
def __add__(self,other):
if isinstance(other,int):
return Fraction( self.num + other*self.den, self.den )
else:
return Fraction( (self.num*other.den + other.num*self.den), self.den*other.den )
def __sub__(self,other):
return self.__add__( other*-1 )
#return Fraction( (self.num*other.den - other.num*self.den), self.den*other.den )
def __mul__(self,other):
if isinstance(other,int):
return Fraction( self.num * other, self.den )
else:
return Fraction( self.num*other.num, self.den*other.den )
def __div__(self,other):
if isinstance(other,int):
return self.__mul__(Fraction(1,other))
else:
return self.__mul__(other.inverse())
def __cmp__(self,other):
if (isinstance(other,int)):
other = Fraction(other)
if ( self.num*other.den < other.num*self.den ):
return -1
elif ( self.num*other.den > other.num*self.den ):
return 1
else:
return 0
def __float__(self):
return float(self.num)/float(self.den)
def __copy__(self):
return Fraction( self.num, self.den )
def __deepcopy__(self,memo=None):
return Fraction( copy.deepcopy(self.num), copy.deepcopy(self.den) )