Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:37405 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 16117 invoked from network); 2 May 2008 17:36:39 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 May 2008 17:36:39 -0000 Authentication-Results: pb1.pair.com smtp.mail=truth@proposaltech.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=truth@proposaltech.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain proposaltech.com from 66.51.199.86 cause and error) X-PHP-List-Original-Sender: truth@proposaltech.com X-Host-Fingerprint: 66.51.199.86 exvs01.ex.dslextreme.net Windows 2000 SP4, XP SP1 Received: from [66.51.199.86] ([66.51.199.86:59695] helo=EXVS01.ex.dslextreme.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 0E/82-01733-5215B184 for ; Fri, 02 May 2008 13:36:37 -0400 Received: from 69.236.135.99 ([69.236.135.99]) by EXVS01.ex.dslextreme.net ([192.168.7.220]) via Exchange Front-End Server owa.dslextreme.net ([192.168.7.126]) with Microsoft Exchange Server HTTP-DAV ; Fri, 2 May 2008 17:39:13 +0000 Received: from inspiron by owa.dslextreme.net; 02 May 2008 10:36:33 -0700 To: internals@lists.php.net In-Reply-To: References: <001401c8ac74$dda6d060$98f47120$@com> Content-Type: text/plain Content-Transfer-Encoding: 7bit Date: Fri, 02 May 2008 10:36:33 -0700 Message-ID: <1209749793.1075.54.camel@inspiron.local> Mime-Version: 1.0 X-Mailer: Evolution 2.8.2 Subject: Re: [PHP-DEV] Float comparison From: truth@proposaltech.com (Todd Ruth) I'm afraid you'll find Pierre's response representative. The php devs seem to consider the fact that a theoretically perfect "==" doesn't exist to mean that the improvements that would cover 99.9% of user issues with float == shouldn't be made. It could be worse, however. At least the cast to string works as one would expect. Just imagine if those who oppose improving "==" had their logic extended to the cast to string. Php might to the following: might output "1.19999999999999996447286321199499070644378662109375" If someone filed a bug report, they could refer you to that paper and tell you there's no bug because you're trying to get a string from float and you never know what you might get. So we can at least be glad that casts to string work as the average person would expect. We can hope that someday the fact that the cast to string works as expected will inspire them believe that making "==" work as expected wouldn't be a bad thing. Seriously though, the biggest issue I see with improving "==" is the implication for other features. For example, what should the following code do? $a[.3+.4+.5] = "hello"; print isset($a[1.2]) Should floats be allowed as array indices? I would say no because I'd rather floats be converted to strings before being used as array indices. That would make the above code work as the average person would expect. Unfortunately, that would be a huge BC break. However, perhaps the perfect shouldn't be the enemy of the good and we shouldn't let the inability to fix floats as array indices be a reason not to improve "==". - Todd On Fri, 2008-05-02 at 19:04 +0200, Pierre Joye wrote: > On Fri, May 2, 2008 at 6:52 PM, Jonathan Bond-Caron wrote: > > Hi, > > > > > > > > I'm new to the PHP internals list and I'm posting an issue I'm sure has been > > mentioned before -- float comparison > > > > > > > > I'd like to know if there are reasons not to change the default behavior > > when comparing floats. > > > > > > > > i.e. This would seem logical to me: > > > > > > > > $test = 19.6*100; > > > > if((float)(string)$test == (double)1960) > > > > echo "GOOD BEHAVIOR"; > > > > > > > > // Implicitely convert the float to string then float (en_US > > locale) > > > > if($test == (double)1960) > > > > echo "THIS SHOULD PASS by casting > > (float)(string)$test internally..."; > > > > else > > > > echo "NOT GOOD BEHAVIOR"; > > > > > > > > // Exact comparison would still fail > > > > if($test !== (double)1960) > > > > echo "GOOD BEHAVIOR"; > > > > > > > > Any reason why $test == (double)1960 should fail? I realized we are > > comparing two floats, but couldn't php internally convert to string then > > float - for the non strict (===) case > > From our bug report: > " > Floating point values have a limited precision. Hence a value might > not have the same string representation after any processing. That also > includes writing a floating point value in your script and directly > printing it without any mathematical operations. > > If you would like to know more about "floats" and what IEEE > 754 is read this: > http://docs.sun.com/source/806-3568/ncg_goldberg.html > > Thank you for your interest in PHP. > " > > We use this text as automatic reply for bugs about floating points (aka bogus). > > Cheers, > -- > Pierre > > http://blog.thepimp.net | http://www.libgd.org >