I've written a simple Fibonacci sequence generator that looks like:
#include <iostream>
void print(int c, int r) {
std::cout << c << "\t\t" << r << std::endl;
}
int main() {
unsigned long long int a = 0, b = 1, c = 1;
for (int r = 1; r <= 1e3; r += 1) {
print(c, r);
a = b;
b = c;
c = a + b;
}
}
However, as r
gets around the value of 40, strange things begin to happen. c
's value oscillate between negative and positive, despite the fact he's an unsigned
integer, and of course the Fibonacci sequence can't be exactly that.
What's going on with unsigned long long
integers?
Does c
get too large even for a long long
integer?