Negative numbers in the Fibonacci series

In every Fibonacci series program (that i saw) after the 46th number (1836311903) the program starts writing negative numbers (like the 47th = -132375223 instead of 2971215073*).
This is my program but the same mistake repeats with other codes from the internet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

using namespace std;

int main()
{

    int NumerOfTheFibonacciSeriesTheUserWants;
    cout << "How many numbers of the Fibonacci series do you want?" << endl ;
    cin >> NumerOfTheFibonacciSeriesTheUserWants;
    cout << endl << "0 , 1";
    int previousNumber = 0;
    int sum = 1;
    int total = 1;
    for (int numberOfTimesRan = 1; numberOfTimesRan <= NumerOfTheFibonacciSeriesTheUserWants - 1 ; numberOfTimesRan++){
        sum = sum + previousNumber;
        previousNumber = sum - previousNumber;
        cout << " , " << sum  ;
        total += sum;

    }
    cout  << "\n \n \n \n The sum of these numbers is " << total << "\n \n \n \n" ;
    return 0;
}




*The right Fibonacci series numbers in: http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibtable.html

You don't say what compiler you're using, but for a 32 bit compiler, the capacity of a signed int is 2^31 = 2,147,483,648.

You're exceeding the capacity of a signed int on a 32 bit compiler.

Possible solution in your case would be to use [unsigned] long long. This will at least postpone your problem. If you will need values which will not fit in long long, you will need to search for some bigInt library.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdexcept>
#include <iostream>

template <typename T>
struct fibonacci_sequence
{
    using value_type = T;

    value_type operator()()
    {
        if (next < current)
            throw std::overflow_error("Overflow encountered in fibonacci_sequence.");

        auto result = current;

        auto next_in_series = current + next;
        current = next;
        next = next_in_series;

        return result;
    }


private:
    value_type current = 0;
    value_type next = 1;
};

template <typename sequence>
void largest_generated(sequence& seq)
{
    typename sequence::value_type largest = 0;

    try {
        for (;;)
            largest = seq();
    }

    catch (std::exception& ex)
    {
        std::cout << ex.what() << '\n';
        std::cout << "Last value generated was " << largest << "\n\n";
    }
}

int main()
{
    fibonacci_sequence<int> int_seq;
    fibonacci_sequence<unsigned> unsigned_seq;
    fibonacci_sequence<unsigned long long> ull_seq;

    largest_generated(int_seq);
    largest_generated(unsigned_seq);
    largest_generated(ull_seq);
}


http://ideone.com/I7DHAk
May I ask, why you did
1
2
3
4
template <typename T>
struct fibonacci_sequence
{
    using value_type = T;
and not
1
2
3
template <typename value_type>
struct fibonacci_sequence
{
?
I figured the OP would be more likely to understand that than a decltype on line 32. The choice was somewhat arbitrary.
Topic archived. No new replies allowed.