Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exceeding C++'s largest integer datatype

Tags:

c++

c++11

I am writing a combinations calculator and for the bigger calculations I end up hitting an overflow with long long int or int64_t. Is it possible to perhaps, at least, convert the number to something of this sort: 6.7090373691429E+19?

Here is my code:

#include <iostream>
#include <string.h>
#include <math.h>

int main() {


  std::string charset;
  int i, length; int64_t total = 0;

  std::cout << "Charset: ";
  std::cin >> charset;
  std::cout << "Length: ";
  std::cin >> length;

    for (i=0;i<(length+1);i++) {
        total += pow(charset.size(),i);
    }

    std::cout << "\nPossible combinations: " << total << std::endl;

    return 0;
}
like image 861
McJohnson Avatar asked May 23 '26 19:05

McJohnson


1 Answers

The C++ standard library does not include arbitrary size integer types.

You can use Boost Multiprecision for this. It has different backends, using dedicated libraries (e.g. GMP) and a custom backend without external dependencies (cpp_int).

Edit: To be fair, vsoftco already mentioned Boost Multiprecision in a comment.

like image 90
Finn Avatar answered May 25 '26 07:05

Finn