How can I check if multiplying two numbers in Java will cause an overflow?
I want to handle the special case where multiplying two numbers together causes an overflow. The code looks something like this:
int a = 20;
long b = 30;
// if a or b are big enough, this result will silently overflow
long c = a * b;
That's a simplified version. In the real program a
and b
are sourced elsewhere at runtime. What I want to achieve is something like this:
long c;
if (a * b will overflow) {
c = Long.MAX_VALUE;
} else {
c = a * b;
}
How do you suggest I best code this?
Update: a
and b
are always non-negative in my scenario.
Java 8 has Math.multiplyExact
, Math.addExact
etc. for ints and long. These throw an unchecked ArithmeticException
on overflow.
If a
and b
are both positive then you can use:
if (a != 0 && b > Long.MAX_VALUE / a) {
// Overflow
}
If you need to deal with both positive and negative numbers then it's more complicated:
long maximum = Long.signum(a) == Long.signum(b) ? Long.MAX_VALUE : Long.MIN_VALUE;
if (a != 0 && (b > 0 && b > maximum / a ||
b < 0 && b < maximum / a))
{
// Overflow
}
Here's a little table I whipped up to check this, pretending that overflow happens at -10 or +10:
a = 5 b = 2 2 > 10 / 5
a = 2 b = 5 5 > 10 / 2
a = -5 b = 2 2 > -10 / -5
a = -2 b = 5 5 > -10 / -2
a = 5 b = -2 -2 < -10 / 5
a = 2 b = -5 -5 < -10 / 2
a = -5 b = -2 -2 < 10 / -5
a = -2 b = -5 -5 < 10 / -2
There are Java libraries that provide safe arithmetic operations, which check long overflow/underflow. For example, Guava's LongMath.checkedMultiply(long a, long b) returns the product of a
and b
, provided it does not overflow, and throws ArithmeticException
if a * b
overflows in signed long
arithmetic.
You could use java.math.BigInteger instead and check the size of the result (haven't tested the code):
BigInteger bigC = BigInteger.valueOf(a) * multiply(BigInteger.valueOf(b));
if(bigC.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) {
c = Long.MAX_VALUE;
} else {
c = bigC.longValue()
}
Use logarithms to check the size of the result.