[New-bugs-announce] [issue1979] Make Decimal comparisons with NaN less arbitrary
Mark Dickinson
report at bugs.python.org
Thu Jan 31 05:46:22 CET 2008
New submission from Mark Dickinson:
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.
----------
assignee: facundobatista
components: Library (Lib)
files: richcmp.patch
keywords: patch
messages: 61884
nosy: facundobatista, marketdickinson
severity: minor
status: open
title: Make Decimal comparisons with NaN less arbitrary
type: behavior
versions: Python 3.0
Added file: http://bugs.python.org/file9337/richcmp.patch
__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue1979>
__________________________________
More information about the New-bugs-announce
mailing list