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

T

Thomas Rachel

Am 27.10.2012 06:48 schrieb Dennis Lee Bieber:
I don't know about the more modern calculators, but at least up
through my HP-41CX, HP calculators didn't do (binary) "floating
point"... They did a form of BCD with a fixed number of significant
/decimal/ digits

Then, what about sqrt(x)**2 or arcsin(sin(x))? Did that always return
the original x?

Thomas
 
N

Nobody

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

AFAICT, it isn't specific to NaN. The test used by .index() and "in"
appears to be equivalent to:

def equal(a, b):
return a is b or a == b

IOW, it always checks for object identity before equality.

Replacing NaN with an instance of a user-defined class with a
non-reflexive __eq__() method supports this:
class Foo(object):
= def __eq__(self, other):
= return False
=
a = Foo()
b = Foo()
a in [1,2,a,3,4] True
b in [1,2,a,3,4] False
[1,2,a,3,4].index(a)
2
[1,2,a,3,4].index(b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: <__main__.Foo object at 0x7fa7055b0550> is not in list
 
N

Nobody

Am 27.10.2012 06:48 schrieb Dennis Lee Bieber:


Then, what about sqrt(x)**2 or arcsin(sin(x))? Did that always return
the original x?

I'd be impressed if it managed the latter, i.e. arcsin(sin(0))==0 while
arcsin(sin(pi))==pi ;)
 
D

Dennis Lee Bieber

Am 27.10.2012 06:48 schrieb Dennis Lee Bieber:


Then, what about sqrt(x)**2 or arcsin(sin(x))? Did that always return
the original x?
The HPs probably not -- since they truncated at the decimal digit
level. The models with guard digits might have displayed the original,
as they used the guard digits to round results back up the display/entry
width (and to handle the infamous repeated addition of small amounts
<G>)
 
M

Mark Adam

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

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
 
E

Ethan Furman

Steven said:
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.

This is incorrect. .index() uses identity first, then equality, and
will match the same NaN in a list. The OP's problem was in using a
different NaN.

Having said that, your find_nan() solution is probably the one to use
anyway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top