Continued Fraction Approximations
I wrote this Python script to find increasingly accurate rational approximations to any number using convergents and continued fraction representations. Below is the output for the Golden ratio φ ≈ 1.618, π ≈ 3.14159, √2 ≈ 1.4142, and Euler's constant e ≈ 2.718.
>>> phi = (math.sqrt(5)+1)/2
>>> convergents2floats(convergents(contfrac(phi)), phi)
Fraction Approximation Difference
1/1 1.00000000000 -0.61803398875
2/1 2.00000000000 0.38196601125
3/2 1.50000000000 -0.11803398875
5/3 1.66666666667 0.04863267792
8/5 1.60000000000 -0.01803398875
13/8 1.62500000000 0.00696601125
21/13 1.61538461538 -0.00264937337
Target: 1.61803398875
>>> convergents2floats(convergents(contfrac(math.pi)), math.pi)
Fraction Approximation Difference
3/1 3.00000000000 -0.14159265359
22/7 3.14285714286 0.00126448927
333/106 3.14150943396 -0.00008321963
355/113 3.14159292035 0.00000026676
103993/33102 3.14159265301 -0.00000000058
104348/33215 3.14159265392 0.00000000033
208341/66317 3.14159265347 -0.00000000012
Target: 3.14159265359
>>> convergents2floats(convergents(contfrac(math.sqrt(2))), math.sqrt(2))
Fraction Approximation Difference
1/1 1.00000000000 -0.41421356237
3/2 1.50000000000 0.08578643763
7/5 1.40000000000 -0.01421356237
17/12 1.41666666667 0.00245310429
41/29 1.41379310345 -0.00042045892
99/70 1.41428571429 0.00007215191
239/169 1.41420118343 -0.00001237894
Target: 1.41421356237
>>> convergents2floats(convergents(contfrac(math.e)), math.e)
Fraction Approximation Difference
2/1 2.00000000000 -0.71828182846
3/1 3.00000000000 0.28171817154
8/3 2.66666666667 -0.05161516179
11/4 2.75000000000 0.03171817154
19/7 2.71428571429 -0.00399611417
87/32 2.71875000000 0.00046817154
106/39 2.71794871795 -0.00033311051
Target: 2.71828182846