Skip to content

Instantly share code, notes, and snippets.

@chapmanjacobd
Created July 30, 2024 07:06
Show Gist options
  • Select an option

  • Save chapmanjacobd/a9561fd283f8bdbeb94398d436318abf to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown
Author

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