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

Skip to content

Commit 5b21df4

Browse files
committed
Repaired inaccuracies in the % docs. In particular, we don't (and can't)
guarantee abs(x%y) < abs(y) in all cases when a float is involved. math.fmod() should, though, so noted that too. Bugfix candidate. Someone should check the LaTeX here first, though.
1 parent 1babdfc commit 5b21df4

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

Doc/ref/ref5.tex

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -694,8 +694,19 @@ \section{Binary arithmetic operations\label{binary}}
694694
point numbers, e.g., \code{3.14\%0.7} equals \code{0.34} (since
695695
\code{3.14} equals \code{4*0.7 + 0.34}.) The modulo operator always
696696
yields a result with the same sign as its second operand (or zero);
697-
the absolute value of the result is strictly smaller than the second
698-
operand.
697+
the absolute value of the result is strictly smaller than the absolute
698+
value of the second operand\footnote{
699+
While \code{abs(x\%y) < abs(y)) is true mathematically, for
700+
floats it may not be true numerically due to roundoff. For
701+
example, and assuming a platform on which a Python float is an
702+
IEEE 754 double-precision number, in order that \code{-1e-100 \% 1e100}
703+
have the same sign as \code{1e100}, the computed result is
704+
\code{-1e-100 + 1e100}, which is numerically exactly equal
705+
to \code{1e100}. Function \function{fmod()} in the \module{math}
706+
module returns a result whose sign matches the sign of the
707+
first argument instead, and so returns \code{-1e-100} in this case.
708+
Which approach is more appropriate depends on the application.
709+
}.
699710
\index{modulo}
700711
701712
The integer division and modulo operators are connected by the
@@ -704,7 +715,7 @@ \section{Binary arithmetic operations\label{binary}}
704715
\code{divmod(x, y) == (x/y, x\%y)}. These identities don't hold for
705716
floating point numbers; there similar identities hold
706717
approximately where \code{x/y} is replaced by \code{floor(x/y)}) or
707-
\code{floor(x/y) - 1} (for floats),\footnote{
718+
\code{floor(x/y) - 1}\footnote{
708719
If x is very close to an exact integer multiple of y, it's
709720
possible for \code{floor(x/y)} to be one larger than
710721
\code{(x-x\%y)/y} due to rounding. In such cases, Python returns

0 commit comments

Comments
 (0)