3 ms·
This casts only $sum to int. If $i is a float, the result is a float. With 32 bits, $sum will be a float bigger than PHP_INT_MAX at some point, and the cast wi
by fooyc 13y ago
This casts only $sum to int. If $i is a float, the result is a float.
With 32 bits, $sum will be a float bigger than PHP_INT_MAX at some point, and the cast will truncate it, which is unlikely to give the right answer.
With 64 bits, which I assume is the platform you tested on, $sum is never bigger than PHP_INT_MAX, so it's never converted to a float, and the (int) cast does nothing (and the computation is done right).
- ck2 13y agoThe result they are showing is clearly on a 64bit machine, my 32bit windows php overflows into a signed integer. I posted initially with casting (int) $i but I tested it without and it worked too. In fact on my 64bit centos machine PHP 5.4 doesn't need the cast at all and the original code gives the correct result the first time as is.