vendredi 22 juillet 2016

Strange values calculating Fibonacci in numpy

I've got this small (maybe inefficient, but still reasonable) code:

fib = np.array([0,1])
for i in range(2,100):
    fib = np.append(fib, fib[-2] + fib[-1])

Reviewing the result at first all looks good:

In [17]: fib[:20]
Out[17]: array([   0,    1,    1,    2,    3,    5,    8,   13,   21,   34,   55,
         89,  144,  233,  377,  610,  987, 1597, 2584, 4181])

But then it becomes strange:

In [18]: fib[51:60]
Out[18]: 
array([-1109825406, -1408458269,  1776683621,   368225352,  2144908973,
       -1781832971,   363076002, -1418756969, -1055680967])

I figure this is a data type accuracy issue, but a clear explanation will be appreciated. Is there a way to do better?

Aucun commentaire:

Enregistrer un commentaire