@ 2016-12-05T09:31:22Z <?php
function add($overflowsTimes, $augend, $addend){
$sum = $augend + $addend;
if(is_float($sum)){
$sum = $addend - (PHP_INT_MAX - $augend);
$overflowsTimes++;
}elseif($sum < 0){
$sum = PHP_INT_MAX + $sum;
$overflowsTimes--;
}
if($overflowsTimes < 0){
throw new Error();
}
return [
$overflowsTimes,
$sum
];
}
assert(add(0, 0, 0) === [0, 0]);
assert(add(0, 100, 200) === [0, 300]);
assert(add(0, PHP_INT_MAX - 100, 100) === [0, PHP_INT_MAX]);
assert(add(0, PHP_INT_MAX, 100) === [1, 100]);
assert(add(0, PHP_INT_MAX, 1000) === [1, 1000]);
assert(add(0, PHP_INT_MAX, PHP_INT_MAX) === [1, PHP_INT_MAX]);
assert(add(1, 0, 0) === [1, 0]);
assert(add(1, 100, 200) === [1, 300]);
assert(add(1, PHP_INT_MAX - 100, 100) === [1, PHP_INT_MAX]);
assert(add(1, PHP_INT_MAX, 100) === [2, 100]);
assert(add(1, PHP_INT_MAX, 1000) === [2, 1000]);
assert(add(1, PHP_INT_MAX, PHP_INT_MAX) === [2, PHP_INT_MAX]);
assert(add(2, 0, 0) === [2, 0]);
assert(add(2, 100, 200) === [2, 300]);
assert(add(2, PHP_INT_MAX - 100, 100) === [2, PHP_INT_MAX]);
assert(add(2, PHP_INT_MAX, 100) === [3, 100]);
assert(add(2, PHP_INT_MAX, 1000) === [3, 1000]);
assert(add(2, PHP_INT_MAX, PHP_INT_MAX) === [3, PHP_INT_MAX]);
try{
add(0, PHP_INT_MAX, PHP_INT_MIN);
}catch(Throwable $e){}
print_r(add(1, PHP_INT_MAX, PHP_INT_MIN));
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 7.0.0 - 7.0.33 , 7.1.0 - 7.1.33 , 7.2.0 - 7.2.33 , 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.6 Array
(
[0] => 0
[1] => 9223372036854775806
)
Output for 5.6.0 - 5.6.40 Notice: Use of undefined constant PHP_INT_MIN - assumed 'PHP_INT_MIN' in /in/kVTFZ on line 43
Notice: Use of undefined constant PHP_INT_MIN - assumed 'PHP_INT_MIN' in /in/kVTFZ on line 46
Array
(
[0] => 1
[1] => 9223372036854775807
)
preferences:dark mode live preview
212.87 ms | 402 KiB | 288 Q