Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #74299

Re: NaN comparisons - Call For Anecdotes

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.018
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'mrab': 0.05; 'cc:addr :python-list': 0.11; "wouldn't": 0.14; '1.5,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'nan': 0.16; 'nans': 0.16; 'somewhere.': 0.16; 'sorted()': 0.16; 'sorting': 0.16; 'sorting.': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'seems': 0.21; '>>>': 0.22; 'rules': 0.22; 'cc:addr:python.org': 0.22; 'comparing': 0.24; 'documented': 0.24; 'skip:l 30': 0.24; 'cc:2**0': 0.24; 'sort': 0.25; 'header:In- Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'end,': 0.31; 'equality': 0.31; 'tuples': 0.31; 'probably': 0.32; 'supposed': 0.32; 'beginning': 0.33; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'being': 0.38; 'pm,': 0.38; 'sure': 0.39; 'simple': 0.61; "you're": 0.61; 'happen': 0.63; 'choose': 0.64; 'default': 0.69; 'jul': 0.74; 'special': 0.74; 'subject:For': 0.78; 'to:none': 0.92; 'reliable,': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=vA9idHiR5KDhQYVxkQL2HePYzjZTKJ/5HauKbnUyyfY=; b=L6KPnUC4qsqzCAG7JCfxMkFsTx2s0ColgT5tq7/Oz9I/6p2sW+3MzUPggr9JcEkN4W dBGKhScpFX+ljfrh7h4rh41WvUkOhq+mzHULov39e4fLnIVLObIM3N/LW4tkYsTOT2BF u9CQhN9CwdqtD55Bqcx9UO+Oz+I+1/cVJIzz56gz5YqPn+nSkCobi/9HUtjE1ocav5w0 wTeJK93aQnFH/dM9Utxfji7qyoZZrLOHH9sP0s5NQjnUpGwP4+h8VjOXAmzQITI2+/q3 dDxJZuB2mEzY8y8tFThWA5L3rQFkdTZEpSrOkpNHqfYOAWvj295FPYkIa4/if/21IXd3 RPRQ==
MIME-Version 1.0
X-Received by 10.52.108.234 with SMTP id hn10mr7698102vdb.4.1404992280246; Thu, 10 Jul 2014 04:38:00 -0700 (PDT)
In-Reply-To <53BE78D0.60802@mrabarnett.plus.com>
References <53BC05FB.4050707@jmunch.dk> <53BD70F4.4000504@stoneleaf.us> <53BDAF90.8010709@jmunch.dk> <CAPM-O+yiioLm-UiV2fYqc2n9qZrN+_v6RkeqJ7mOQ3Kq+cH0CQ@mail.gmail.com> <53BDCA33.3020100@jmunch.dk> <85oawyt4ho.fsf@benfinney.id.au> <53BE78D0.60802@mrabarnett.plus.com>
Date Thu, 10 Jul 2014 21:38:00 +1000
Subject Re: NaN comparisons - Call For Anecdotes
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.11724.1404992282.18130.python-list@python.org> (permalink)
Lines 27
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1404992282 news.xs4all.nl 2941 [2001:888:2000:d::a6]:44026
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:74299

Show key headers only | View raw


On Thu, Jul 10, 2014 at 9:28 PM, MRAB <python@mrabarnett.plus.com> wrote:
> I can think of one place where equality of NaNs would be useful:
> sorting.
>
> However, in that use-case, you would also want it to be orderable,
> perhaps greater than any other non-NaN float.

In that case, you're setting special rules, so you probably want a
key-based sort. Since the sorting of tuples works by comparing
elements sequentially, it's simple enough:

>>> l=[1.0,2.0,1.5,float("nan"),1.7]
>>> sorted(l,key=lambda n: (n!=n, n))
[1.0, 1.5, 1.7, 2.0, nan]

I'm not sure what's supposed to happen if you just let sorted() go
with its default comparisons. It seems to put the nan at the end, but
I wouldn't bet on that being reliable, unless it's documented
somewhere. Of course, with a key sort you can choose to put NaNs at
the beginning just as easily:

>>> sorted(l,key=lambda n: (n==n, n))
[nan, 1.0, 1.5, 1.7, 2.0]

You want custom rules for sorting? You got 'em.

ChrisA

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: NaN comparisons - Call For Anecdotes Chris Angelico <rosuav@gmail.com> - 2014-07-10 21:38 +1000

csiph-web