Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:86944 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 13575 invoked from network); 29 Jun 2015 16:49:58 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 29 Jun 2015 16:49:58 -0000 Authentication-Results: pb1.pair.com smtp.mail=ajf@ajf.me; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ajf@ajf.me; sender-id=pass Received-SPF: pass (pb1.pair.com: domain ajf.me designates 192.64.116.216 as permitted sender) X-PHP-List-Original-Sender: ajf@ajf.me X-Host-Fingerprint: 192.64.116.216 imap10-3.ox.privateemail.com Received: from [192.64.116.216] ([192.64.116.216:46461] helo=imap10-3.ox.privateemail.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 1E/22-25761-D2771955 for ; Mon, 29 Jun 2015 12:49:54 -0400 Received: from localhost (localhost [127.0.0.1]) by mail.privateemail.com (Postfix) with ESMTP id E4A962400D3; Mon, 29 Jun 2015 12:49:46 -0400 (EDT) X-Virus-Scanned: Debian amavisd-new at imap10.ox.privateemail.com Received: from mail.privateemail.com ([127.0.0.1]) by localhost (imap10.ox.privateemail.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id worz6hJEUjUg; Mon, 29 Jun 2015 12:49:46 -0400 (EDT) Received: from [192.168.0.3] (unknown [90.211.5.114]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.privateemail.com (Postfix) with ESMTPSA id C08512400E3; Mon, 29 Jun 2015 12:49:42 -0400 (EDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2098\)) In-Reply-To: Date: Mon, 29 Jun 2015 17:49:01 +0100 Cc: Dmitry Stogov , PHP Internals , Nikita Popov , Anatol Belski Content-Transfer-Encoding: quoted-printable Message-ID: References: <33BCE1D0-BA6D-464C-B23D-69AF71356111@ajf.me> <3932E76B-DC75-40CD-8B1A-B84F387707CC@ajf.me> To: Bob Weinand X-Mailer: Apple Mail (2.2098) Subject: Re: [PHP-DEV] Fix division by zero to throw exception (round 2) From: ajf@ajf.me (Andrea Faulds) Hi Bob, > On 29 Jun 2015, at 16:54, Bob Weinand wrote: >=20 > I would like to bring this topic back up, as there were users confused = with it and it's absolutely not consistent what we have now. > See also https://bugs.php.net/bug.php?id=3D69957 (As I thought it was = non-intentional, I went ahead and "fixed" it, was reverted later, hence = discussing that now here.) >=20 > So, looks like there was some quick decisions and discussion I totally = had missed. > What we have now is: >=20 >> Am 03.04.2015 um 23:13 schrieb Dmitry Stogov : >>=20 >> So the summary: >>=20 >> 1) division by zero produces a warning and +/-INF IS_DOUBLE. = Compile-time >> evaluation is disabled. >>=20 >> 3) Modulo by zero produces Exception.Compile-time evaluation is = disabled. >=20 > Why? Why do we change the one but not the other? >=20 > Why does 0 % 0 throw an Exception, but 0 / 0 NAN? > Why does 1 % 0 throw an Exception, but 1 / 0 INF? >=20 > I'd like to either properly return 0, INF or NAN in both cases or in = none. >=20 > Having different rules for so similar operations is non-sense, I = think. It just is inconsistent and causes confusion. Those operations are not as similar as you think! In PHP, the complement of % isn=E2=80=99t /, it=E2=80=99s intdiv(). = intdiv() takes an integer divided and divisor, and produces an integer = quotient. % also takes an integer dividend and divisor, but produces an = integer remainder. intdiv() and % work with integer-only inputs and = always produce integer outputs. /, on the other hand, takes an integer or float dividend and divisor, = and produces an integer or float fractional result. It might as well = work only on floats and always produce floats, really, given that it = behaves the same as if it did. So intdiv() and % work on integers, and / works on integers and floats. = This informs how they handle certain error cases. For integer division and modulo (PHP=E2=80=99s intdiv() and %), the = accepted way to handle division by zero is to produce an error. = There=E2=80=99s no correct or useful result you can produce. In PHP, we = used to produce FALSE here, which is a different type (boolean). I = don=E2=80=99t think that was a good idea because of PHP=E2=80=99s weak = typing. If you use FALSE in some other arithmetic operation, it=E2=80=99ll= be coerced to zero, and produce weird results from other operations. = Sure, there=E2=80=99s an E_WARNING produced, but your code keeps running = and produces garbage. So, throwing an exception is safer and brings PHP = into line with established practice in other programming languages. For floating-point division (PHP=E2=80=99s /), on the other hand, errors = are usually handled differently. IEEE 754 defines special error values = we can produce: +Infinity, -Infinity and NaN. These are still values of = the float type, but they=E2=80=99re not normal numbers. They flow = through floating-point operations in a well-defined manner. For example, = if you do anything with a NaN and a NaN, you get a NaN, while if you = divide by zero, you get =C2=B1Infinity, and if you divide by Infinity, = you get =C2=B10. Again, PHP used to produce FALSE here, which has the = problems described earlier. =C2=B1Infinity and NaN, on the other hand, = flow properly through later arithmetic operations. If the error affects = your result, it will most likely be obvious, because it=E2=80=99ll be = =C2=B10, =C2=B1Infinity, or NaN. Yes, you result is garbage, but it=E2=80=99= s at least obviously so. And, like with the integer behaviour, this = brings PHP into line with established practice. tl;dr: intdiv() and % have matching behaviour because they work with = integers and that=E2=80=99s what=E2=80=99s usually does for them, / has = different behaviour because it works with floats and that=E2=80=99s = what=E2=80=99s done for them. I hope that makes sense. -- Andrea Faulds http://ajf.me/