So I am trying to learn python and wrote a small program to give me the Fibonacci numbers. While I know that they can be defined recursively. I wanted to use the known mathematical formula for Efficiency. My code is very simple:
from math import sqrt
def F(n):
return ((1+sqrt(5))**n-(1-sqrt(5))**n)/(2**n*sqrt(5))
zahl = int(input("Welche Fibonacci Zahl möchten Sie: "))
print(F(zahl))
When I execute it, it gives me weird rounding Errors, for example for the Input 6 I get
8.000000000000002
instead of the expected 8.0. Does anyone know where this rounding error Comes from and how to avoid it. I was hoping that the Standard sqrt function in the math library was working precise enough! I am using Python 3.5 (32bbit) if that helps.