Skip to content

Instantly share code, notes, and snippets.

@showell
Created April 24, 2023 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save showell/c4ac5fd661776d93f3a25de0103dca66 to your computer and use it in GitHub Desktop.
Save showell/c4ac5fd661776d93f3a25de0103dca66 to your computer and use it in GitHub Desktop.
Show rounding errors in Fibonacci series
import math
ROOT5 = math.sqrt(5)
PHI = (1 + ROOT5) / 2
def closed_fib(n):
v = (PHI**n) / ROOT5
return int(v)
def test():
fib = {}
fib[0] = 0
fib[1] = 1
for i in range(2, 100):
fib[i] = fib[i - 1] + fib[i - 2]
print()
print(f"fib[{i}] == {fib[i]}")
print(f"closed_fib({i}) == {closed_fib(i)}")
if i > 50 and fib[i] != closed_fib(i):
print(
f"""
ROUNDING ERROR!
closed_fib({i}) has about {int(math.log(closed_fib(i), 2))} bits"""
)
break
test()
@showell
Copy link
Author

showell commented Apr 24, 2023

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