summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorMark Dickinson <mdickinson@enthought.com>2012-11-18 10:20:28 (GMT)
committerMark Dickinson <mdickinson@enthought.com>2012-11-18 10:20:28 (GMT)
commit0d187318156f21b99868d1a901b8b343128b9f64 (patch)
treea7ae83126c21497e153b628d40e99a2d1384997e /Doc
parent02512fb1cf92ef4e905952a697da8f5a0a8695b2 (diff)
downloadcpython-0d187318156f21b99868d1a901b8b343128b9f64.zip
cpython-0d187318156f21b99868d1a901b8b343128b9f64.tar.gz
cpython-0d187318156f21b99868d1a901b8b343128b9f64.tar.bz2
Issue #12005: clarify behaviour of % and // for Decimal objects.
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/decimal.rst23
1 files changed, 23 insertions, 0 deletions
diff --git a/Doc/library/decimal.rst b/Doc/library/decimal.rst
index 697ae87..27e790e 100644
--- a/Doc/library/decimal.rst
+++ b/Doc/library/decimal.rst
@@ -375,6 +375,29 @@ Decimal objects
compared, sorted, and coerced to another type (such as :class:`float` or
:class:`long`).
+ 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 sign of the result is the sign of the
+ *dividend* rather than the sign of the divisor::
+
+ >>> (-7) % 4
+ 1
+ >>> Decimal(-7) % Decimal(4)
+ Decimal('-3')
+
+ The integer division operator ``//`` behaves analogously, returning the
+ integer part of the true quotient (truncating towards zero) rather than its
+ floor, so as to preseve the usual identity ``x == (x // y) * y + x % y``::
+
+ >>> -7 // 4
+ -2
+ >>> Decimal(-7) // Decimal(4)
+ Decimal('-1')
+
+ The ``%`` and ``//`` operators implement the ``remainder`` and
+ ``divide-integer`` operations (respectively) as described in the
+ specification.
+
Decimal objects cannot generally be combined with floats in
arithmetic operations: an attempt to add a :class:`Decimal` to a
:class:`float`, for example, will raise a :exc:`TypeError`.