This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Two float('nan') are not equal
Type: behavior Stage:
Components: None Versions: Python 2.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: jmfauth, mark.dickinson
Priority: normal Keywords:

Created on 2010-01-08 19:09 by jmfauth, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg97432 - (view) Author: jmf (jmfauth) Date: 2010-01-08 19:09
I did not find any report about this.

Windows 7, Python 2.6.4

>>> float('inf') == float('inf')
True
>>> float('-inf') == float('-inf')
True
>>> float('-inf') == float('inf')
False
>>> float('-inf') != float('inf')
True
>>> float('nan') == float('nan')
False
>>> float('nan') != float('nan')
True
>>> 

I'm not an expert on that field, I wonder if the following is
not related to a sign bit missmatch, see also Python 3.1.1 below.

>>> copysign(1, float('inf'))
1.0
>>> copysign(1, float('-inf'))
-1.0
>>> copysign(1, float('nan'))
-1.0
>>> copysign(1, float('-nan'))
-1.0
>>> 

Windows 7, Python 3.1.1

>>> float('inf') == float('inf')
True
>>> float('-inf') == float('-inf')
True
>>> float('-inf') == float('inf')
False
>>> float('-inf') != float('inf')
True
>>> float('nan') == float('nan')
False
>>> float('nan') != float('nan')
True
>>> 

Same behaviour as with Python 2.6.4

>>> copysign(1, float('inf'))
1.0
>>> copysign(1, float('-inf'))
-1.0
>>> copysign(1, float('nan'))
-1.0
>>> copysign(1, float('-nan'))  <<<<<<<<<<<<<<<<<<<
1.0
>>> 

Different behaviour from Python 2.6.4
msg97435 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-01-08 19:25
>>> float('nan') == float('nan')
False
>>> float('nan') != float('nan')
True

This is deliberate, though perhaps surprising if you haven't seen it before.  There's a long history of nan comparisons behaving this way (that is, x == nan always returns False, x != nan always returns True, even if x is a nan);  the behaviour comes from the IEEE 754 specification for floating-point arithmetic.

>>> copysign(1, float('nan'))
-1.0
>>> copysign(1, float('-nan'))
-1.0

This is also as expected:  the sign bit of a nan result (either as a result of conversion from string, or the result of an arithmetic operation) is probably best regarded as undefined.  The new string -> float conversion algorithms that are used in Python 3.1 (and in the upcoming 2.7) make sure that 'nan' and '-nan' are converted to nans with different signs, but again that's an implementation detail that shouldn't be relied upon.
History
Date User Action Args
2022-04-11 14:56:56adminsetgithub: 51909
2010-01-08 19:25:26mark.dickinsonsetstatus: open -> closed

nosy: + mark.dickinson
messages: + msg97435

resolution: not a bug
2010-01-08 19:09:18jmfauthcreate