Good evening again,

PHP currently lacks a way to do integer division. You can floor or int cast a 
floating-point division, but beyond 53-bits that produces the wrong result:

$ sapi/cli/php -r 'var_dump((int)(PHP_INT_MAX / 3));'
int(3074457345618258432)

Furthermore, using a floating-point division isn’t really a proper way to do 
it; it’s a workaround for a lack of language support for a quite basic 
operation. This RFC proposes a function for integer division, intdiv(). It 
would work properly with 64-bit values:

$ sapi/cli/php -r 'var_dump(intdiv(PHP_INT_MAX, 3));'
int(3074457345618258602)

It works exactly as you’d expect it to. If you divide by zero, it does exactly 
the same thing the division operator does (E_WARNING and returns FALSE).

While this is simple enough, I’m also considering possibly shortening it to 
div() (more likely to cause conflicts, but easier to type), implementing it as 
an internal function (à la pow()), and perhaps implementing it as an infix 
operator (3 div 2 === 1).

The RFC is here: https://wiki.php.net/rfc/intdiv

It elaborates a little more, so I’d suggest reading it first. What are your 
thoughts? I think this is a simple and obvious addition.

Thanks!
--
Andrea Faulds
http://ajf.me/





--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to