On 16 Jul 2014, at 02:51, Bishop Bettini <[email protected]> wrote:
> Hi Andrea,
>
> TL;DR -- I agree with the principal but want implemented as the infix
> operator %% with a test for PHP_INT_MIN %% -1 === false (and a E_WARNING).
%% is perhaps the only good non-keyword syntax choice. I like that.
> As a user, I could implement intdiv and get the same functionality as
> proposed:
> function intdiv($n, $d) { return (int)($n / $d); }
You could, yes, but it wouldn’t return the correct result beyond 53-bit
integers, and I’m not sure it would handle $x/-1 particularly well.
>
> Heck, I could even simplify further:
> function intdivmod($n, $d, $m = 1) {
> return (int)($n / $d) % $m;
> }
>
> Though an internal implementation would be faster and would bypass the
> float truncation issue (which IMO is a separate issue and neither a pro nor
> con for this RFC), I feel like we need to confer additional benefit if we
> implement this in core.
>
> Specifically, I'd want this implemented infix. Let's call it // for now.
> I'd like to be able to:
>
> $x = 247 // 5;
> $x //= 5;
>
> The functional notation doesn't afford assign equals. It collides with
> user functions. It complicates expressions. (I also don't like pow()
> functionally and wish PHP had an operator like ** for powers.) So, why not
> implement this as an actual operator?
PHP *does* have an operator “like **” for powers… the ** operator. Did you
somehow miss that RFC passing? :)
>
> As far as an operator, that's trickier. // and \ would be most like
> existing languages, but those are problematic as already stated. We could
> overload /, relying on the left and right operands to be int to induce
> integer division... but I'm pretty sure that would be more hassle than it's
> worth. So the best I can think of is %%. That seems ok to me, since there
> is some family resemblance between modulus and integer division.
Right. This is just returning the other part of an integer division, the actual
result itself, not the remainder.
> Finally, I think a test on the other side of the spectrum is needed:
> PHP_INT_MIN %% -1. Mathematically that should be PHP_INT_MAX+1, I believe,
> so a warning and false seems appropriate.
The patch currently returns zero because I don’t want to yield a float like /
does, but E_WARNING and FALSE sounds good, we do that for integer division
anyway.
Do others on internals think that %% is a good syntax choice? Does anyone have
an objection to it? It wouldn’t conflict with anything, it uses punctuation,
and it makes sense.
--
Andrea Faulds
http://ajf.me/
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php