diff options
author | Mark Dickinson <mdickinson@enthought.com> | 2019-06-02 09:24:06 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-02 09:24:06 (GMT) |
commit | c52996785a45d4693857ea219e040777a14584f8 (patch) | |
tree | ee1d54ea597b45ef0d57407f8affaffe57f93be6 /Doc | |
parent | 5ae299ac78abb628803ab7dee0997364547f5cc8 (diff) | |
download | cpython-c52996785a45d4693857ea219e040777a14584f8.zip cpython-c52996785a45d4693857ea219e040777a14584f8.tar.gz cpython-c52996785a45d4693857ea219e040777a14584f8.tar.bz2 |
bpo-36027: Extend three-argument pow to negative second argument (GH-13266)
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/functions.rst | 21 | ||||
-rw-r--r-- | Doc/whatsnew/3.8.rst | 6 |
2 files changed, 24 insertions, 3 deletions
diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst index d5c9f18..415a65b 100644 --- a/Doc/library/functions.rst +++ b/Doc/library/functions.rst @@ -1277,9 +1277,24 @@ are always available. They are listed here in alphabetical order. operands, the result has the same type as the operands (after coercion) unless the second argument is negative; in that case, all arguments are converted to float and a float result is delivered. For example, ``10**2`` - returns ``100``, but ``10**-2`` returns ``0.01``. If the second argument is - negative, the third argument must be omitted. If *z* is present, *x* and *y* - must be of integer types, and *y* must be non-negative. + returns ``100``, but ``10**-2`` returns ``0.01``. + + For :class:`int` operands *x* and *y*, if *z* is present, *z* must also be + of integer type and *z* must be nonzero. If *z* is present and *y* is + negative, *x* must be relatively prime to *z*. In that case, ``pow(inv_x, + -y, z)`` is returned, where *inv_x* is an inverse to *x* modulo *z*. + + Here's an example of computing an inverse for ``38`` modulo ``97``:: + + >>> pow(38, -1, 97) + 23 + >>> 23 * 38 % 97 == 1 + True + + .. versionchanged:: 3.8 + For :class:`int` operands, the three-argument form of ``pow`` now allows + the second argument to be negative, permitting computation of modular + inverses. .. function:: print(*objects, sep=' ', end='\\n', file=sys.stdout, flush=False) diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index 591b454..74d0079 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -304,6 +304,12 @@ Other Language Changes * Added new ``replace()`` method to the code type (:class:`types.CodeType`). (Contributed by Victor Stinner in :issue:`37032`.) +* For integers, the three-argument form of the :func:`pow` function now permits + the exponent to be negative in the case where the base is relatively prime to + the modulus. It then computes a modular inverse to the base when the exponent + is ``-1``, and a suitable power of that inverse for other negative exponents. + (Contributed by Mark Dickinson in :issue:`36027`.) + New Modules =========== |