Skip Menu |

This queue is for tickets about the Math-Fibonacci CPAN distribution.

Report information
The Basics
Id: 58353
Status: open
Priority: 0/
Queue: Math-Fibonacci

People
Owner: Nobody in particular
Requestors: user42 [...] zip.com.au
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: fib series() rounding
Date: Sun, 13 Jun 2010 07:49:10 +1000
To: bug-Math-Fibonacci [...] rt.cpan.org
From: Kevin Ryde <user42 [...] zip.com.au>
With Math::Fibonacci 1.5 on recent 32-bit i386 debian perl 5.10.1, a program use strict; use warnings; use Math::Fibonacci 'series'; my @fib = series(70); foreach my $i (59 .. 61) { printf ("%2d %16s\n", $i, $fib[$i]); } prints 59 1548008755920 60 2504730781961 61 4052739537880 Where I expected the last number to be 4052739537881, the sum of the previous two. Those values are about 40-something bits, so they're still exactly representable in a 53-bit double and I hoped Math::Fibonacci would give exact values up to the point they can be represented. Perhaps simple additions in series() would maintain exactness up to 53-bits, and even to 64-bits on a 64-bit UV system.
Five years has passed, and so far the author has not replied. I looked into this, and it turns out that the Fibonacci numbers are computed as nearestint ((g ** shift) / sqrt(5)) where g = 1.61803398874989, an approximation to the golden ratio, and nearestint() is simply a function returning the nearest integer. The problem is that g is too small. This has no effect on the first terms in the series, but as the exponent increases, so does the effect of g being too small. In your case we have $ perl -wle 'print 1.61803398874989 ** 62 / sqrt(5)' 4052739537880.25 which, rounded to the nearest integer is 4052739537880, not 4052739537881 as you expect. What I find odd is that g is not as accurat as it could have been. Using g = 1.618033988749895 gives us a better result: $ perl -wle 'print 1.618033988749895 ** 62 / sqrt(5)' 4052739537881.01 which, when rounded to the nearest integer, returns the correct result.