Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.031 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'expressions': 0.07; 'float': 0.07; "ain't": 0.09; 'anders': 0.09; 'back.': 0.09; 'iterate': 0.09; 'keys,': 0.09; 'cc:addr:python-list': 0.11; '*should*': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'keys.': 0.16; 'nan': 0.16; 'nans': 0.16; 'number?': 0.16; 'rounding': 0.16; 'wrote:': 0.18; 'wed,': 0.18; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'certainly': 0.24; 'comparing': 0.24; 'replace': 0.24; 'skip': 0.24; 'cc:2**0': 0.24; 'compare': 0.26; 'equivalent': 0.26; 'possibly': 0.26; 'header:In- Reply-To:1': 0.27; 'am,': 0.29; "doesn't": 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'assumes': 0.31; 'container': 0.31; 'equality': 0.31; 'keys': 0.31; 'up:': 0.31; "we're": 0.32; 'not.': 0.33; 'table': 0.34; "can't": 0.35; 'anywhere': 0.35; 'something': 0.35; 'equal': 0.35; 'no,': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'building': 0.35; 'really': 0.36; 'doing': 0.36; 'should': 0.36; 'list': 0.37; 'area': 0.37; 'step': 0.37; 'problems': 0.38; 'e.g.': 0.38; 'does': 0.39; 'itself': 0.39; 'sure': 0.39; 'skip:x 10': 0.40; 'above,': 0.60; 'mentioned': 0.61; 'simply': 0.61; 'simple': 0.61; "you're": 0.61; 'back': 0.62; 'line,': 0.68; 'evaluate': 0.72; 'jul': 0.74; 'special': 0.74; 'subject:For': 0.78; 'float,': 0.84; 'forward.': 0.84; 'shared,': 0.84; 'single,': 0.84; 'to:none': 0.92; 'from.': 0.93; 'imagine': 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=lgMEh3wCIVkjtMIe+Dxdo3VjCG9jiEzk+iEKRJJ2hqw=; b=kbDLzW/UxtQY8tyG0sJJIqBa9Wygctl55fCt8x6pXB8XB3NFHPArBCrMDeNZSR2wDY fzW/8XTXN60ITnlHmjHJFnHEj0sSkx5ai3S0uQx3GB9U4icaKOOPiGDd8EBADlHaVpcc HtSnbrobryuetkZvoRIppHhpUBGLPxX2llzmwOaH/A9xcd+c7SMcyE4b2114481YmHf4 yPPi+IitbPPecVwLgfITY/gWQ1b97h62LSREF1rgVoPUwTS1XXJFZ6tQY6d8SpKZr+ed wj5XuZ58pLPOlxJNM1MlbGJms8My0ZoBebtJ435LY78VMG28VLWsm7/GgLEqvZR0kg+3 t4Ag== MIME-Version: 1.0 X-Received: by 10.220.249.6 with SMTP id mi6mr3333574vcb.33.1404832797058; Tue, 08 Jul 2014 08:19:57 -0700 (PDT) In-Reply-To: <53BC05FB.4050707@jmunch.dk> References: <53BC05FB.4050707@jmunch.dk> Date: Wed, 9 Jul 2014 01:19:57 +1000 Subject: Re: NaN comparisons - Call For Anecdotes From: Chris Angelico Cc: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 59 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1404832805 news.xs4all.nl 2967 [2001:888:2000:d::a6]:59540 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:74171 On Wed, Jul 9, 2014 at 12:53 AM, Anders J. Munch <2014@jmunch.dk> wrote: > In the end I came up with this hack: Every time I struct.unpack'd a > float, I check if it's a NaN, and if it is, then I replace it with a > reference to a single, shared, "canonical" NaN. That means that > container objects that skip __equal__ when comparing an object to > itself will work -- e.g. hash keys. Let's take a step back. No, let's take a step forward. And let's take a step back again. (And we're building a military-grade laser!) Why *should* all NaNs be equal to each other? You said on the other list that NaN==NaN was equivalent to (2+2)==(1+3), but that assumes that NaN is a single "thing". It's really describing the whole huge area of "stuff that just ain't numbers". Imagine if (x + y) wasn't 4, but was "table". And (a + b) turned out to be "cyan". Does table equal cyan, just because neither of them is a number? Certainly not. Neither should (inf - inf) be equal to (inf / inf). Both of those expressions evaluate to something that can't possibly be a number - it can't be anywhere on the number line, it can't be anywhere on the complex plane, it simply isn't a number. And they're not the same non-numeric "thing". For hash keys, float object identity will successfully look them up: >>> d={} >>> d[float("nan")]=1 >>> d[float("nan")]=2 >>> x=float("nan") >>> d[x]=3 >>> d[x] 3 >>> d {nan: 1, nan: 2, nan: 3} So I'm not sure where the problems come from. You can iterate over a dict's keys and look things up with them: >>> for k,v in d.items(): print(k,v,d[k]) nan 1 1 nan 2 2 nan 3 3 You can do a simple 'is' check as well as your equality check: if x is y or x == y: print("They're the same") But any time you compare floats for equality, you *already* have to understand what you're doing (because of rounding and such), so I don't see why the special case on NaN is significant, unless as mentioned above, you want all NaNs to be equal, which doesn't make sense. ChrisA