Created
April 24, 2023 12:59
-
-
Save showell/c4ac5fd661776d93f3a25de0103dca66 to your computer and use it in GitHub Desktop.
Show rounding errors in Fibonacci series
This file contains hidden or 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
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.youtube.com/watch?v=wxPbuo1pLZ0