Integer multiplication exceeding the range

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?

Thanks.
closed account (3qX21hU5)
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>

using namespace 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.
Last edited on
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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace 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;
}
closed account (3qX21hU5)
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.
You could test by division to see whether you get back the original numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
    int a, b, c, d;

    // Random number initialization
    srand (time(NULL));

    //const int e = 1000000000;

    const int 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;
    }
don't forget long, long long, double, long double ( in order of size )
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <limits>
    std::cout << "int limit: " << std::numeric_limits<int>::max() << std::endl;
    std::cout << "short limit: " << std::numeric_limits<short>::max() << std::endl;
    std::cout << "long limit: " << std::numeric_limits<long>::max() << std::endl;
    std::cout << "long long limit: " << std::numeric_limits<long long>::max() << std::endl;
    std::cout << "double limit: " << std::numeric_limits<double>::max() << std::endl;
    std::cout << "long double limit: " << std::numeric_limits<long double>::max() << std::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.
Last edited on
If the magnitude of the result is less than the magnitude of the larger non-zero multiplicand then you had overflow.

Hope this helps.
Thanks guys! These are some of the ideas I were looking for.
@Duoas
I believe that's correct, check the if statement. I think dividing by 0 throws a floating point exception.

1
2
3
4
5
6
7
8
#include <iostream>
int main()
{
    int first(10), second(100000000), result(first * second);
    if (first != 0 && result / first != second)
        std::cout << "Overflow\n";
    return 0;
}
The point is that division takes too long.

1
2
3
4
5
6
7
8
9
long long multiply( long long larger, long long smaller )
  {
  long long result = larger * smaller;

  if ((result < larger) and (smaller != 0))
    throw "integer multiplication overflow";

  return result;
  }

Hope this helps.
Topic archived. No new replies allowed.