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

Skip to content

Commit 1ea7c75

Browse files
committed
Reflect recent patch for float % and divmod() by Tim Peters. Content
updates by Tim Peters, markup by FLD.
1 parent 9263e78 commit 1ea7c75

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

Doc/lib/libfuncs.tex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff 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}}}

Doc/ref/ref5.tex

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -583,9 +583,16 @@ \section{Binary arithmetic operations\label{binary}}
583583
following identity: \code{x == (x/y)*y + (x\%y)}. Integer division and
584584
modulo 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

590597
The \code{+} (addition) operator yields the sum of its arguments.
591598
The arguments must either both be numbers or both sequences of the

0 commit comments

Comments
 (0)