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 module performs wrong floor division with negative numbers
Type: Stage: resolved
Components: Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: mark.dickinson, multiks2200
Priority: normal Keywords:

Created on 2021-01-15 15:33 by multiks2200, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg385113 - (view) Author: Jens (multiks2200) Date: 2021-01-15 15:33
from decimal import Decimal

print(-0.9//0.123)
# prints -8.0
print(Decimal('-0.9')//Decimal('0.123'))
# prints -7
print(-10//4.2)
# prints -3.0
print(Decimal('-10')//Decimal('4.2'))
# prints -2
msg385114 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2021-01-15 15:38
The behaviour is deliberate, if unfortunate: it's covered in the documentation here: https://docs.python.org/3/library/decimal.html#decimal-objects - see the paragraph starting

> There are some small differences between arithmetic on Decimal objects
> and arithmetic on integers and floats. When the remainder operator % is
> applied to Decimal objects

The issue is that the decimal spec specifies "divide-integer" and "remainder" operations. We've chosen to map those operations to "%" and "//" for convenience, even though there's a difference between float and Decimal here.
msg385117 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2021-01-15 16:26
Thanks for the report. I'm going to close here, since this isn't a bug.

If you want to advocate for a behaviour change, by all means go ahead, but be aware that it would likely be a hard sell. The main challenge would be finding a way to change the behaviour that doesn't abruptly break existing code that depends on the current semantics.
History
Date User Action Args
2022-04-11 14:59:40adminsetgithub: 87102
2021-01-15 16:26:05mark.dickinsonsetstatus: open -> closed
resolution: not a bug
messages: + msg385117

stage: resolved
2021-01-15 15:38:05mark.dickinsonsetnosy: + mark.dickinson
messages: + msg385114
2021-01-15 15:33:47multiks2200create