X Tutup
Skip to content

Commit a7f0dff

Browse files
committed
2 parents e858bca + 37a79fb commit a7f0dff

File tree

3 files changed

+84
-8
lines changed

3 files changed

+84
-8
lines changed

Lib/decimal.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,14 +1045,16 @@ def __neg__(self, context=None):
10451045
if ans:
10461046
return ans
10471047

1048-
if not self:
1049-
# -Decimal('0') is Decimal('0'), not Decimal('-0')
1048+
if context is None:
1049+
context = getcontext()
1050+
1051+
if not self and context.rounding != ROUND_FLOOR:
1052+
# -Decimal('0') is Decimal('0'), not Decimal('-0'), except
1053+
# in ROUND_FLOOR rounding mode.
10501054
ans = self.copy_abs()
10511055
else:
10521056
ans = self.copy_negate()
10531057

1054-
if context is None:
1055-
context = getcontext()
10561058
return ans._fix(context)
10571059

10581060
def __pos__(self, context=None):
@@ -1065,14 +1067,15 @@ def __pos__(self, context=None):
10651067
if ans:
10661068
return ans
10671069

1068-
if not self:
1069-
# + (-0) = 0
1070+
if context is None:
1071+
context = getcontext()
1072+
1073+
if not self and context.rounding != ROUND_FLOOR:
1074+
# + (-0) = 0, except in ROUND_FLOOR rounding mode.
10701075
ans = self.copy_abs()
10711076
else:
10721077
ans = Decimal(self)
10731078

1074-
if context is None:
1075-
context = getcontext()
10761079
return ans._fix(context)
10771080

10781081
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
@@ -34,6 +34,9 @@ Core and Builtins
3434
Library
3535
-------
3636

37+
- Issue #11131: Fix sign of zero in decimal.Decimal plus and minus
38+
operations when the rounding mode is ROUND_FLOOR.
39+
3740
- Issue #5622: Fix curses.wrapper to raise correct exception if curses
3841
initialization fails.
3942

0 commit comments

Comments
 (0)
X Tutup