This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: decimal.py: plus/minus with ROUND_FLOOR
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.1, Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: mark.dickinson Nosy List: mark.dickinson, python-dev, skrah
Priority: normal Keywords: patch

Created on 2011-02-05 18:04 by skrah, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue11131.patch mark.dickinson, 2011-02-06 17:58 review
issue11131-2.patch skrah, 2011-02-07 14:26 review
Messages (7)
msg128007 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2011-02-05 18:04
Another exciting corner case in plus/minus:

"The operations are evaluated using the same rules as add and subtract;
 the operations plus(a) and minus(a) (where a and b refer to any numbers)
 are calculated as the operations add(’0’, a) and subtract(’0’, b)
 respectively, where the ’0’ has the same exponent as the operand."


But add and subtract have a special rule for the sign with ROUND_FLOOR:

"Otherwise, the sign of a zero result is 0 unless either both operands 
 were negative or the signs of the operands were different and the 
 rounding is round-floor."


So, +Decimal("-0") and -Decimal("0") should be a negative zero. I checked
this against decNumber. Currently:


>>> c = getcontext()
>>> c.rounding = ROUND_FLOOR
>>> +Decimal("-0")
Decimal('0')
>>> -Decimal("0")
Decimal('0')
msg128070 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2011-02-06 17:58
Nice catch!  Please could you test the attached patch (against py3k)?

I'll apply it after 3.2 is out.  The fix should also go into the 2.7 maintenance branch, I think.
msg128116 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2011-02-07 14:26
The patch and the test cases look good, but the implicit context must
be looked up earlier now (see the altered patch).

I just realize that there aren't any regression tests for this sort
of situation, since for the main tests we are only testing the context
methods.
msg128136 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2011-02-07 16:50
Ah, good point about the context;  thanks for the updated patch.

Yes, those missing regression tests are a problem;  this isn't the first time that the above problem has appeared.
msg130668 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2011-03-12 11:05
New changeset d5fe8b33f711 by Mark Dickinson in branch '2.7':
Issue 11131: Fix sign of zero result on plus and minus operations in ROUND_FLOOR rounding mode.
http://hg.python.org/cpython/rev/d5fe8b33f711
msg130669 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2011-03-12 11:19
New changeset 1c4be28cda5b by Mark Dickinson in branch '3.1':
Issue 11131: Fix sign of zero result on decimal.Decimal plus and minus operations in ROUND_FLOOR rounding mode.
http://hg.python.org/cpython/rev/1c4be28cda5b

New changeset 426057570bee by Mark Dickinson in branch '3.2':
Merge #11131
http://hg.python.org/cpython/rev/426057570bee

New changeset af0dcd9df0c0 by Mark Dickinson in branch 'default':
Merge #11131
http://hg.python.org/cpython/rev/af0dcd9df0c0
msg130670 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2011-03-12 11:26
Fixed in 2.7, 3.1, 3.2, 3.3.  Closing.
History
Date User Action Args
2022-04-11 14:57:12adminsetgithub: 55340
2011-03-12 11:26:57mark.dickinsonsetstatus: open -> closed

messages: + msg130670
resolution: fixed
versions: + Python 3.1
2011-03-12 11:19:27python-devsetmessages: + msg130669
2011-03-12 11:05:53python-devsetnosy: + python-dev
messages: + msg130668
2011-02-07 16:50:06mark.dickinsonsetmessages: + msg128136
2011-02-07 14:26:47skrahsetfiles: + issue11131-2.patch

messages: + msg128116
2011-02-06 17:58:45mark.dickinsonsetfiles: + issue11131.patch
versions: + Python 2.7
messages: + msg128070

assignee: mark.dickinson
keywords: + patch
2011-02-05 18:04:49skrahcreate