Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'false.': 0.09; 'keys,': 0.09; 'lookup': 0.09; 'raises': 0.09; 'thus,': 0.09; 'cc:addr :python-list': 0.11; 'def': 0.12; 'thread': 0.14; "wouldn't": 0.14; 'posted': 0.15; '"=="': 0.16; '"is"': 0.16; 'dict': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'nan': 0.16; 'nans,': 0.16; 'other:': 0.16; 'shortcut': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'wed,': 0.18; '>>>': 0.22; 'example': 0.22; 'cc:addr:python.org': 0.22; 'comparing': 0.24; 'test.': 0.24; 'cc:2**0': 0.24; 'compare': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; '"",': 0.31; 'equality': 0.31; 'keyerror:': 0.31; 'file': 0.32; 'class': 0.32; 'this.': 0.32; 'another': 0.32; '(most': 0.33; 'skip:_ 10': 0.34; "i'd": 0.34; 'could': 0.34; "can't": 0.35; 'operations': 0.35; 'test': 0.35; 'received:google.com': 0.35; 'false': 0.36; 'otherwise.': 0.36; 'rather': 0.38; 'anything': 0.39; 'recent': 0.39; "couldn't": 0.39; 'itself': 0.39; 'even': 0.60; 'fact,': 0.69; 'jul': 0.74; 'surprise': 0.74; 'subject:For': 0.78; 'to:none': 0.92 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=HsTApH63BZvKEyZb1dqRu1fsS9Y0foHRRe7c9vWd1T0=; b=DL5oYqCIvgmptgh1Ca0IemWl11st4FhOPwhf/smLgBKbC1SVCCEacY1KgP/ln1zCHt bIHAK8y/VEhkjlrZ7twfoYqooIi0atlQ5CypEBbgu8Law7BMeD8pu9+cesw9Cv+RLgra NTcrq7y4T47lFBxpsgqV3UyA4TNeMYUihJDFuXi3F9mv5lmkiMwURy/yAbATfv3nzd1Q SKxvEpLSvd4yXVSJyGMHp40Pj6plIj9LHRGJI1KHXTcpDMZlOrAyLUzCUp3NxigeY62X YOhx9IqQdT+nTEF7uo9wAwDq3Iy9Y0DUIcm1YPgode1dZeBr1lSWSfykwCwhWELdkQW2 Aacg== MIME-Version: 1.0 X-Received: by 10.52.232.200 with SMTP id tq8mr8177368vdc.32.1404842093270; Tue, 08 Jul 2014 10:54:53 -0700 (PDT) In-Reply-To: <871ttvahaq.fsf@elektro.pacujo.net> References: <53BC05FB.4050707@jmunch.dk> <878uo3akqy.fsf@elektro.pacujo.net> <871ttvahaq.fsf@elektro.pacujo.net> Date: Wed, 9 Jul 2014 03:54:52 +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: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1404842102 news.xs4all.nl 2859 [2001:888:2000:d::a6]:36461 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:74192 On Wed, Jul 9, 2014 at 3:31 AM, Marko Rauhamaa wrote: > Chris Angelico : > >> I'd say it would surprise people rather a lot if operations like dict >> insertion/lookup could trigger arithmetic exceptions. :) > > That wouldn't trigger exceptions. > > Dict operations do an "is" test before an "==" test. In fact, you > couldn't even use NaN as a dict key otherwise. Thus, dict operations > never test NaN == NaN. Check out the example I posted early in this thread of a dict with three keys, all of them NaN. And note that hash(float("nan"))==0. Now try looking up d[0]. Before it raises KeyError, it has to compare that 0 for equality with each of the nans, because it can't shortcut it based on the hash. In fact, I can prove it thus: >>> class X: def __eq__(self, other): if self is other: print("Comparing against self - I am me!") return True print("Comparing against",other,"-",id(other)) return False def __hash__(self): return 0 >>> d[X()] Comparing against nan - 18777952 Comparing against nan - 19624864 Comparing against nan - 18776272 Traceback (most recent call last): File "", line 1, in d[X()] KeyError: <__main__.X object at 0x016B40D0> Any lookup of anything with a hash of 0 will do this. 0 itself (as any type of number), another NaN, or anything at all. For the dict to work sanely, these comparisons have to work and be False. ChrisA