|
| 1 | +PEP: 9999 |
| 2 | +Title: imath --- module for number-theoretic functions |
| 3 | +Author: Sergey B Kirpichev |
| 4 | +Sponsor: TBD |
| 5 | +Discussions-To: Pending |
| 6 | +Status: Draft |
| 7 | +Type: Standards Track |
| 8 | +Created: 10-May-2025 |
| 9 | +Python-Version: 3.15 |
| 10 | +Post-History: `02-Jun-2019 <https://github.com/python/cpython/issues/81313>`__, |
| 11 | + `09-May-2025 <https://discuss.python.org/t/91337>`__ |
| 12 | + |
| 13 | + |
| 14 | +Abstract |
| 15 | +======== |
| 16 | + |
| 17 | +This PEP describes new module for number-theoretic functions over integers, |
| 18 | +like :external+py3.14:func:`math.gcd` or :external+py3.14:func:`math.isqrt`. |
| 19 | + |
| 20 | + |
| 21 | +Motivation |
| 22 | +========== |
| 23 | + |
| 24 | +The :external+py3.14:mod:`math` documentaton says: "This module provides |
| 25 | +access to the mathematical functions defined by the C standard." But as a |
| 26 | +state of art, over time the module was populated with functions, that aren't |
| 27 | +related to the C standard or floating-point arithmetics. Now it's much more |
| 28 | +harder to describe module scope, content and interfaces (e.g. returned |
| 29 | +values). |
| 30 | + |
| 31 | +Apparently, the :external+py3.14:mod:`math` can't serve as a catch-all place |
| 32 | +for mathematical functions: we have also the :external+py3.14:mod:`cmath` and |
| 33 | +the :external+py3.14:mod:`statistics`. Lets make same for integer-related |
| 34 | +functions. |
| 35 | + |
| 36 | + |
| 37 | +Specification |
| 38 | +============= |
| 39 | + |
| 40 | +The PEP propose moving functions for integers (now placed in subsection |
| 41 | +"Number-theoretic functions" [1]_) to a separate module, called `imath`. As |
| 42 | +maintaining backward-compatible aliases in the :external+py3.14:mod:`math` |
| 43 | +module is not a problem --- they only soft-deprecated. |
| 44 | + |
| 45 | +Possible extensions for the new module and it's scope are discussed in the |
| 46 | +Open Issues section. |
| 47 | + |
| 48 | + |
| 49 | +Backwards Compatibility |
| 50 | +======================= |
| 51 | + |
| 52 | +No code breaks expected. |
| 53 | + |
| 54 | + |
| 55 | +Reference Implementation |
| 56 | +======================== |
| 57 | + |
| 58 | +TBD |
| 59 | + |
| 60 | + |
| 61 | +Open Issues |
| 62 | +=========== |
| 63 | + |
| 64 | +Module name |
| 65 | +----------- |
| 66 | + |
| 67 | +Another variants for name: `ntheory` (like SymPy's submodule), `intmath`. |
| 68 | + |
| 69 | +Possible renaming of isqrt |
| 70 | +-------------------------- |
| 71 | + |
| 72 | +As there is no clash with floating-point version, it could be renamed to `sqrt` |
| 73 | +(Java-like). |
| 74 | + |
| 75 | +Module scope and possible extensions |
| 76 | +------------------------------------ |
| 77 | + |
| 78 | +Unless we can just provide bindings to some well supported mathematical |
| 79 | +library like the GMP, the module scope should be limited. For example, no |
| 80 | +primality testing and factorization. |
| 81 | + |
| 82 | +Among proposed in the initial discussion thread: |
| 83 | + |
| 84 | +* ``c_div()`` --- for integer ceiling divide, see [2]_, [3]_. |
| 85 | +* ``gcdext()`` --- to solve linear Diophantine equation in two variables (the |
| 86 | +:external+py3.14:class:`int` implementation actually include extended |
| 87 | +Euclidean algorithm) |
| 88 | +* ``isqrt_rem()`` --- to return both integer square root and a remainder (if |
| 89 | + integer isn't a perfect square) |
| 90 | +* ``ilog()`` --- integer logarihm, currently :external+py3.14:func:`math.log` |
| 91 | + has a special handling for integer arguments. It's unique (wrt other module |
| 92 | + functions) and not documented so far, see [4]_ |
| 93 | +* ``fibonacci()``. |
| 94 | + |
| 95 | + |
| 96 | +Footnotes |
| 97 | +========= |
| 98 | + |
| 99 | +.. [1] Number-theoretic functions |
| 100 | + (https://docs.python.org/3.14/library/math.html#number-theoretic-functions) |
| 101 | +.. [2] Integer ceiling divide |
| 102 | + (https://discuss.python.org/t/91269) |
| 103 | +.. [3] https://gmpy2.readthedocs.io/en/stable/mpz.html#gmpy2.c_div |
| 104 | +.. [4] https://github.com/python/cpython/issues/120950 |
| 105 | +
|
| 106 | +
|
| 107 | +Copyright |
| 108 | +========= |
| 109 | + |
| 110 | +This document is placed in the public domain or under the |
| 111 | +CC0-1.0-Universal license, whichever is more permissive. |
0 commit comments