Hi,
This is a very naive question. I am writing a code where large integers are being multiplied. Now I want to make sure that this multiplication does not exceed the range of integers in C++. If it does, the program should print a message. Is there any way to implement it?
The maximum amount a integer or I believe any type can hold is implementation defined, so it can vary.
Here is how you can find out what it is on your implementation.
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
#include <limits>
usingnamespace std;
int main()
{
cout << "The max size of int is: " << numeric_limits<int>::max() << endl;
cout << "The max size of unsigned int is: " << numeric_limits<unsigned>::max() << endl;
cout << "The max size of short int is: " << numeric_limits<short>::max() << endl;
}
Just replace the type inside the < > with the type you are using to find out it max size.
Though if you are working with big numbers I would suggest getting a big number library to work with, or for a good learning experience creating your own.
Thanks Zereo. However, my question is slightly different. I want to ensure that whenever the multiplication of numbers exceeds the bound of integers, I am being informed.
Though my code is rather complicated, we can consider following simple example below. In the code below, can I perform some kind of check on 'd' at any intermediary stage to know if the multiplication exceeded the bound of integers?
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main()
{
int a, b, c, d;
// Random number initialization
srand (time(NULL));
a = rand();
b = rand();
c = rand();
d = 1000000000*a*b*c;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
// Either here or earlier we want to perform some kind of check on 'd'
// that will tell if d went beyond the bound.
cout << "Multiplication is: " << d << endl;
return 0;
}
I don't see why not and that is why I gave you the tools to find out what the max range is for integers. Now this is no where near the best solution but why not try using a if condition that tests weather the multiplication you want to do will go beyond the size that a integer can hold? If it doesn't you can then assign it to the variable or whatever you want to do with the result.
int a, b, c, d;
// Random number initialization
srand (time(NULL));
//const int e = 1000000000;
constint e = 1;
a = rand();
b = rand();
c = rand();
d = e*a*b*c;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
// Either here or earlier we want to perform some kind of check on 'd'
// that will tell if d went beyond the bound.
cout << "Multiplication is: " << d << endl;
if (d/a/b/c == e)
{
cout << "success" << endl;
}
else
{
cout << "overflowed" << endl;
}
int limit: 2147483647
short limit: 32767
long limit: 2147483647
long long limit: 9223372036854775807
double limit: 1.7976931348623157081e+308
long double limit: 1.189731495357231765e+4932
Process returned 0 (0x0) execution time : 0.028 s
Press any key to continue.