Exceeding integer data type C ++
I am writing a combination calculator, and for large calculations, I end up overflow with long long int
or int64_t
. Is it possible at least to convert the number to something like this: 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;
}
+3
source to share
1 answer
The C ++ Standard Library does not include arbitrary sized integer types.
To do this, you can use Zoom Ellipsis . It has different servers using dedicated libraries (like GMP) and a custom backend with no external dependencies (cpp_int).
Edit: To be fair, vsoftco already mentioned Boost Multiprecision in a comment.
+5
source to share