Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:75574 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 53593 invoked from network); 16 Jul 2014 01:51:20 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Jul 2014 01:51:20 -0000 Authentication-Results: pb1.pair.com smtp.mail=bishop.bettini@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=bishop.bettini@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.50 as permitted sender) X-PHP-List-Original-Sender: bishop.bettini@gmail.com X-Host-Fingerprint: 209.85.215.50 mail-la0-f50.google.com Received: from [209.85.215.50] ([209.85.215.50:49595] helo=mail-la0-f50.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 6F/C0-49478-69AD5C35 for ; Tue, 15 Jul 2014 21:51:19 -0400 Received: by mail-la0-f50.google.com with SMTP id gf5so29527lab.9 for ; Tue, 15 Jul 2014 18:51:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:reply-to:sender:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=25lnOe1bgI3+lkSDFBOXs1NBivAiuCK07HFmqb58x7s=; b=y11AAwa7Ts9teENDPggAX9C1T5E4Yn89KzKD0VYlfMlHqJqSV+mk21NSZvuY2doLh3 vJErngA4fWa3/fC9nN+xfslWI7nv43Qslf3XVoKd8L/8IKYpAuFL+Ic5QRnU2twxWIad alF+zOPQwgXKAQ87XzqcFS63BY8LQzBciphV6VDhm2n/zYBZZxp1Cz1D8TBJXPAU5IRz bM41FNYZvpAtuBBGLaOJpwl0cejaOCz8cCWgA0CioVazMEkneJivQsQJ3Du/rBx4Iuac 0R60qfJDaBbxFW8eLEZGqaNz/BdGZWM7BsPnUOFH4IZx1AamZst48u6a/HDgrD3PiKY7 i6Wg== MIME-Version: 1.0 X-Received: by 10.152.19.229 with SMTP id i5mr5658940lae.60.1405475475883; Tue, 15 Jul 2014 18:51:15 -0700 (PDT) Reply-To: bishop@php.net Sender: bishop.bettini@gmail.com Received: by 10.152.242.34 with HTTP; Tue, 15 Jul 2014 18:51:15 -0700 (PDT) Received: by 10.152.242.34 with HTTP; Tue, 15 Jul 2014 18:51:15 -0700 (PDT) In-Reply-To: References: <7646A8D1-69A2-4255-B048-D3B9F28B422F@ajf.me> Date: Tue, 15 Jul 2014 21:51:15 -0400 X-Google-Sender-Auth: Ubgf0w5WGURE6ynyblW8cNIT7l4 Message-ID: To: Andrea Faulds Cc: PHP internals Content-Type: multipart/alternative; boundary=089e01493e347b0c9e04fe45c254 Subject: Re: [PHP-DEV] [RFC] intdiv() From: bishop@php.net (Bishop Bettini) --089e01493e347b0c9e04fe45c254 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hi Andrea, TL;DR -- I agree with the principal but want implemented as the infix operator %% with a test for PHP_INT_MIN %% -1 =3D=3D=3D false (and a E_WARN= ING). 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 =3D 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 =3D 247 // 5; $x //=3D 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" wrote: > > On 15 Jul 2014, at 03:31, Bishop Bettini 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 don= e > after? > > Sure. Say I have a number of seconds which I wish to split into years, > days, hours, minutes and seconds, for example: > > $s =3D 1000000; > $seconds =3D $s % 60; > $minutes =3D intdiv($s, 60) % 60; > $hours =3D intdiv($s, 3600) % 24; > $days =3D intdiv($s, 3600 * 24) % 365; > $years =3D intdiv($s, 3600 * 24 * 365); > > Currently, you=E2=80=99d have to cheat and use floating-point division: > > $x =3D 1000000; > $seconds =3D $s % 60; > $minutes =3D (int)($s / 60) % 60; > $hours =3D (int)($s / 3600) % 24; > $days =3D (int)($s / (3600 * 24)) % 365; > $years =3D (int)($s / (3600 * 24 * 365)); > > The second one is a bit of a hack, really, but it would probably work mos= t > of the time since realistically nobody is using >53-bit time values at th= e > moment (though people are using >32-bit values, so that 64-bit RFC can=E2= =80=99t > 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/ > > > > > --089e01493e347b0c9e04fe45c254--