-3

This code returns the correct Fibonacci terms at first but then when it gets to the 47th term it starts giving negative numbers or seemingly random numbers

This is my code:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
    int x;
    int t1 = 0;
    int t2 = 1;
    int nextTerm = 0;
    cout<< "Enter the term in the sequence that you want:\n";
    cin>> x;

    for (int i = 1; i <= x; i++) {
        if (i == 1) {
            cout<< t1 << ", ";
            continue;
        }

        if (i == 2) {
            cout<< t2 << ", ";
            continue;
        }
        nextTerm = t1 + t2;
        t1 = t2;
        t2 = nextTerm;

        cout<< nextTerm << ", ";

    }
    cout<< "\n";
    return 0;
}

And this is what it returns:

enter image description here

If anyone could explain why this is please answer, also I recently started learning C++ so please go easy on me in the comments :)

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • 4
    integer overflow. Use a bigger integer type or a bigint library. Note also that signed integer overflow is UB. – jaggedSpire Jan 10 '17 at 19:16
  • The maximum number representable by a 32-bit integer (which `int` is on many platforms) is 2^31-1, or 2147483647. – qxz Jan 10 '17 at 19:20
  • Ohhh, thank you jaggedSpire! I'll go research that now :) – UnlimitedAwe Jan 10 '17 at 19:20
  • Weird. I can't find a good dupe target, even though this question gets asked pretty frequently. [this one](http://stackoverflow.com/questions/9645193) has a good question/answer pair, but is more on the topic of direct calculation instead of iterative, and [this one](http://stackoverflow.com/questions/22185259/) has more wrong than an integer overflow... – jaggedSpire Jan 10 '17 at 19:21
  • Alternative duplicate, [Why am i not able to print 47th fibonacci number correctly?](http://stackoverflow.com/q/38300063/364696). – ShadowRanger Jan 10 '17 at 19:22
  • And a C++ specific one: [Fibonacci series in C++ can't get more than 47 numbers](http://stackoverflow.com/q/22185259/364696) – ShadowRanger Jan 10 '17 at 19:23
  • Thank you guys! This really helped :) – UnlimitedAwe Jan 10 '17 at 19:25

3 Answers3

2

Built-in numeric types in c++ have a fixed range of values they can represent. What happens when signed integer like int exceed this range (oveflow) is undefined. Often you will notice that they wrap around to there smallest possible representable value, but don't count on it. See std::numeric_limits for more information. Note that the range of values depends on your platform and compiler.

The type intmax_t defined in the cstdint header will provide you with your platform's largest integer type.

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
1

Have you checked how big your integer is? The c++ int can hold only integers of certain size and when you add more to it it overflows (it is modular arithmetic just like analog clock: if it is 10 o'clock and you wait four hours it is 2 o'clock). So a very big number + something = very small number (usually).

F_47 = 2971215073

Check this out: How would you set a variable to the largest number possible in C?

Community
  • 1
  • 1
kyticka
  • 604
  • 8
  • 19
  • 2
    It would be helpful if you explained why this is causing the problem (I have already upvoted as I believe you are correct, but it would be helpful if you explained why the OP needs to check the size of `int`). – pstrjds Jan 10 '17 at 19:19
0

you can have only numbers less than or equal to 2^31-1 that is approximately around 10^9 , so when the number exceeds , it will gonna be a negative number if you use long long instead of int the range of numbers you can see becomes greater

Mohsen_Fatemi
  • 2,183
  • 2
  • 16
  • 25