Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Bugs

[issue7279] decimal.py: == and != comparisons involving NaNs

 

 

Python bugs RSS feed   Index | Next | Previous | View Threaded


report at bugs

Nov 7, 2009, 2:43 AM

Post #1 of 3 (58 views)
Permalink
[issue7279] decimal.py: == and != comparisons involving NaNs

New submission from Stefan Krah <stefan-usenet[at]bytereef.org>:

I'm not sure this is a bug, but I am trying to understand the rationale
for mimicking IEEE 754 for == and != comparisons involving NaNs. The
comment in decimal.py says:

"Note: The Decimal standard doesn't cover rich comparisons for Decimals.
In particular, the specification is silent on the subject of what
should happen for a comparison involving a NaN."


First, I think rich comparisons are covered with compare_total(), but
indeed that isn't very useful for == and !=. (It might be useful for
sorting a list of decimals.)

The standard compare() function returns NaN for comparisons involving
NaNs. In addition to that it signals for sNaNs. I'm interpreting this as
"the comparison is undefined". So, in terms of decimal return values,
the standard does define NaN comparisons.

The question remains how to translate "undefined" to a Python truth
value. I'd think that the natural thing is to raise an InvalidOperation
exception in the same way it is done for <, <=, >, >=.


This ...

Decimal("NaN") == 9 ==> InvalidOperation
Decimal("sNaN") == 9 ==> InvalidOperation

... is the behavior of compare_signal(). In my opinion this would follow
the principle of least surprise for the user.

----------
messages: 95017
nosy: mark.dickinson, skrah
severity: normal
status: open
title: decimal.py: == and != comparisons involving NaNs

_______________________________________
Python tracker <report[at]bugs.python.org>
<http://bugs.python.org/issue7279>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 7, 2009, 12:42 PM

Post #2 of 3 (51 views)
Permalink
[issue7279] decimal.py: == and != comparisons involving NaNs [In reply to]

Changes by Stefan Krah <stefan-usenet[at]bytereef.org>:


----------
type: -> behavior

_______________________________________
Python tracker <report[at]bugs.python.org>
<http://bugs.python.org/issue7279>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 8, 2009, 1:28 AM

Post #3 of 3 (48 views)
Permalink
[issue7279] decimal.py: == and != comparisons involving NaNs [In reply to]

Mark Dickinson <dickinsm[at]gmail.com> added the comment:

There's a second issue to consider here, which is that Python uses
equality as specified by the == operator as the basic equivalence relation
for set and dict membership tests. So as a general rule, an equality test
between two objects of the same type shouldn't be raising exceptions. If
== raised for comparisons with nans then it would make it awkward to put
nans into a set.

Hmm. But now I notice that you can't put Decimal nans into sets anyway:
you get a 'TypeError: Cannot hash a NaN value'. I'm not sure of the
rationale for this.

One might also question whether Decimal("NaN") < 9 should really be
raising InvalidOperation, or whether (as an operation that doesn't return
a Decimal instance and is in some sense outside the scope of the standard-
--similar to int(Decimal('nan')) and hash(Decimal('nan'))) it should be
raising some general Python exception instead.

I'm closing this as invalid: the behaviour isn't a bug, at least in the
sense that the code is working as designed. I think there may well be a
useful discussion here, but the bugtracker isn't the right place to have
it: could we move it to python-dev instead?

----------
resolution: -> invalid
status: open -> closed

_______________________________________
Python tracker <report[at]bugs.python.org>
<http://bugs.python.org/issue7279>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com

Python bugs RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.