I am working on an assignment (I know many have asked but none have asked about the number of digits I need plus some) that is to accept input for the fibonacci corresponding to that (i.e. 0=0, 1=1, 2=1, ..., 96=51680708854858323072). When it gets to 79 (should be 14472334024676221 since 77=5527939700884757 and 78=8944394323791464) I end up getting 14472334024676220. I believe this is because of precision. I know this is not as little code as could be used to solve this but it is how the assignment was set up.

long double whatFib ( long double checkFib );

int main()
{

	long double  userFib;

    do
    {
         cout << "Which Fibonacci number would you like?  ";
         cin >> userFib;
    }
    while ( userFib < 0 || userFib > 96 );
    
    cout.precision(0);
    
    cout << "Fibonacci #" << userFib << " is " << fixed << whatFib(userFib);
    
    cin >> userFib;

    return 0;

}

long double whatFib( long double checkFib )
{
 
    long double result, fibOne, fibTwo, fibNum;
    fibOne = 0;
    fibTwo = 1; 
    
    if ( checkFib == 0 )
    {
         result = 0;
    }
    else if ( checkFib == 1 )
    {
         result = 1;
    }
    else
    {
        for ( fibNum = 1; fibNum <= checkFib; fibNum++ )
        {
     
            result = fibOne + fibTwo;
            fibTwo = fibOne;
            fibOne = result;
    
            cout << "#" << fibNum << ":  " << fixed << result << endl;
    
        }
                
    }
        
    return result;
    
}

You've answered your question. It is a limitation of the precision of the long double - 15 digits in this case.

I'm betting you're using M$ Visual C++ - it limits long double to 8 bytes, same as a plain ole double. If you ran the same code in other compilers (gcc, for example) you should get real 10 byte long double which should give 19 digits.

Why M$ keeps long double to 8 bytes? Good question.

Val

vmanes - You were right I put it on the Linux box and sure enough, it worked. I'm actually using Dev-C++ since it was the only thing I could get to work on my vista notebook. (Normally I use Visual Studio .NET)

siddhant3s - Thanks for the link. I don't need that many digits yet but I know where I can find the answer when I do need it.

And apparently, Dev-C++ uses Mingw, which uses M$ components. Thus, the same problem with long doubles.

Val

This question has already been answered. Start a new discussion instead.