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).

As a user, I could implement intdiv and get the same functionality as
proposed:
    function intdiv($n, $d) { return (int)($n / $d); }

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?

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.

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.

Regards,
bishop
On Jul 15, 2014 8:11 AM, "Andrea Faulds" <a...@ajf.me> wrote:

>
> On 15 Jul 2014, at 03:31, Bishop Bettini <bis...@php.net> wrote:
> > I need some education.  Can you elaborate on the specific situations
> where integer division would be used without other functions from bcmath or
> gmp?
> >
> > I see in the RFC you mention seconds to hh:mm and index to rows/cols,
> but can you give some actual "before" and "after" samples?  Like this is
> how it would be done today without intdiv, and here's how it would be done
> after?
>
> Sure. Say I have a number of seconds which I wish to split into years,
> days, hours, minutes and seconds, for example:
>
> $s = 1000000;
> $seconds = $s % 60;
> $minutes = intdiv($s, 60) % 60;
> $hours = intdiv($s, 3600) % 24;
> $days = intdiv($s, 3600 * 24) % 365;
> $years = intdiv($s, 3600 * 24 * 365);
>
> Currently, you’d have to cheat and use floating-point division:
>
> $x = 1000000;
> $seconds = $s % 60;
> $minutes = (int)($s / 60) % 60;
> $hours = (int)($s / 3600) % 24;
> $days = (int)($s / (3600 * 24)) % 365;
> $years = (int)($s / (3600 * 24 * 365));
>
> The second one is a bit of a hack, really, but it would probably work most
> of the time since realistically nobody is using >53-bit time values at the
> moment (though people are using >32-bit values, so that 64-bit RFC can’t
> come soon enough given Y2K38).
>
> However, intdiv() is perhaps not the best way to implement it or the best
> syntax.
>
> --
> Andrea Faulds
> http://ajf.me/
>
>
>
>
>

Reply via email to