Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > a.index(float('nan')) fails

Reply
Thread Tools

a.index(float('nan')) fails

 
 
mamboknave@gmail.com
Guest
Posts: n/a
 
      10-26-2012
>>> 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'))

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
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?

Thanks!
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      10-26-2012
On 10/25/2012 9:46 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) 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'))

> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> 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

Containment of nan in collection is tested by is, not ==.

>>> nan2 = float('nan')
>>> nan2 is nan

False
>>> nan2 == nan

False
>>> nan2 in nanlist

False

--
Terry Jan Reedy

 
Reply With Quote
 
 
 
 
Cameron Simpson
Guest
Posts: n/a
 
      10-26-2012
On 25Oct2012 18:46, (E-Mail Removed) <(E-Mail Removed)> 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'))
| Traceback (most recent call last):
| File "<stdin>", line 1, in <module>
| 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?

The special NaN float value always compares unequal, even to itself.
IEEE floating point FTW!

You're using index incorrectly, but only because it relies on ==
returning True, which it won't.

You can use math.isnan:

http://docs.python.org/library/math.html#math.isnan
http://docs.python.org/py3k/library/...tml#math.isnan

for the test instead. Nan requires special handling.

Cheers,
--
Cameron Simpson <(E-Mail Removed)>

I'm not making any of this up you know. - Anna Russell
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      10-26-2012
On 2012-10-26 03:04, Terry Reedy wrote:
> On 10/25/2012 9:46 PM, (E-Mail Removed) 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'))

>> Traceback (most recent call last):
>> File "<stdin>", line 1, in <module>
>> 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
>
> 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.

Another consequence is that the presence of a NaN in a list prevents
..sort() from sorting correctly.

 
Reply With Quote
 
mamboknave@gmail.com
Guest
Posts: n/a
 
      10-26-2012
On Thursday, October 25, 2012 7:16:02 PM UTC-7, Cameron Simpson wrote:

Of course!! How could I get into that trap??

Thanks to you & to Terry
 
Reply With Quote
 
mamboknave@gmail.com
Guest
Posts: n/a
 
      10-26-2012
On Thursday, October 25, 2012 7:16:02 PM UTC-7, Cameron Simpson wrote:

Of course!! How could I get into that trap??

Thanks to you & to Terry
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      10-26-2012
On Thu, 25 Oct 2012 18:46:20 -0700, mamboknave wrote:

> 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?


You shouldn't be using index() or == to detect NANs. The right way to
detect NANs is with the math.isnan() function.

The list.index method tests for the item with equality. Since NANs are
mandated to compare unequal to anything, including themselves, index
cannot match them.

Try this instead:

from math import isnan

def find_nan(seq):
"""Return the index of the first NAN in seq, otherwise None."""
for i, x in enumerate(seq):
if isnan(x):
return i


For old versions of Python that don't provide an isnan function, you can
do this:

def isnan(x):
return x != x



--
Steven
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      10-26-2012
On Thu, 25 Oct 2012 22:04:52 -0400, Terry Reedy wrote:

> 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


The IEEE 754 standard says nothing about object identity. It only
discusses value equality.

> >>> nan == nan

> False


IEEE 754 states that all NANs compare unequal to everything, including
NANs with the same bit value. It doesn't make an exception for
comparisons with itself.

I'm not entirely sure why you suggest that there is an argument about
what IEEE 754 says about NANs. As far as I can see, the argument is
whether or not language designers should pick and choose which bits of
the standard they want to follow, thus taking a step backwards to the
chaos of numerical computing prior to the IEEE 754 standard.

Seriously, there are some real horror stories about numerical computing
back in the Bad Old Days. Like computers where 1.0/x could give a divide-
by-zero error even though x == 0 returned False.



--
Steven
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      10-26-2012
On 10/25/2012 10:44 PM, Steven D'Aprano wrote:
> On Thu, 25 Oct 2012 22:04:52 -0400, Terry Reedy wrote:
>
>> 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

>
> The IEEE 754 standard says nothing about object identity. It only
> discusses value equality.
>
>> >>> nan == nan

>> False

>
> IEEE 754 states that all NANs compare unequal to everything, including
> NANs with the same bit value. It doesn't make an exception for
> comparisons with itself.
>
> I'm not entirely sure why you suggest that there is an argument about
> what IEEE 754 says about NANs.


I did not do so.

> As far as I can see, the argument is
> whether or not language designers should pick and choose which bits of
> the standard they want to follow, thus taking a step backwards to the
> chaos of numerical computing prior to the IEEE 754 standard.


There has been disagreement about whether the standard mandates that
Python behave the way it does. That is a fact, but I have no interest in
discussing the issue.

--
Terry Jan Reedy

 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      10-26-2012
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, (E-Mail Removed) 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 "<stdin>", line 1, in <module>
>>> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Constant.t fails 240 of 272 tests and recurs.t fails 1 of 25 tests on HPUX using perl 5.8.7 dayo Perl Misc 11 12-16-2005 09:09 PM
slideshow fails, Firefox debugger also fails lkrubner@geocities.com Javascript 2 12-23-2004 06:22 PM
Wireless Zero Configuration Servoce fails to start andrew Wireless Networking 0 07-28-2004 03:08 PM
IAS fails with certs from Stand Alone CA Harrison Midkiff Wireless Networking 2 07-22-2004 09:45 PM
Forms Authentication Fails Between ASP.NET 1.0 and 1.1 Applications (Cookie Decryption Fails) John Saunders ASP .Net 1 11-18-2003 03:25 PM



Advertisments