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.

Author mark.dickinson
Recipients facundobatista, mark.dickinson
Date 2008-01-31.04:46:13
SpamBayes Score 0.0009371092
Marked as misclassified No
Message-id <1201754782.28.0.976193131321.issue1979@psf.upfronthosting.co.za>
In-reply-to
Content
For Python 3.0 the decimal module got rich comparisons.  Those comparisons have somewhat unconventional 
behavior with respect to NaNs, as seen below:  <, <= and == comparisons involving NaNs always return False, 
while >, >= and != comparisons always return True.

The Decimal specification has nothing helpful to say about comparisons involving NaNs.  But reading IEEE-
754r (on which the Decimal specification is closely based), there are two possible options:

 (1) have comparisons involving NaNs (except for !=) raise InvalidOperation in the context, and hence give a 
Python exception (assuming that InvalidOperation isn't trapped.)

 (2) have comparisons involving NaNs always return False (except for !=, which always returns True).

I think either of these is better than the current behavior.  (2) seems like the better option, for a couple 
of reasons:  first, it's the way that Python floats currently work, and second, there might be issues with 
list membership testing if equality comparisons involving NaNs raised an exception or returned a NaN.

Since Mike Cowlishaw is intimately involved with both the Decimal specification and the IEEE-754r process, I 
thought it might be useful to have his opinion on this;  his main recommendation was to have the Decimal 
type do the same as the float type.

The attached patch makes <, <=, >, >= and == comparisons involving NaNs always return False, and != 
comparisons always return True.  It also renames __cmp__ to _cmp and adds a few tests for the new behavior.

Here's how NaN comparisons currently work:

Python 3.0a2+ (py3k:60470M, Jan 30 2008, 23:11:40) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from decimal import *
>>> n = Decimal("nan")
>>> i = Decimal("inf")
>>> n < n
False
>>> n > n
True
>>> i < n
False
>>> i > n
True

See also issue #1514428.
History
Date User Action Args
2008-01-31 04:46:22mark.dickinsonsetspambayes_score: 0.000937109 -> 0.0009371092
recipients: + mark.dickinson, facundobatista
2008-01-31 04:46:22mark.dickinsonsetspambayes_score: 0.000937109 -> 0.000937109
messageid: <1201754782.28.0.976193131321.issue1979@psf.upfronthosting.co.za>
2008-01-31 04:46:15mark.dickinsonlinkissue1979 messages
2008-01-31 04:46:15mark.dickinsoncreate