Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:37406 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 17275 invoked from network); 2 May 2008 17:38:56 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 May 2008 17:38:56 -0000 Authentication-Results: pb1.pair.com header.from=jbondc@openmv.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=jbondc@openmv.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain openmv.com from 64.15.152.204 cause and error) X-PHP-List-Original-Sender: jbondc@openmv.com X-Host-Fingerprint: 64.15.152.204 mail.ca.gdesolutions.com Received: from [64.15.152.204] ([64.15.152.204:59508] helo=mail.ca.gdesolutions.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 02/D2-01733-FA15B184 for ; Fri, 02 May 2008 13:38:55 -0400 Received: from localhost (localhost [127.0.0.1]) by mail.ca.gdesolutions.com (Postfix) with ESMTP id 664EF5C23; Fri, 2 May 2008 13:38:52 -0400 (EDT) X-Virus-Scanned: amavisd-new at gdesolutions.com Received: from mail.ca.gdesolutions.com ([127.0.0.1]) by localhost (mail.ca.gdesolutions.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id IMyxAB1amZpJ; Fri, 2 May 2008 13:38:48 -0400 (EDT) Received: from jbondc (modemcable158.97-203-24.mc.videotron.ca [24.203.97.158]) by mail.ca.gdesolutions.com (Postfix) with ESMTP id ABC565C1C; Fri, 2 May 2008 13:38:48 -0400 (EDT) To: "'Pierre Joye'" Cc: References: <001401c8ac74$dda6d060$98f47120$@com> In-Reply-To: Date: Fri, 2 May 2008 13:38:47 -0400 Message-ID: <000001c8ac7b$60d0f140$2272d3c0$@com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 12.0 Thread-Index: AcisdqOL8m7tB+jKTCOHrZmdcY3NhwAAO+Sw Content-Language: en-ca Subject: RE: [PHP-DEV] Float comparison From: jbondc@openmv.com ("Jonathan Bond-Caron") Thanks Pierre, I realize it's not a "bug" and I've read some "bogus" reports :) But since (float)(string)$test == (double)1960 is 'correct' My question is: can php do the conversion internally? It would seem *very* convenient for users, If not changing the float behavior, I'm advocating for a (numeric) type that does this magic conversion. (numeric) == (float)(string) in en_US locale / convert to string with a "." seperator Let's say it's hard to promote PHP as enterprise ready without dealing so well with numbers. I don't know the float-to-string internals but it seems to do 'right' thing. We get '1960', then converting back to a float, all accuracy bits are 0, so (numeric)$test == (double)1960 I would guess adding a new type is a big amount work? -----Original Message----- From: Pierre Joye [mailto:pierre.php@gmail.com] Sent: May 2, 2008 1:05 PM To: Jonathan Bond-Caron Cc: internals@lists.php.net Subject: Re: [PHP-DEV] Float comparison 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