-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfraction.py
More file actions
286 lines (196 loc) · 11.3 KB
/
fraction.py
File metadata and controls
286 lines (196 loc) · 11.3 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
from .operations import gcd, scm
class Fraction():
"""
A class for representing fractions as numerator and denominator
"""
__slots__ = ["numerator", "denominator"]
__available_types = ["int", "float", "Polinominal", "Fraction"]
def __new__(cls, numerator, denominator=1):
if type(numerator).__name__ not in cls.__available_types and type(denominator) == cls.__available_types:
raise TypeError
if numerator == 0:
return numerator
instance = super(Fraction, cls).__new__(cls)
if type(numerator) == int and type(denominator) == int:
common_divisor = gcd(numerator, denominator)
instance.numerator = numerator // common_divisor
instance.denominator = denominator // common_divisor
return instance
elif type(numerator).__name__ in ["int", "Polinominal"] and type(denominator).__name__ in ["int", "Polinominal"]:
common_divisor = gcd(numerator, denominator)
numerator = numerator / common_divisor
denominator = denominator / common_divisor
k = 1
p = 0
if type(numerator).__name__ == "Polinominal":
for num in numerator.coefficients:
if type(numerator.coefficients[num]) == Fraction:
k = scm(k, numerator.coefficients[num].denominator)
p = gcd(p, int(numerator.coefficients[num].numerator))
elif type(numerator.coefficients[num]) in [int, float] and\
int(numerator.coefficients[num]) == numerator.coefficients[num]:
p = gcd(p, int(numerator.coefficients[num]))
if type(denominator).__name__ == "Polinominal":
for num in denominator.coefficients:
if type(denominator.coefficients[num]) == Fraction:
k = scm(k, denominator.coefficients[num].denominator)
p = gcd(p, int(denominator.coefficients[num].numerator))
elif type(denominator.coefficients[num]) in [int, float] and\
int(denominator.coefficients[num]) == denominator.coefficients[num]:
p = gcd(p, int(denominator.coefficients[num]))
instance.numerator = numerator * abs(k) / abs(p)
instance.denominator = denominator * abs(k) / abs(p)
return instance
elif type(numerator) == Fraction or type(denominator) == Fraction:
return numerator / denominator
else:
instance.numerator = Fraction.toFration(numerator).numerator
instance.denominator = Fraction.toFration(numerator).denominator
return instance
@staticmethod
def toFration(number: float|int) -> "Fraction":
"""
A function for finding the numerator and denominator that makes a given number
"""
if type(number) not in [int, float]:
raise TypeError("can only convert 'float' or 'int' to Fraction with this function")
if type(number) == int:
return number
numerator, denominator = 1, 1
sign = -1 if (number < 0) else 1
number = abs(number)
while abs(number - numerator / denominator) > 10e-20:
if numerator > 10**3 or denominator > 10**3:
raise Exception("number cannot be converted :(")
if numerator / denominator < number:
numerator += 1
else:
denominator += 1
return Fraction(sign * numerator, denominator)
def __add__(self, other) -> "Fraction":
"""'Fraction' + other"""
if type(other).__name__ not in Fraction.__available_types:
raise TypeError(f"unsupported operand type(s) for +: '{type(other).__name__}' and 'Fraction'")
elif type(other) == float:
try:
other = self.toFration(other)
except Exception:
return other + self.numerator / self.denominator
if type(other) == Fraction:
if "Polinominal" in [type(self.numerator).__name__, type(self.denominator).__name__,
type(other.numerator).__name__, type(other.denominator).__name__]:
return (self.numerator * other.denominator + self.denominator * other.numerator) / (self.denominator * other.denominator)
return Fraction(self.numerator * other.denominator + self.denominator * other.numerator, self.denominator * other.denominator)
__radd__ = __add__
def __sub__(self, other):
"""'Fraction' - other"""
if type(other).__name__ not in Fraction.__available_types:
raise TypeError(f"unsupported operand type(s) for -: 'Fraction' and '{type(other).__name__}'")
elif type(other) == float:
try:
other = self.toFration(other)
except Exception:
return self.numerator / self.denominator - other
if type(other) == Fraction:
if "Polinominal" in [type(self.numerator).__name__, type(self.denominator).__name__,
type(other.numerator).__name__, type(other.denominator).__name__]:
a = (self.numerator * other.denominator - self.denominator * other.numerator) / (self.denominator * other.denominator)
return a
return Fraction(self.numerator * other.denominator - self.denominator * other.numerator, self.denominator * other.denominator)
def __rsub__(self, other) -> "Fraction":
"""other - 'Fraction'"""
if type(other).__name__ not in Fraction.__available_types:
raise TypeError(f"unsupported operand type(s) for -: '{type(other).__name__}' and 'Fraction'")
elif type(other) == float:
try:
other = self.toFration(other)
except Exception:
return other - self.numerator / self.denominator
if type(other) == Fraction:
if "Polinominal" in [type(self.numerator).__name__, type(self.denominator).__name__,
type(other.numerator).__name__, type(other.denominator).__name__]:
return (self.denominator * other.numerator - self.numerator * other.denominator) / (self.denominator * other.denominator)
return Fraction(self.denominator * other.numerator - self.numerator * other.denominator, self.denominator * other.denominator)
def __mul__(self, other) -> "Fraction":
"""'Fraction' * other"""
if type(other).__name__ not in Fraction.__available_types:
raise TypeError(f"unsupported operand type(s) for *: '{type(other).__name__}' and 'Fraction'")
elif type(other) == float:
try:
other = self.toFration(other)
except Exception:
return other * self.numerator / self.denominator
if type(other) == Fraction:
if "Polinominal" in [type(self.numerator).__name__, type(self.denominator).__name__,
type(other.numerator).__name__, type(other.denominator).__name__]:
return (self.numerator * other.numerator) / (self.denominator * other.denominator)
elif type(other).__name__ == "Polinominal":
return other * self
return Fraction(self.numerator * other.numerator, self.denominator * other.denominator)
__rmul__ = __mul__
def __truediv__(self, other) -> "Fraction":
"""'Fraction' / other"""
if type(other).__name__ not in Fraction.__available_types:
raise TypeError(f"unsupported operand type(s) for /: '{type(other).__name__}' and 'Fraction'")
elif type(other) == float:
try:
other = self.toFration(other)
except Exception:
return self.numerator / self.denominator / other
if type(other) == Fraction:
if "Polinominal" in [type(self.numerator).__name__, type(self.denominator).__name__,
type(other.numerator).__name__, type(other.denominator).__name__]:
return (self.numerator * other.denominator) / (self.denominator * other.numerator)
return Fraction(self.numerator * other.denominator, self.denominator * other.numerator)
def __rtruediv__(self, other) -> "Fraction":
"""other / 'Fraction'"""
if type(other).__name__ not in Fraction.__available_types:
raise TypeError(f"unsupported operand type(s) for /: 'Fraction' and '{type(other).__name__}'")
elif type(other) == float:
try:
other = self.toFration(other)
except Exception:
return other / self.numerator * self.denominator
if type(other) == Fraction:
if "Polinominal" in [type(self.numerator).__name__, type(self.denominator).__name__,
type(other.numerator).__name__, type(other.denominator).__name__]:
return (self.denominator * other.numerator) / (self.numerator * other.numerator)
return Fraction(other.numerator * self.denominator, other.denominator * self.numerator)
def __neg__(self) -> "Fraction":
"""-'Fraction'"""
return Fraction(-self.numerator, self.denominator)
def __pow__(self, other) -> "Fraction":
"""'Fraction' ** other"""
if type(other).__name__ not in Fraction.__available_types:
raise TypeError(f"unsupported operand type(s) for **: 'Fraction' and '{type(other).__name__}'")
return Fraction(self.numerator**other, self.denominator**other)
def __float__(self) -> float:
return self.numerator / self.denominator
def __int__(self) -> int:
return self.numerator // self.denominator
def __eq__(self, other) -> bool: # self == other
return abs(float(self) - float(other)) < 10**(-20)
def __lt__(self, other) -> bool: # self < other
return (float(self) - float(other)) < 0
def __le__(self, other) -> bool: # self <= other
return (float(self) - float(other)) <= 0
def __gt__(self, other) -> bool: # self > other
return (float(self) - float(other)) > 0
def __ge__(self, other) -> bool: # self >= other
return (float(self) - float(other)) >= 0
def __abs__(self) -> "Fraction": # abs( self )
return Fraction(abs(self.numerator), abs(self.denominator))
def __iadd__(self, other) -> "Fraction":
return self + other
def __isub__(self, other) -> "Fraction":
return self - other
def __imul__(self, other) -> "Fraction":
return self * other
def __itruediv__(self, other) -> "Fraction":
return self / other
def __repr__(self) -> str:
if self.numerator == 0 or self.denominator == 1:
return str(self.numerator)
elif type(self.numerator).__name__ == "Polinominal" or type(self.denominator).__name__ == "Polinominal":
return f"({self.numerator})/({self.denominator})"
return f"{self.numerator}/{self.denominator}"