1

What is bigger than unsigned long long int in C++ (Visual Studio)?

fib(93) doesn't work, but fib(92) works correctly:

#include <iostream>
using namespace std;

long long int fib(int n) {
    unsigned long long * f = new unsigned long long int[n];
    f[0] = 1;
    f[1] = 1;
    for (int i = 2; i < n; i++) {
        f[i] = f[i - 1] + f[i - 2];
    }
    return f[n-1];
}

int main() {
    cout << fib(93);
    system("pause");
    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    That's potentially quite a large memory leak. :-) – erip May 12 '22 at 18:39
  • 1
    there is no integer built in type bigger than that – Federico May 12 '22 at 18:40
  • 2
    Well, `unsigned long long[2]` is bigger than `unsigned long long` (i.e. takes more space), but I guess you were looking for an unsigned integral type that has a greater maximum value than `unsigned long long`... Btw: To calculate a single fibonacci value, you just need store 2 intermediate values; you don't need to keep all the numbers you never touch again... – fabian May 12 '22 at 18:46
  • suggested solution ? – Omar Saleem Haji May 12 '22 at 18:47
  • The suggested solution is to use an arbitrary precision numeric library instead of built-in types. – Mark Ransom May 12 '22 at 18:48
  • See also "[biggest integer datatype in c++?](//stackoverflow.com/q/1417113/90527)" – outis May 12 '22 at 19:45
  • I am impressed by the number of programming beginners that want to use huge integer numbers. I have been programming in C++ for more than 20 years, and I *never* felt any need to use an arbitrary precision library. Well, for hashing, I wish I could use `uint128_t`, but otherwise, 64-bit integers seem to much longer than necessary nearly always. – prapin May 12 '22 at 20:29
  • @prapin I think it's class problems and code challenges that require those really big integers, nothing in real life. A little known fact is that a double floating point can hold a 53 bit integer; JavaScript doesn't bother having an integer type. – Mark Ransom May 13 '22 at 20:48

1 Answers1

3

There is currently no native integer type larger than (unsigned) long long int defined by the C++ standard. Some compilers do implement a custom 128-bit integer type as an extension (__(u)int128_t, etc), but that is not standard. There are plenty of big-integer libraries readily available that implement arbitrary-length integers.

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
anatolyg
  • 26,506
  • 9
  • 60
  • 134