I am trying to use fairly simple C++ code to complete this problem: The user will input a number of the Fibonacci series, and through a function called whichfib(), the program will then output the number using long double data type.

I have gotten the code to work until the number 47, some numbers after 47 will be negative, and some are just completely off. The code should only work from fib numbers 0-95, here is my code:

#include <iostream>
#include <iomanip>
using namespace std;

long double whichfib (int num);

int main ()
{
int num, count=-1;

while (count < 0 || count > 96)
{
        cout << "Which Fibonacci number would you like? ";
         cin >> num;
 count = num;
}
        cout << setprecision(0);
        cout << "Fibonacci #" << num << " is " << fixed << noshowpoint << whichfib (num) << endl;
return 0;
}


long double whichfib (int num)
{
int count=2, newnum1, newnum2, newnum;
long double fnum;

if (num == 0)
fnum = 0;

 else if (num == 1)
 fnum = 1;

 else if (num == 2)
 fnum = 1;

else
{
 newnum1=0;
 newnum2=1;
 
do
{
 newnum = newnum1 + newnum2;
  newnum1 = newnum2;
   newnum2 = newnum;
count++;
}
while (count < num);

 fnum = newnum1 + newnum2;
}
return fnum;
}

if anyone can help it would be greatly appreciated, but please remember this is a beginner course, I cannot put any advanced statements, it must be a while statement and also just what is available in the iostream library. Thanks again

Dani AI

Generated

Nice diagnosis from and nice followup from — the wrong/negative outputs are a classic overflow/precision issue. Below are practical checks and a small helper you can drop into code to discover exactly how far long double will safely carry Fibonacci integers on a given compiler/runtime, plus a safe-add test you can use to avoid silent integer wraparound.

To detect integer overflow before it happens (useful if you keep intermediate values as integers):

#include <limits>

bool add_will_overflow_int(int a, int b) {
    return (b > 0) && (a > std::numeric_limits<int>::max() - b);
}

Use that check in the loop to stop or switch types before an overflow occurs. Signed integer overflow on typical platforms wraps around and produces negative/garbled numbers; unsigned overflow also wraps, while floating-point will go to infinity or lose integer precision once values exceed the mantissa.

To compute the largest Fibonacci index that remains an exact integer in long double on the current build, use a short routine that queries the floating type precision (std::numeric_limits<T>::digits) and applies the exponential estimate (Binet). Example:

#include <cmath>
#include <limits>

int max_exact_fib_index_for_long_double() {
    long double log2_phi = std::log2((1.0L + std::sqrt(5.0L)) / 2.0L);
    long double log2_sqrt5 = 0.5L * std::log2(5.0L);
    int m = std::numeric_limits<long double>::digits; // mantissa bits
    return static_cast<int>(std::floor((m + log2_sqrt5) / log2_phi));
}

Typical results: compilers that provide 80-bit x87 long double give about 93, while implementations where long double == double give about 78 (see the math behind this on the Fibonacci page and std::numeric_limits docs). For exact results beyond those indices, use an arbitrary-precision integer type such as Boost.Multiprecision (cpp_int) or a big-int library. References: Fibonacci numbers (Binet and growth) and std::numeric_limits.

Recommended Answers

All 5 Replies

Welcome aboard..hope you'll enjoy it here. Your formatting is horrible...please look at the two anouncements at the top of the page, so you can place your code in tags. Also, in the future, before posting, search the forums to see if someone else had the same problem/assignment. We are willing to help, but we don't want to waste time doing the same thing over and over again...here are a few links that may be able to help you:
http://www.daniweb.com/forums/showthread.php?t=51631&highlight=Fibonacci+number+series
http://www.daniweb.com/forums/showthread.php?t=94074&highlight=Fibonacci+number+series
You can search around for the others....also....Google is your friend.

Yeah, im sure that it is horrible, this is the introductory course, and our teacher isnt too worried about the formatting, just as long as it works. I have searched the forums and all of the answers given direct to stuff that we have not learned yet, so I cannot use any of these methods. Those two links you posted came up broken for me.

I'm not talking about the formatting for your class...i'm talking about formatting the code for the forum....again...read the two announcements at the top of the page...

Your problem lies in the intermediate variables you use in the whichfib( ) function:

long double whichfib (int num)
{
      int count=2, newnum1, newnum2, newnum;
      long double fnum;

Your use of long double is good for dealing with the large values that will result. You should also use that type for the newnum variables. Remember, at each step, these variables grow at the same rate as your final value - they get BIG. The int will blow up at 2 billion and change, well short of your desired answer range. If you used unsigned int, you'd get 4 billion and change, still too small. All the variables other than count should be long doubles.

And, just for fun, keep in mind that long double is not a consistent sized variable on all systems. In Windows, using MS Visual C++, long doubles are 8 bytes. Most other compilers will use 10 bytes.

Val

ok thanks for the help guys, I got it figured out today

long double whichfib (int num)
{
int count=1;
long double newnum, newnum1, newnum2;

if (num == 0)
newnum = 0;

 else if (num == 1)
 newnum = 1;

else
{
newnum1=0;
newnum2=1;

do
{
newnum = newnum1 + newnum2;
newnum1 = newnum2;
newnum2 = newnum;
count++;
}
while (count < num);

}
return newnum;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.