[Haskell-cafe] Fibonacci numbers generator in Haskell
Doug Quale
quale1 at charter.net
Fri Jun 16 18:11:54 EDT 2006
Doug Quale <quale1 at charter.net> writes:
> Mathew Mills <mathewmills at mac.com> writes:
>
> > Is there anything that can be done (easily) to reduce the rounding errors?
>
> The hint that I gave before is one easy way.
>
> > fib :: Integer -> Integer
> > fib x = let phi = ( 1 + sqrt 5 ) / 2
> > in truncate( ( 1 / sqrt 5 ) * ( phi ^ x + 0.5) )
>
> You run out of precision eventually. IEEE Double's give you about 15
> decimal digits, so the results become approximate for x > 75.
Sorry, I suffered brain lock. The correct expression is parenthesized
differently:
> fib n = truncate(phi^n/sqrt 5 + 0.5)
> where phi = (1 + sqrt 5)/2
More information about the Haskell-Cafe
mailing list