File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -178,8 +178,12 @@ \section{Built-in Functions \label{built-in-funcs}}
178178 operand types, the rules for binary arithmetic operators apply. For
179179 plain and long integers, the result is the same as
180180 \code {(\var {a} / \var {b}, \var {a} \% {} \var {b})}.
181- For floating point numbers the result is the same as
182- \code {(math.floor(\var {a} / \var {b}), \var {a} \% {} \var {b})}.
181+ For floating point numbers the result is \code {(\var {q}, \var {a} \% {}
182+ \var {b})}, where \var {q} is usually \code {math.floor(\var {a} /
183+ \var {b})} but may be 1 less than that. In any case \code {\var {q} *
184+ \var {b} + \var {a} \% {} \var {b}} is very close to \var {a}, if
185+ \code {\var {a} \% {} \var {b}} is non-zero it has the same sign as
186+ \var {b}, and \code {0 <= abs(\var {a} \% {} \var {b}) < abs(\var {b})}.
183187\end {funcdesc }
184188
185189\begin {funcdesc }{eval}{expression\optional {, globals\optional {, locals}}}
Original file line number Diff line number Diff line change @@ -583,9 +583,16 @@ \section{Binary arithmetic operations\label{binary}}
583583following identity: \code {x == (x/y)*y + (x\% y)}. Integer division and
584584modulo are also connected with the built-in function \function {divmod()}:
585585\code {divmod(x, y) == (x/y, x\% y)}. These identities don't hold for
586- floating point and complex numbers; there a similar identity holds where
587- \code {x/y} is replaced by \code {floor(x/y)}) or
588- \code {floor((x/y).real)}, respectively.
586+ floating point and complex numbers; there similar identities hold
587+ approximately where \code {x/y} is replaced by \code {floor(x/y)}) or
588+ \code {floor(x/y) - 1} (for floats),\footnote {
589+ If x is very close to an exact integer multiple of y, it's
590+ possible for \code {floor(x/y)} to be one larger than
591+ \code {(x-x\% y)/y} due to rounding. In such cases, Python returns
592+ the latter result, in order to preserve that \code {divmod(x,y)[0]
593+ * y + x \% {} y} be very close to \code {x}.
594+ } or \code {floor((x/y).real)} (for
595+ complex).
589596
590597The \code {+} (addition) operator yields the sum of its arguments.
591598The arguments must either both be numbers or both sequences of the
You can’t perform that action at this time.
0 commit comments