-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRationals.py
More file actions
executable file
·96 lines (70 loc) · 1.56 KB
/
Rationals.py
File metadata and controls
executable file
·96 lines (70 loc) · 1.56 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 25 10:44:29 2021
@author: maherme
"""
#%%
from fractions import Fraction
# You can get some help typing help(Fraction) in the python console
# There are many ways to create a fraction number:
print(Fraction(1))
print(Fraction(denominator=1, numerator=2))
print(Fraction(2, 1))
print(Fraction(numerator=1, denominator=2))
print(Fraction(1, 2))
print(Fraction(0.125))
print(Fraction('0.125'))
print(Fraction(22/7))
#%%
# All arithmetic operators work:
x = Fraction(2, 3)
y = Fraction(3, 4)
print(x + y)
print(x * y)
print(x/y)
#%%
# Python simplify the fractions also:
print(Fraction(8, 16))
#%%
# The sign of a fraction is set in the numerator:
print(Fraction(1, -4))
x = Fraction(1, -4)
print(x.numerator)
print(x. denominator)
#%%
# Let's see precision in rational vs floats:
import math
x = Fraction(math.pi)
print(x)
print(float(x))
y = Fraction(math.sqrt(2))
print(y)
print(float(y))
#%%
a = 0.125
print(a)
b = 0.3
print(b)
print(Fraction(a))
print(Fraction(b)) # Notice the representation of 0.3
#%%
# Let's see 0.3 in more detail:
print(format(b, '0.5f'))
print(format(b, '0.15f'))
print(format(b, '0.25f')) # So 0.3 is not stored as 0.3 in the machine!
#%%
# We can limit the denominator value in the aproximation of the fraction:
x = Fraction(0.3)
print(x.limit_denominator(10))
#%%
# Let's see with pi number
x = Fraction(math.pi)
print(x)
print(float(x))
print(x.limit_denominator(10))
print(22/7)
print(x.limit_denominator(100))
print(x.limit_denominator(100_000))
print(312689/99532)
#%%