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

Skip to content

Commit 37a79fb

Browse files
committed
Issue 11131: Fix sign of zero result on decimal.Decimal plus and minus operations in ROUND_FLOOR rounding mode.
1 parent abd4a05 commit 37a79fb

3 files changed

Lines changed: 84 additions & 8 deletions

File tree

Lib/decimal.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,14 +1040,16 @@ def __neg__(self, context=None):
10401040
if ans:
10411041
return ans
10421042

1043-
if not self:
1044-
# -Decimal('0') is Decimal('0'), not Decimal('-0')
1043+
if context is None:
1044+
context = getcontext()
1045+
1046+
if not self and context.rounding != ROUND_FLOOR:
1047+
# -Decimal('0') is Decimal('0'), not Decimal('-0'), except
1048+
# in ROUND_FLOOR rounding mode.
10451049
ans = self.copy_abs()
10461050
else:
10471051
ans = self.copy_negate()
10481052

1049-
if context is None:
1050-
context = getcontext()
10511053
return ans._fix(context)
10521054

10531055
def __pos__(self, context=None):
@@ -1060,14 +1062,15 @@ def __pos__(self, context=None):
10601062
if ans:
10611063
return ans
10621064

1063-
if not self:
1064-
# + (-0) = 0
1065+
if context is None:
1066+
context = getcontext()
1067+
1068+
if not self and context.rounding != ROUND_FLOOR:
1069+
# + (-0) = 0, except in ROUND_FLOOR rounding mode.
10651070
ans = self.copy_abs()
10661071
else:
10671072
ans = Decimal(self)
10681073

1069-
if context is None:
1070-
context = getcontext()
10711074
return ans._fix(context)
10721075

10731076
def __abs__(self, round=True, context=None):

Lib/test/decimaltestdata/extra.decTest

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2745,3 +2745,73 @@ pwmx437 power 17 1728 1729 -> 1
27452745
pwmx438 power 18 1728 1729 -> 1
27462746
pwmx439 power 19 1728 1729 -> 456
27472747
pwmx440 power 20 1728 1729 -> 1
2748+
2749+
-- plus and minus zero in various rounding modes (see issue 11131)
2750+
extended: 1
2751+
precision: 9
2752+
maxexponent: 384
2753+
minexponent: -383
2754+
2755+
rounding: half_even
2756+
plux1000 plus 0.0 -> 0.0
2757+
plux1001 plus -0.0 -> 0.0
2758+
minx1000 minus 0.0 -> 0.0
2759+
minx1001 minus -0.0 -> 0.0
2760+
absx1000 abs 0.0 -> 0.0
2761+
absx1001 abs -0.0 -> 0.0
2762+
2763+
rounding: half_up
2764+
plux1010 plus 0.0 -> 0.0
2765+
minx1010 minus 0.0 -> 0.0
2766+
plux1011 plus -0.0 -> 0.0
2767+
minx1011 minus -0.0 -> 0.0
2768+
absx1010 abs 0.0 -> 0.0
2769+
absx1011 abs -0.0 -> 0.0
2770+
2771+
rounding: ceiling
2772+
plux1020 plus 0.0 -> 0.0
2773+
minx1020 minus 0.0 -> 0.0
2774+
plux1021 plus -0.0 -> 0.0
2775+
minx1021 minus -0.0 -> 0.0
2776+
absx1020 abs 0.0 -> 0.0
2777+
absx1021 abs -0.0 -> 0.0
2778+
2779+
rounding: floor
2780+
plux1030 plus 0.0 -> 0.0
2781+
minx1030 minus 0.0 -> -0.0
2782+
plux1031 plus -0.0 -> -0.0
2783+
minx1031 minus -0.0 -> 0.0
2784+
absx1030 abs 0.0 -> 0.0
2785+
absx1031 abs -0.0 -> 0.0
2786+
2787+
rounding: down
2788+
plux1040 plus 0.0 -> 0.0
2789+
minx1040 minus 0.0 -> 0.0
2790+
plux1041 plus -0.0 -> 0.0
2791+
minx1041 minus -0.0 -> 0.0
2792+
absx1040 abs 0.0 -> 0.0
2793+
absx1041 abs -0.0 -> 0.0
2794+
2795+
rounding: up
2796+
plux1050 plus 0.0 -> 0.0
2797+
minx1050 minus 0.0 -> 0.0
2798+
plux1051 plus -0.0 -> 0.0
2799+
minx1051 minus -0.0 -> 0.0
2800+
absx1050 abs 0.0 -> 0.0
2801+
absx1051 abs -0.0 -> 0.0
2802+
2803+
rounding: half_down
2804+
plux1060 plus 0.0 -> 0.0
2805+
minx1060 minus 0.0 -> 0.0
2806+
plux1061 plus -0.0 -> 0.0
2807+
minx1061 minus -0.0 -> 0.0
2808+
absx1060 abs 0.0 -> 0.0
2809+
absx1061 abs -0.0 -> 0.0
2810+
2811+
rounding: 05up
2812+
plux1070 plus 0.0 -> 0.0
2813+
minx1070 minus 0.0 -> 0.0
2814+
plux1071 plus -0.0 -> 0.0
2815+
minx1071 minus -0.0 -> 0.0
2816+
absx1070 abs 0.0 -> 0.0
2817+
absx1071 abs -0.0 -> 0.0

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ Core and Builtins
4040
Library
4141
-------
4242

43+
- Issue #11131: Fix sign of zero in decimal.Decimal plus and minus
44+
operations when the rounding mode is ROUND_FLOOR.
45+
4346
- Issue #5622: Fix curses.wrapper to raise correct exception if curses
4447
initialization fails.
4548

0 commit comments

Comments
 (0)