Created
July 30, 2024 07:06
-
-
Save chapmanjacobd/a9561fd283f8bdbeb94398d436318abf to your computer and use it in GitHub Desktop.
Muller's Recurrence in python -- https://latkin.org/blog/2014/11/22/mullers-recurrence-roundoff-gone-wrong/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |
Author
chapmanjacobd
commented
Jul 30, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment