The Fibonacci number is generated using integer arithmetic. The Binet formula uses floating-point arithmetic. Floating-point calculations will always have these small inaccuracies because not every real number can be represented accurately.
Specifically, an 8-byte float
in SQL Server only has a 15-digit mantissa. It cannot be any more precise than 15 decimal points. Not coincidentally, the errors you are seeing occur at the 15th digit. I would hazard a guess that numbers below 70 are accurate, because they are within the precision limits of a float
.
In other words, this behaviour is by design. There is a limit to the precision you can achieve with floating-point math, and you've hit it. In order to go beyond that, you'd have to use an arbitrary-precision math library, and I'm not aware of any available within the SQL Server environment (although that doesn't necessarily mean they don't exist).
P.S. Recursion is a very inefficient method of generating a Fibonacci number, especially within a database. If this is more than an academic exercise then I would recommend switching to an iterative solution.