Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:75520 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 98283 invoked from network); 15 Jul 2014 00:14:55 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Jul 2014 00:14:55 -0000 Authentication-Results: pb1.pair.com header.from=ajf@ajf.me; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ajf@ajf.me; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain ajf.me designates 192.64.116.207 as permitted sender) X-PHP-List-Original-Sender: ajf@ajf.me X-Host-Fingerprint: 192.64.116.207 imap2-2.ox.privateemail.com Received: from [192.64.116.207] ([192.64.116.207:42094] helo=imap2-2.ox.privateemail.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 5E/13-15121-D7274C35 for ; Mon, 14 Jul 2014 20:14:53 -0400 Received: from localhost (localhost [127.0.0.1]) by mail.privateemail.com (Postfix) with ESMTP id 100188C007B for ; Mon, 14 Jul 2014 20:14:51 -0400 (EDT) X-Virus-Scanned: Debian amavisd-new at imap2.ox.privateemail.com Received: from mail.privateemail.com ([127.0.0.1]) by localhost (imap2.ox.privateemail.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id bfnxoW3mZ02U for ; Mon, 14 Jul 2014 20:14:50 -0400 (EDT) Received: from [192.168.0.15] (unknown [90.210.122.167]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.privateemail.com (Postfix) with ESMTPSA id 4B1138C007D for ; Mon, 14 Jul 2014 20:14:49 -0400 (EDT) Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable Message-ID: <7646A8D1-69A2-4255-B048-D3B9F28B422F@ajf.me> Date: Tue, 15 Jul 2014 01:14:44 +0100 To: PHP internals Mime-Version: 1.0 (Mac OS X Mail 7.3 \(1878.6\)) X-Mailer: Apple Mail (2.1878.6) Subject: [RFC] intdiv() From: ajf@ajf.me (Andrea Faulds) 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=92t really a proper way = to do it; it=92s 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=92d 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=92m also considering possibly shortening = it to div() (more likely to cause conflicts, but easier to type), = implementing it as an internal function (=E0 la pow()), and perhaps = implementing it as an infix operator (3 div 2 =3D=3D=3D 1). The RFC is here: https://wiki.php.net/rfc/intdiv It elaborates a little more, so I=92d suggest reading it first. What are = your thoughts? I think this is a simple and obvious addition. Thanks! -- Andrea Faulds http://ajf.me/