I'm relatively new to processing so apologies if I've missed something obvious in the following. After combing over the forums I've yet to find a post referencing Binet's -
I'm looking to calculate the Nth Fibonacci number using the following code based on Binet's formula:
int FibN(int FibNum) {
float phi = (1+sqrt(5))/2;
float res = (pow(phi,FibNum)-pow((-1/phi),FibNum)) / sqrt(5);
return int(res);
}
This works great up until the 30th position
FibN(30) = 832040 // Correct
FibN(31) = 1346269 // Correct
FibN(32) = 2178309 // Correct
FibN(33) = 3524579 // Incorrect - should be 3524578
FibN(34) = 5702888 // Incorrect - should be 5702887
As we go higher things are farther and farther off
FibN(46) = 1836312704 // Should be 1836311903
Am I missing something inherent in the data type or the conversions? Is there a more direct processing method to obtain these results without calculating all the proceeding values for the Nth position?