From 7a7739d75ed505033445248a126830bedb17101b Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Fri, 28 Aug 2009 13:25:02 +0000 Subject: [PATCH] Issue #6794: Fix handling of NaNs in Decimal.compare_total and Decimal.compare_total_mag. --- Lib/decimal.py | 7 +++++-- Lib/test/decimaltestdata/extra.decTest | 16 ++++++++++++++++ Misc/NEWS | 3 +++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Lib/decimal.py b/Lib/decimal.py index f36e8460a42..e7864534dca 100644 --- a/Lib/decimal.py +++ b/Lib/decimal.py @@ -2736,12 +2736,15 @@ class Decimal(object): other_nan = other._isnan() if self_nan or other_nan: if self_nan == other_nan: - if self._int < other._int: + # compare payloads as though they're integers + self_key = len(self._int), self._int + other_key = len(other._int), other._int + if self_key < other_key: if sign: return _One else: return _NegativeOne - if self._int > other._int: + if self_key > other_key: if sign: return _NegativeOne else: diff --git a/Lib/test/decimaltestdata/extra.decTest b/Lib/test/decimaltestdata/extra.decTest index 2e3fce85585..1d6ea3702ad 100644 --- a/Lib/test/decimaltestdata/extra.decTest +++ b/Lib/test/decimaltestdata/extra.decTest @@ -154,6 +154,22 @@ extr1301 fma Inf 0 sNaN456 -> NaN Invalid_operation extr1302 fma 0E123 -Inf sNaN789 -> NaN Invalid_operation extr1302 fma -Inf 0E-456 sNaN148 -> NaN Invalid_operation +-- Issue #6794: when comparing NaNs using compare_total, payloads +-- should be compared as though positive integers; not +-- lexicographically as strings. +extr1400 comparetotal NaN123 NaN45 -> 1 +extr1401 comparetotal sNaN123 sNaN45 -> 1 +extr1402 comparetotal -NaN123 -NaN45 -> -1 +extr1403 comparetotal -sNaN123 -sNaN45 -> -1 +extr1404 comparetotal NaN45 NaN123 -> -1 +extr1405 comparetotal sNaN45 sNaN123 -> -1 +extr1406 comparetotal -NaN45 -NaN123 -> 1 +extr1407 comparetotal -sNaN45 -sNaN123 -> 1 + +extr1410 comparetotal -sNaN63450748854172416 -sNaN911993 -> -1 +extr1411 comparetotmag NaN1222222222222 -NaN999999 -> 1 + + -- max/min/max_mag/min_mag bug in 2.5.2/2.6/3.0: max(NaN, finite) gave -- incorrect answers when the finite number required rounding; similarly -- for the other thre functions diff --git a/Misc/NEWS b/Misc/NEWS index 16ec78b6127..a63182fdea2 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -359,6 +359,9 @@ Core and Builtins Library ------- +- Issue #6794: Fix Decimal.compare_total and Decimal.compare_total_mag: NaN + payloads are now ordered by integer value rather than lexicographically. + - Issue #6693: New functions in site.py to get user/global site packages paths. - The thread.lock type now supports weak references.