-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfraction.py
More file actions
46 lines (29 loc) · 1.04 KB
/
fraction.py
File metadata and controls
46 lines (29 loc) · 1.04 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
import functools
@functools.total_ordering
class Save:
def __init__(self, numerator, denominator, approx, delta):
self.numerator = numerator
self.denominator = denominator
self.approx = approx
self.delta = delta
def __lt__(self, other):
return self.delta < other.delta
def main():
target = float(input("Please input a decimal number to approximate:"))
list = []
numerator = 1
denominator = 1
approx = numerator / denominator
while denominator <= 1000 and numerator <= 1000:
approx = numerator / denominator
if approx < target:
numerator += 1
list.append(Save(numerator, denominator, approx, abs(approx - target)))
else:
denominator += 1
list.append(Save(numerator, denominator, approx, abs(approx - target)))
list.sort()
for i in range(0, 10):
print("{:} / {:} = {:.5f} = ".format(list[i].numerator, list[i].denominator, list[i].approx))
if __name__ == "__main__":
main()