Thread: Binet's Formula

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    5

    Binet's Formula

    I am trying to calculate the Nth number in the Fibonacci Sequence using Binet's Formula, however I have found the following two problems with my current implementation.
    If N > 46 it overflows and if N is negative it doesnt work for some reason.

    Here's my snippet of code.

    Code:
    long double f(short N) {
    
    
    double phi = (1+pow(5,0.5))/2;
    return ceil((pow(phi,N) - pow(1-phi,N))/pow(5,0.5));
    
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't see why it would overflow at 46 - however, if your intermediate results are double (such as the phi, pow(), ceil() etc), there's little point in using long double for the result.

    Negative N is unlikely to be solved with this formula, as x^-N would not be anywhere similar to what the definition of the Fibonacci series defines for negative N. I'm pretty sure that Binet's formula is not valid for negative N.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Zeller's Formula
    By unejam2005 in forum C++ Programming
    Replies: 6
    Last Post: 11-14-2005, 09:48 PM
  2. how to change char to formula
    By kosong in forum C Programming
    Replies: 2
    Last Post: 06-09-2003, 04:31 AM
  3. Math formula (js -->C++)
    By Demon1s in forum C++ Programming
    Replies: 20
    Last Post: 05-02-2003, 05:22 AM
  4. Need help with this formula -b/2a..Please read.
    By foofoo in forum C Programming
    Replies: 4
    Last Post: 06-04-2002, 11:59 PM
  5. Formula string conversion
    By Gr3g in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2002, 08:28 PM