Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:37404 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 14172 invoked from network); 2 May 2008 17:24:52 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 May 2008 17:24:52 -0000 Authentication-Results: pb1.pair.com header.from=andrey@mysql.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=andrey@mysql.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain mysql.com from 64.212.38.36 cause and error) X-PHP-List-Original-Sender: andrey@mysql.com X-Host-Fingerprint: 64.212.38.36 mailgate-b.mysql.com Linux 2.6 Received: from [64.212.38.36] ([64.212.38.36:45481] helo=mailgate-b.mysql.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id FD/22-01733-16E4B184 for ; Fri, 02 May 2008 13:24:50 -0400 Received: from mail.mysql.com (mailsend.mysql.com [10.100.1.123]) by mailgate-b.mysql.com (8.13.8/8.13.8) with ESMTP id m42HOja1025953 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Fri, 2 May 2008 19:24:45 +0200 Received: from [134.155.144.194] (vpn-144-194.vpn.uni-mannheim.de [134.155.144.194]) (authenticated bits=0) by mail.mysql.com (8.13.3/8.13.3) with ESMTP id m42HOfJU015649 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Fri, 2 May 2008 19:24:42 +0200 Message-ID: <481B4E5B.9020002@mysql.com> Date: Fri, 02 May 2008 19:24:43 +0200 User-Agent: Thunderbird 2.0.0.12 (X11/20080226) MIME-Version: 1.0 To: Jonathan Bond-Caron CC: internals@lists.php.net References: <001401c8ac74$dda6d060$98f47120$@com> In-Reply-To: <001401c8ac74$dda6d060$98f47120$@com> X-Enigmail-Version: 0.95.6 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Float comparison From: andrey@mysql.com (Andrey Hristov) Hi, 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 > > > > Thanks > Never compare directly floats but subtract one from the other and check whether the difference is bigger or smaller than a small predefined number. Regards, Andrey -- Andrey Hristov, Connectors Software Developer Sun Microsystems GmbH, Sonnenallee 1, 85551 Kirchheim-Heimstetten Amtsgericht Muenchen: HRB161028 Geschaeftsfuehrer: Thomas Schroeder, Wolfgang Engels, Dr. Roland Boemer Vorsitzender des Aufsichtsrates: Martin Haering