Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed6.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'mrab': 0.05; 'means,': 0.07; 'valueerror:': 0.07; 'python': 0.09; 'argument,': 0.09; 'propagate': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; '(but': 0.15; 'collections.': 0.16; 'equality.': 0.16; 'nan': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject:fails': 0.16; 'summary,': 0.16; 'wrote:': 0.17; 'detect': 0.17; 'jan': 0.18; '>>>': 0.18; '"",': 0.22; 'bug?': 0.22; 'defined': 0.22; 'second': 0.24; 'feature': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'looks': 0.26; 'skip:[ 10': 0.26; '(most': 0.27; '(as': 0.27; '2.6': 0.27; 'object,': 0.27; "doesn't": 0.28; 'header:X-Complaints-To:1': 0.28; '3.1': 0.29; 'consequence': 0.29; 'function': 0.30; 'file': 0.32; 'traceback': 0.33; 'values.': 0.33; 'to:addr:python-list': 0.33; 'equal': 0.33; 'list': 0.35; 'false': 0.35; 'pm,': 0.35; 'list.': 0.35; 'received:org': 0.36; 'except': 0.36; 'but': 0.36; 'item': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'decision': 0.60; 'skip:n 10': 0.63; 'email addr:gmail.com': 0.63; 'therefore': 0.65; 'believe': 0.69; 'correctly?': 0.84; 'insanity': 0.84; 'received:fios.verizon.net': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: a.index(float('nan')) fails Date: Fri, 26 Oct 2012 04:00:03 -0400 References: <5089F33A.8010804@mrabarnett.plus.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120824 Thunderbird/15.0 In-Reply-To: <5089F33A.8010804@mrabarnett.plus.com> 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: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351238424 news.xs4all.nl 6854 [2001:888:2000:d::a6]:43301 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32199 On 10/25/2012 10:19 PM, MRAB wrote: > On 2012-10-26 03:04, Terry Reedy wrote: >> On 10/25/2012 9:46 PM, mamboknave@gmail.com wrote: >>>>>> a = [float('nan'), 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>>>>> a >>> [nan, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>>>>> a.index(float('nan')) This is a second nan object, and it is not in the list. >>> Traceback (most recent call last): >>> File "", line 1, in >>> ValueError: list.index(x): x not in list >>> >>> That means, the function .index() cannot detect nan values. >>> It happens on both Python 2.6 and Python 3.1 >>> >>> Is this a bug? Or I am not using .index() correctly? >> >> It is a consequence of the following, which some people (but not all) >> believe is mandated by the IEEE standard. >> >> >>> nan = float('nan') >> >>> nan is nan >> True >> >>> nan == nan >> False >> >> >>> nanlist = [nan] >> >>> nan in nanlist >> True >> >>> nanlist.index(nan) >> 0 .index found the nan. >> Containment of nan in collection is tested by is, not ==. >> >> >>> nan2 = float('nan') >> >>> nan2 is nan >> False >> >>> nan2 == nan >> False >> >>> nan2 in nanlist >> False >> > In summary, .index() looks for an item which is equal to its argument, > but it's a feature of NaN (as defined by the standard) that it doesn't > equal NaN, therefore .index() will never find it. Except that is *does* find the particular nan object that is in the collection. So nan in collection and list.index(nan) look for the nan by identity, not equality. This inconsistency is an intentional decision to not propagate the insanity of nan != nan to Python collections. -- Terry Jan Reedy