-
Notifications
You must be signed in to change notification settings - Fork 1
/
ratio.py
52 lines (38 loc) · 1.31 KB
/
ratio.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
import math
class Fraction:
def __init__(self, numerator, denominator):
self.num = numerator
self.den = denominator
def __str__(self):
return f'{self.num}/{self.den}'
def multiply(self, c):
return Fraction(self.num * c, self.den * c)
def simplify(self):
i: int = 2
while i <= min(self.num, self.den):
if self.num % i == 0 and self.den % i == 0:
return Fraction(self.num // i, self.den // i)
else:
i += 1
return self
def opposite(self):
return Fraction(self.num * -1, self.den)
def reciprocal(self):
return Fraction(self.den, self.num)
def sqrt(self):
return Fraction(math.sqrt(self.num), math.sqrt(self.den)).simplify()
def eval(self):
return self.num / self.den
class RMath:
@classmethod
def add(self, a: Fraction, b: Fraction):
return Fraction(a.num * b.den + b.num * a.den, a.den * b.den)
@classmethod
def subtract(self, a: Fraction, b: Fraction):
return self.add(a, b.opposite())
@classmethod
def multiply(self, a: Fraction, b: Fraction):
return Fraction(a.num * b.num, a.den * b.den)
@classmethod
def divide(self, a: Fraction, b: Fraction):
return self.multiply(a, b.reciprocal())