Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 708d581

Browse files
Issue #4084: Fix max, min, max_mag and min_mag Decimal methods to
give correct results in the case where one argument is a quiet NaN and the other is a finite number that requires rounding. Thanks Mark Dickinson.
1 parent 10b24e8 commit 708d581

3 files changed

Lines changed: 37 additions & 16 deletions

File tree

Lib/decimal.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2651,10 +2651,10 @@ def max(self, other, context=None):
26512651
sn = self._isnan()
26522652
on = other._isnan()
26532653
if sn or on:
2654-
if on == 1 and sn != 2:
2655-
return self._fix_nan(context)
2656-
if sn == 1 and on != 2:
2657-
return other._fix_nan(context)
2654+
if on == 1 and sn == 0:
2655+
return self._fix(context)
2656+
if sn == 1 and on == 0:
2657+
return other._fix(context)
26582658
return self._check_nans(other, context)
26592659

26602660
c = self._cmp(other)
@@ -2693,10 +2693,10 @@ def min(self, other, context=None):
26932693
sn = self._isnan()
26942694
on = other._isnan()
26952695
if sn or on:
2696-
if on == 1 and sn != 2:
2697-
return self._fix_nan(context)
2698-
if sn == 1 and on != 2:
2699-
return other._fix_nan(context)
2696+
if on == 1 and sn == 0:
2697+
return self._fix(context)
2698+
if sn == 1 and on == 0:
2699+
return other._fix(context)
27002700
return self._check_nans(other, context)
27012701

27022702
c = self._cmp(other)
@@ -3251,10 +3251,10 @@ def max_mag(self, other, context=None):
32513251
sn = self._isnan()
32523252
on = other._isnan()
32533253
if sn or on:
3254-
if on == 1 and sn != 2:
3255-
return self._fix_nan(context)
3256-
if sn == 1 and on != 2:
3257-
return other._fix_nan(context)
3254+
if on == 1 and sn == 0:
3255+
return self._fix(context)
3256+
if sn == 1 and on == 0:
3257+
return other._fix(context)
32583258
return self._check_nans(other, context)
32593259

32603260
c = self.copy_abs()._cmp(other.copy_abs())
@@ -3281,10 +3281,10 @@ def min_mag(self, other, context=None):
32813281
sn = self._isnan()
32823282
on = other._isnan()
32833283
if sn or on:
3284-
if on == 1 and sn != 2:
3285-
return self._fix_nan(context)
3286-
if sn == 1 and on != 2:
3287-
return other._fix_nan(context)
3284+
if on == 1 and sn == 0:
3285+
return self._fix(context)
3286+
if sn == 1 and on == 0:
3287+
return other._fix(context)
32883288
return self._check_nans(other, context)
32893289

32903290
c = self.copy_abs()._cmp(other.copy_abs())

Lib/test/decimaltestdata/extra.decTest

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,23 @@ extr1301 fma Inf 0 sNaN456 -> NaN Invalid_operation
154154
extr1302 fma 0E123 -Inf sNaN789 -> NaN Invalid_operation
155155
extr1302 fma -Inf 0E-456 sNaN148 -> NaN Invalid_operation
156156

157+
-- max/min/max_mag/min_mag bug in 2.5.2/2.6/3.0: max(NaN, finite) gave
158+
-- incorrect answers when the finite number required rounding; similarly
159+
-- for the other thre functions
160+
maxexponent: 999
161+
minexponent: -999
162+
precision: 6
163+
rounding: half_even
164+
extr1400 max NaN 1234567 -> 1.23457E+6 Inexact Rounded
165+
extr1401 max 3141590E-123 NaN1729 -> 3.14159E-117 Rounded
166+
extr1402 max -7.654321 -NaN -> -7.65432 Inexact Rounded
167+
extr1410 min -NaN -765432.1 -> -765432 Inexact Rounded
168+
extr1411 min 3141592 NaN -> 3.14159E+6 Inexact Rounded
169+
extr1420 max_mag 0.1111111 -NaN123 -> 0.111111 Inexact Rounded
170+
extr1421 max_mag NaN999999999 0.001234567 -> 0.00123457 Inexact Rounded
171+
extr1430 min_mag 9181716151 -NaN -> 9.18172E+9 Inexact Rounded
172+
extr1431 min_mag NaN4 1.818180E100 -> 1.81818E+100 Rounded
173+
157174
-- Tests for the is_* boolean operations
158175
precision: 9
159176
maxExponent: 999

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ Core and Builtins
3838
Library
3939
-------
4040

41+
- Issue #4084: Fix max, min, max_mag and min_mag Decimal methods to
42+
give correct results in the case where one argument is a quiet NaN
43+
and the other is a finite number that requires rounding.
44+
4145
- Issue #4483: _dbm module now builds on systems with gdbm & gdbm_compat
4246
libs.
4347

0 commit comments

Comments
 (0)