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

Mark Adam dreamingforward at gmail.com
Sat Oct 27 14:33:51 EDT 2012


On Thu, Oct 25, 2012 at 9:04 PM, Terry Reedy <tjreedy at udel.edu> wrote:
> On 10/25/2012 9:46 PM, mamboknave at 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'))
>>
>> 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

It should be noted, for the record, that "nan is nan" returning True
has nothing to do with the concept of numbers or the IEEE standard and
is purely a consequence that Python runs on hardware with memory
addresses and such.

>>>> nan == nan
> False

Here, equality, IS about number and this is appropriate and conforms
to the IEEE standard.

>>>> nanlist = [nan]
>>>> nan in nanlist
> True
>>>> nanlist.index(nan)
> 0

Here you just see an phenomenon with the python object/reference
model, which, being as it is, has nothing to do with numbers.  This is
an area which, potentially could be changed in Python without
violating the IEEE standard whatsoever.

Mark



More information about the Python-list mailing list