Logo

Programming-Idioms

History of Idiom 86 > diff from v15 to v16

Edit summary for version 16 by programming-idioms.org:
[Java] PROBLEM: this works only when x and y are positive.

Version 15

2017-04-13, 15:26:34

Version 16

2018-03-05, 16:04:30

Idiom #86 Check if integer multiplication will overflow

Write boolean function multiplyWillOverflow which takes two integers x, y and return true if (x*y) overflows.

Idiom #86 Check if integer multiplication will overflow

Write boolean function multiplyWillOverflow which takes two integers x, y and return true if (x*y) overflows.

Code
static boolean multiplyWillOverflow(int x, int y) {
	return Integer.MAX_VALUE/x < y;
}
Code
static boolean multiplyWillOverflow(int x, int y) {
	return Integer.MAX_VALUE/x < y;
}
Comments bubble
PROBLEM: this works only when x and y are positive.