# HG changeset patch # Parent 682494b54f8a3e1e235d2def9f84476e20aaf72b diff --git a/Doc/library/decimal.rst b/Doc/library/decimal.rst --- a/Doc/library/decimal.rst +++ b/Doc/library/decimal.rst @@ -1922,3 +1922,29 @@ >>> Context(prec=5, rounding=ROUND_DOWN).create_decimal('1.2345678') Decimal('1.2345') +Q. I noticed that Decimals and floats behave differently when using the modulus +operator on negative numbers. Is this intentional? + +.. doctest:: newcontext + + >>> -3.0 % 5.0 + 2.0 + >>> Decimal('-3') % Decimal('5') + Decimal('-3') + +A. Yes. Decimal's modulus operator implements the remainder operation that is +required by the decimal arithmetic specification. Consequently, it's behaviour +differs from that floats or ints. :func:`divmod` and ``//`` are similarly +affected: + +.. doctest:: newcontext + + >>> divmod(-3, 5) + (-1, 2) + >>> divmod(Decimal('-3'), Decimal('5')) + (Decimal('-0'), Decimal('-3')) + >>> -3 // 5 + -1 + >>> Decimal('-3') // Decimal('5') + Decimal('-0') +