[issue36027] Consider adding modular multiplicative inverse to the math module

Mark Dickinson report at bugs.python.org
Tue Feb 19 05:41:11 EST 2019


Mark Dickinson <dickinsm at gmail.com> added the comment:

Here's an example of some code in the standard library that would have benefited from the availability of `pow(x, n, m)` for arbitrary negative n: https://github.com/python/cpython/blob/e7a4bb554edb72fc6619d23241d59162d06f249a/Lib/_pydecimal.py#L957-L960

    if self._exp >= 0:
        exp_hash = pow(10, self._exp, _PyHASH_MODULUS)
    else:
        exp_hash = pow(_PyHASH_10INV, -self._exp, _PyHASH_MODULUS)

where:

    _PyHASH_10INV = pow(10, _PyHASH_MODULUS - 2, _PyHASH_MODULUS)

With the proposed addition, that just becomes `pow(10, self._exp, _PyHASH_MODULUS)`, and the `_PyHASH_10INV` constant isn't needed any more.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36027>
_______________________________________


More information about the Python-bugs-list mailing list