Logo

Programming-Idioms

History of Idiom 86 > diff from v16 to v17

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

Version 16

2018-03-05, 16:04:30

Version 17

2018-03-05, 16:05:21

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
function multiplyWillOverflow($x, $y)
{
      return ($x * $y) > PHP_INT_MAX;
}
Code
function multiplyWillOverflow($x, $y)
{
      return ($x * $y) > PHP_INT_MAX;
}
Comments bubble
PHP_INT_MAX has different values on 32bit vs 64bit machines
Comments bubble
PHP_INT_MAX has different values on 32bit vs 64bit machines.

PROBLEM: this works only when $x and $y are positive.
Doc URL
http://php.net/manual/en/reserved.constants.php
Doc URL
http://php.net/manual/en/reserved.constants.php