Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:73488 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 63695 invoked from network); 31 Mar 2014 11:02:32 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 31 Mar 2014 11:02:32 -0000 Authentication-Results: pb1.pair.com smtp.mail=inefedor@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=inefedor@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.217.180 as permitted sender) X-PHP-List-Original-Sender: inefedor@gmail.com X-Host-Fingerprint: 209.85.217.180 mail-lb0-f180.google.com Received: from [209.85.217.180] ([209.85.217.180:44265] helo=mail-lb0-f180.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id E0/92-44499-64B49335 for ; Mon, 31 Mar 2014 06:02:31 -0500 Received: by mail-lb0-f180.google.com with SMTP id 10so5553211lbg.39 for ; Mon, 31 Mar 2014 04:02:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=content-type:to:cc:subject:references:date:mime-version :content-transfer-encoding:from:message-id:in-reply-to:user-agent; bh=LlAP8KwUSDXPDC4jmLirBDec9xiaWYNkKEtOIST06os=; b=zmwa5x0o2p/i7teqECS9zmU6LMe2g9QRZkSWpd84HwcGeCJeAFMMYbiGbEKT/C736Z +iaC2Vevn5PmwuJpl2i2nPf/iTJstD2DVhK4MQzILcZmYlJyTUX9F3ZZ3mzyXkkm/htc 9KZaR+/WVCqlVeZzuYqZbHXNvlCeGAuormrC0JfVy/2TnhijJOQmkkiZj5AuupsPRAr/ 4l2bblMcLbPcQlo/fj6VgT0f49VvQo6gymQjzhE3YsiDbLN4P8ibRQBeEyQ6bVTLHPIn zxLCubf+4xNHPtstpdxLg44iU+BVhRoovSFmqrxUKrMqfevhwnNRfODH5XAhK4OuHeW6 lhlg== X-Received: by 10.112.85.6 with SMTP id d6mr17482302lbz.8.1396263747144; Mon, 31 Mar 2014 04:02:27 -0700 (PDT) Received: from nikita-pc (93-80-62-168.broadband.corbina.ru. [93.80.62.168]) by mx.google.com with ESMTPSA id cq4sm13698504lad.5.2014.03.31.04.02.26 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 31 Mar 2014 04:02:26 -0700 (PDT) Content-Type: text/plain; charset=koi8-r; format=flowed; delsp=yes To: "Crypto Compress" Cc: "PHP Developers Mailing List" References: <5339497A.2040506@googlemail.com> Date: Mon, 31 Mar 2014 15:02:25 +0400 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Message-ID: In-Reply-To: <5339497A.2040506@googlemail.com> User-Agent: Opera Mail/12.16 (Win32) Subject: Re: [PHP-DEV] usort callback should always return integer (not float) From: inefedor@gmail.com ("Nikita Nefedov") On Mon, 31 Mar 2014 14:54:50 +0400, Crypto Compress wrote: > Am 31.03.2014 11:00, schrieb Nikita Nefedov: >> Hi, >> >> I've just stumbled upon this limitation in sorting (compare) callbacks. >> Is there any >> reason why we can't handle floats in this case? >> >> I think it would be nice to be able to return float from the callback >> because it's often happens that all it does is just `return >> $a->someMethod() - $b->someMethod();` (where someMethod() returns >> float) or `return $a["something"] - >> $b["something"];` and to make it work I need to write additional if >> statement. I think the reasoning could be complex behavior of doubles? >> Especially comparing doubles. But will we see any double-related >> pitfalls when all our code does with it is `return retval < 0 ? -1 : >> retval > 0 ? 1 : 0;` (so essentially - compares them to zero)? >> >> >> I made a PR [1] on github for 5.6 version. I think this change is >> pretty small and could go into 5.6 not breaking our release process >> rules? Though it WILL introduce some BC breaks for userland code that >> uses floats. But this code was already broken when it was relying on >> old behavior. >> >> [1] https://github.com/php/php-src/pull/638 >> > > Hi, > > try this: > > $retval = 1.0 - 0.7 - 0.3; > var_dump($retval < 0 ? 'lt' : $retval > 0 ? 'gt' : 'equal'); // result > is *not* equal > > cryptocompress Yes, I understand the pitfalls of floating point number arithmetics. But isn't it userland's problem?