Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a3f3740

Browse files
committed
Issue #12005: clarify behaviour of % and // for Decimal objects.
1 parent 17babc5 commit a3f3740

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

Doc/library/decimal.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,29 @@ Decimal objects
365365
compared, sorted, and coerced to another type (such as :class:`float` or
366366
:class:`int`).
367367

368+
There are some small differences between arithmetic on Decimal objects and
369+
arithmetic on integers and floats. When the remainder operator ``%`` is
370+
applied to Decimal objects, the sign of the result is the sign of the
371+
*dividend* rather than the sign of the divisor::
372+
373+
>>> (-7) % 4
374+
1
375+
>>> Decimal(-7) % Decimal(4)
376+
Decimal('-3')
377+
378+
The integer division operator ``//`` behaves analogously, returning the
379+
integer part of the true quotient (truncating towards zero) rather than its
380+
floor, so as to preseve the usual identity ``x == (x // y) * y + x % y``::
381+
382+
>>> -7 // 4
383+
-2
384+
>>> Decimal(-7) // Decimal(4)
385+
Decimal('-1')
386+
387+
The ``%`` and ``//`` operators implement the ``remainder`` and
388+
``divide-integer`` operations (respectively) as described in the
389+
specification.
390+
368391
Decimal objects cannot generally be combined with floats or
369392
instances of :class:`fractions.Fraction` in arithmetic operations:
370393
an attempt to add a :class:`Decimal` to a :class:`float`, for

0 commit comments

Comments
 (0)