(original) (raw)

On 4/27/2011 2:15 PM, Mark Dickinson wrote:
On Wed, Apr 27, 2011 at 7:41 PM, Glenn Linderman <v+python@g.nevcal.com> wrote:  
One issue that I don't fully understand: I know there is only one instance  
of None in Python, but I'm not sure where to discover whether there is only  
a single, or whether there can be multiple, instances of NaN or Inf. The  
IEEE 754 spec is clear that there are multiple bit sequences that can be  
used to represent these, so I would hope that there can be, in fact, more  
than one value containing NaN (and Inf).

This would properly imply that a collection should correctly handle the case
of storing multiple, different items using different NaN (and Inf)
instances. A dict, for example, should be able to hold hundreds of items
with the index value of NaN.

The distinction between "is" and "==" would permit proper operation, and I
believe that Python's "rebinding" of names to values rather than the copying
of values to variables makes such a distinction possible to use in a correct
manner.




For infinities, there's no issue: there are exactly two distinct
infinities (+inf and -inf), and they don't have any special properties
that affect membership tests. Your float-keyed dict can contain both
+inf and -inf keys, or just one, or neither, in exactly the same way
that it can contain both +5.0 and -5.0 as keys, or just one, or
neither.

For nans, you *can* put multiple nans into a dictionary as separate
keys, but under the current rules the test for 'sameness' of two nan
keys becomes a test of object identity, not of bitwise equality.
Python takes no notice of the sign bits and 'payload' bits of a float
nan, except in operations like struct.pack and struct.unpack. For
example:



Thanks, Mark, for the succinct description and demonstration. Yes,
only two Inf values, many possible NaNs. And this is what I would
expect.



I would not, however expect the original case that was described:

>>> nan = float('nan')


>>> nan == nan


False


>>> [nan] == [nan]


True # also True in tuples, dicts, etc.