Skip to content

Instantly share code, notes, and snippets.

@chapmanjacobd
Created July 30, 2024 07:06
Show Gist options
  • Save chapmanjacobd/a9561fd283f8bdbeb94398d436318abf to your computer and use it in GitHub Desktop.
Save chapmanjacobd/a9561fd283f8bdbeb94398d436318abf to your computer and use it in GitHub Desktop.
from fractions import Fraction
def f(y, z):
return 108 - (815 - 1500 / z) / y
# Initialize exact values using Fraction for exact arithmetic
xExact = {0: Fraction(4), 1: Fraction(17, 4)}
# Initialize floating point values
xFloat = {0: 4.0, 1: 4.25}
for n in range(2, 101):
xExact[n] = f(xExact[n-1], xExact[n-2])
xFloat[n] = f(xFloat[n-1], xFloat[n-2])
table = []
for i in range(101):
table.append([i, float(xExact[i]), round(xFloat[i], 20)])
# Print the table with headers
headers = ["i", "x[i] \"exact\"", "x[i] floating point"]
print(f"{headers[0]:<5} {headers[1]:<25} {headers[2]:<25}")
for row in table:
print(f"{row[0]:<5} {row[1]:<25} {row[2]:<25}")
@chapmanjacobd
Copy link
Author

i     x[i] "exact"              x[i] floating point      
0     4.0                       4.0                      
1     4.25                      4.25                     
2     4.470588235294118         4.470588235294116        
3     4.644736842105263         4.6447368421052175       
4     4.770538243626063         4.770538243625083        
5     4.855700712589074         4.855700712568563        
6     4.910847499082793         4.91084749866063         
7     4.945537404123916         4.945537395530508        
8     4.966962581762701         4.966962408040999        
9     4.980045701355631         4.980042204293014        
10    4.987979448478392         4.987909232795786        
11    4.992770288062068         4.991362641314552        
12    4.995655891506634         4.967455095552268        
13    4.997391268381344         4.42969049830883         
14    4.998433943944817         -7.817236578459315       
15    4.999060071970894         168.93916767106458       
16    4.999435937146839         102.03996315205927       
17    4.999661524103767         100.0999475162497        
18    4.9997969007134175        100.00499204097244       
19    4.999878135477931         100.0002495792373        
20    4.9999268795046           100.00001247862016       
21    4.999956127061158         100.00000062392161       
22    4.9999736760057125        100.0000000311958        
23    4.999984205520272         100.00000000155978       
24    4.999990523282228         100.00000000007799       
25    4.99999431395856          100.0000000000039        
26    4.999996588371256         100.0000000000002        
27    4.9999979530213565        100.00000000000001       
28    4.999998771812312         100.0                    
29    4.999999263087206         100.0                    
30    4.9999995578522585        100.0                    
31    4.999999734711332         100.0                    
32    4.99999984082679          100.0                    
33    4.999999904496072         100.0                    
34    4.999999942697642         100.0                    
35    4.999999965618585         100.0                    
36    4.99999997937115          100.0                    
37    4.99999998762269          100.0                    
38    4.999999992573614         100.0                    
39    4.999999995544169         100.0                    
40    4.999999997326501         100.0                    
41    4.999999998395901         100.0                    
42    4.9999999990375406        100.0                    
43    4.999999999422524         100.0                    
44    4.9999999996535145        100.0                    
45    4.9999999997921085        100.0                    
46    4.999999999875265         100.0                    
47    4.999999999925159         100.0                    
48    4.9999999999550955        100.0                    
49    4.999999999973057         100.0                    
50    4.999999999983834         100.0                    
51    4.9999999999903           100.0                    
52    4.999999999994181         100.0                    
53    4.999999999996509         100.0                    
54    4.999999999997905         100.0                    
55    4.999999999998743         100.0                    
56    4.999999999999246         100.0                    
57    4.999999999999547         100.0                    
58    4.999999999999728         100.0                    
59    4.9999999999998375        100.0                    
60    4.999999999999902         100.0                    
61    4.999999999999941         100.0                    
62    4.9999999999999645        100.0                    
63    4.999999999999979         100.0                    
64    4.999999999999988         100.0                    
65    4.999999999999992         100.0                    
66    4.999999999999996         100.0                    
67    4.999999999999997         100.0                    
68    4.999999999999998         100.0                    
69    4.999999999999999         100.0                    
70    4.999999999999999         100.0                    
71    5.0                       100.0                    
72    5.0                       100.0                    
73    5.0                       100.0                    
74    5.0                       100.0                    
75    5.0                       100.0                    
76    5.0                       100.0                    
77    5.0                       100.0                    
78    5.0                       100.0                    
79    5.0                       100.0                    
80    5.0                       100.0                    
81    5.0                       100.0                    
82    5.0                       100.0                    
83    5.0                       100.0                    
84    5.0                       100.0                    
85    5.0                       100.0                    
86    5.0                       100.0                    
87    5.0                       100.0                    
88    5.0                       100.0                    
89    5.0                       100.0                    
90    5.0                       100.0                    
91    5.0                       100.0                    
92    5.0                       100.0                    
93    5.0                       100.0                    
94    5.0                       100.0                    
95    5.0                       100.0                    
96    5.0                       100.0                    
97    5.0                       100.0                    
98    5.0                       100.0                    
99    5.0                       100.0                    
100   5.0                       100.0                    

@chapmanjacobd
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment