Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:59885 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 16019 invoked from network); 13 Apr 2012 16:09:29 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Apr 2012 16:09:29 -0000 Authentication-Results: pb1.pair.com header.from=smalyshev@sugarcrm.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=smalyshev@sugarcrm.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain sugarcrm.com designates 67.192.241.143 as permitted sender) X-PHP-List-Original-Sender: smalyshev@sugarcrm.com X-Host-Fingerprint: 67.192.241.143 smtp143.dfw.emailsrvr.com Linux 2.6 Received: from [67.192.241.143] ([67.192.241.143:43874] helo=smtp143.dfw.emailsrvr.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 1A/0F-35770-8BF488F4 for ; Fri, 13 Apr 2012 12:09:29 -0400 Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp4.relay.dfw1a.emailsrvr.com (SMTP Server) with ESMTP id 5946B3A06F4; Fri, 13 Apr 2012 12:09:26 -0400 (EDT) X-Virus-Scanned: OK Received: by smtp4.relay.dfw1a.emailsrvr.com (Authenticated sender: smalyshev-AT-sugarcrm.com) with ESMTPSA id E17863A064C; Fri, 13 Apr 2012 12:09:24 -0400 (EDT) Message-ID: <4F884FB4.70901@sugarcrm.com> Date: Fri, 13 Apr 2012 09:09:24 -0700 Organization: SugarCRM User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:11.0) Gecko/20120327 Thunderbird/11.0.1 MIME-Version: 1.0 To: Gustavo Lopes CC: PHP Developers Mailing List References: In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] '9223372036854775807' == '9223372036854775808' From: smalyshev@sugarcrm.com (Stas Malyshev) Hi! > There are other situations where the result of the comparison may be > "inaccurate" -- in the sense that two strings may be constructed as > representing different numbers, but they compare equal. > > * Comparing two different real numbers that map to the same double > precision number: > var_dump("1.9999999999999999" == "2"); //true For floats, there's no accurate comparison anyway, it is a known fact. However for edge cases like one mentioned in the subject, I think it may make sense to make an exception, since it indeed is kind of obscure why it works this way and I do not see why this behavior - while having its (obscure) reasons - would be useful to anybody. > However, taking the last case an example, this is the same that happens if > you compare: > var_dump((int)"9223372036854775807" == (double)"9223372036854775808"); > //true This, however is a different case since you explicitly coerce the types and you must know that both conversions are lossy. It's like doing substr($a, 0, 1) == substr($b, 0, 1) - of course it can return true even if $a and $b different. When you convert bigger type (string) to smaller type (int) you must accept the potential loss or check for it if it's important. However I think it would make sense not to use this conversion in string comparisons when we know it's lossy - it seems to be outside of the use case for such comparisons and it seems apparent by now that it is hard for people to understand why it works this way. -- Stanislav Malyshev, Software Architect SugarCRM: http://www.sugarcrm.com/ (408)454-6900 ext. 227