summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Dickinson <mdickinson@enthought.com>2011-03-12 11:12:52 (GMT)
committerMark Dickinson <mdickinson@enthought.com>2011-03-12 11:12:52 (GMT)
commit37a79fb75b8f6d1833caee16c777d07b9338938f (patch)
tree5cb83acd730e2ac26c763ecf5d1928af5da172fc
parentabd4a0556142e971c22d23243bacd3f0ad6d2299 (diff)
downloadcpython-37a79fb75b8f6d1833caee16c777d07b9338938f.zip
cpython-37a79fb75b8f6d1833caee16c777d07b9338938f.tar.gz
cpython-37a79fb75b8f6d1833caee16c777d07b9338938f.tar.bz2
Issue 11131: Fix sign of zero result on decimal.Decimal plus and minus operations in ROUND_FLOOR rounding mode.
-rw-r--r--Lib/decimal.py19
-rw-r--r--Lib/test/decimaltestdata/extra.decTest70
-rw-r--r--Misc/NEWS3
3 files changed, 84 insertions, 8 deletions
diff --git a/Lib/decimal.py b/Lib/decimal.py
index c61b549..5e53d8e 100644
--- a/Lib/decimal.py
+++ b/Lib/decimal.py
@@ -1040,14 +1040,16 @@ class Decimal(object):
if ans:
return ans
- if not self:
- # -Decimal('0') is Decimal('0'), not Decimal('-0')
+ if context is None:
+ context = getcontext()
+
+ if not self and context.rounding != ROUND_FLOOR:
+ # -Decimal('0') is Decimal('0'), not Decimal('-0'), except
+ # in ROUND_FLOOR rounding mode.
ans = self.copy_abs()
else:
ans = self.copy_negate()
- if context is None:
- context = getcontext()
return ans._fix(context)
def __pos__(self, context=None):
@@ -1060,14 +1062,15 @@ class Decimal(object):
if ans:
return ans
- if not self:
- # + (-0) = 0
+ if context is None:
+ context = getcontext()
+
+ if not self and context.rounding != ROUND_FLOOR:
+ # + (-0) = 0, except in ROUND_FLOOR rounding mode.
ans = self.copy_abs()
else:
ans = Decimal(self)
- if context is None:
- context = getcontext()
return ans._fix(context)
def __abs__(self, round=True, context=None):
diff --git a/Lib/test/decimaltestdata/extra.decTest b/Lib/test/decimaltestdata/extra.decTest
index fce8435..fe8b77a 100644
--- a/Lib/test/decimaltestdata/extra.decTest
+++ b/Lib/test/decimaltestdata/extra.decTest
@@ -2745,3 +2745,73 @@ pwmx437 power 17 1728 1729 -> 1
pwmx438 power 18 1728 1729 -> 1
pwmx439 power 19 1728 1729 -> 456
pwmx440 power 20 1728 1729 -> 1
+
+-- plus and minus zero in various rounding modes (see issue 11131)
+extended: 1
+precision: 9
+maxexponent: 384
+minexponent: -383
+
+rounding: half_even
+plux1000 plus 0.0 -> 0.0
+plux1001 plus -0.0 -> 0.0
+minx1000 minus 0.0 -> 0.0
+minx1001 minus -0.0 -> 0.0
+absx1000 abs 0.0 -> 0.0
+absx1001 abs -0.0 -> 0.0
+
+rounding: half_up
+plux1010 plus 0.0 -> 0.0
+minx1010 minus 0.0 -> 0.0
+plux1011 plus -0.0 -> 0.0
+minx1011 minus -0.0 -> 0.0
+absx1010 abs 0.0 -> 0.0
+absx1011 abs -0.0 -> 0.0
+
+rounding: ceiling
+plux1020 plus 0.0 -> 0.0
+minx1020 minus 0.0 -> 0.0
+plux1021 plus -0.0 -> 0.0
+minx1021 minus -0.0 -> 0.0
+absx1020 abs 0.0 -> 0.0
+absx1021 abs -0.0 -> 0.0
+
+rounding: floor
+plux1030 plus 0.0 -> 0.0
+minx1030 minus 0.0 -> -0.0
+plux1031 plus -0.0 -> -0.0
+minx1031 minus -0.0 -> 0.0
+absx1030 abs 0.0 -> 0.0
+absx1031 abs -0.0 -> 0.0
+
+rounding: down
+plux1040 plus 0.0 -> 0.0
+minx1040 minus 0.0 -> 0.0
+plux1041 plus -0.0 -> 0.0
+minx1041 minus -0.0 -> 0.0
+absx1040 abs 0.0 -> 0.0
+absx1041 abs -0.0 -> 0.0
+
+rounding: up
+plux1050 plus 0.0 -> 0.0
+minx1050 minus 0.0 -> 0.0
+plux1051 plus -0.0 -> 0.0
+minx1051 minus -0.0 -> 0.0
+absx1050 abs 0.0 -> 0.0
+absx1051 abs -0.0 -> 0.0
+
+rounding: half_down
+plux1060 plus 0.0 -> 0.0
+minx1060 minus 0.0 -> 0.0
+plux1061 plus -0.0 -> 0.0
+minx1061 minus -0.0 -> 0.0
+absx1060 abs 0.0 -> 0.0
+absx1061 abs -0.0 -> 0.0
+
+rounding: 05up
+plux1070 plus 0.0 -> 0.0
+minx1070 minus 0.0 -> 0.0
+plux1071 plus -0.0 -> 0.0
+minx1071 minus -0.0 -> 0.0
+absx1070 abs 0.0 -> 0.0
+absx1071 abs -0.0 -> 0.0
diff --git a/Misc/NEWS b/Misc/NEWS
index b3d099b..df2aa4b 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -40,6 +40,9 @@ Core and Builtins
Library
-------
+- Issue #11131: Fix sign of zero in decimal.Decimal plus and minus
+ operations when the rounding mode is ROUND_FLOOR.
+
- Issue #5622: Fix curses.wrapper to raise correct exception if curses
initialization fails.