Skip to content

Commit 0465dd1

Browse files
skirpichevhugovk
andauthored
PEP 791: address PEP review comments (#4430)
Co-authored-by: Hugo van Kemenade <[email protected]>
1 parent a9d6759 commit 0465dd1

File tree

1 file changed

+74
-53
lines changed

1 file changed

+74
-53
lines changed

peps/pep-0791.rst

Lines changed: 74 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PEP: 791
2-
Title: imath --- module for integer-specific mathematics functions
2+
Title: intmath --- module for integer-specific mathematics functions
33
Author: Sergey B Kirpichev <[email protected]>
44
Sponsor: Victor Stinner <[email protected]>
55
Discussions-To: https://discuss.python.org/t/92548
@@ -8,7 +8,6 @@ Type: Standards Track
88
Created: 12-May-2025
99
Python-Version: 3.15
1010
Post-History: `12-Jul-2018 <https://mail.python.org/archives/list/[email protected]/thread/YYJ5YJBJNCVXQWK5K3WSVNMPUSV56LOR/>`__,
11-
02-Jun-2019,
1211
`09-May-2025 <https://discuss.python.org/t/91337>`__,
1312
`19-May-2025 <https://discuss.python.org/t/92548>`__,
1413

@@ -35,10 +34,10 @@ when explicitly noted otherwise, all return values are floats." This is no
3534
longer true: *None* of the functions listed in the `Number-theoretic
3635
functions <https://docs.python.org/3.14/library/math.html#number-theoretic-functions>`_
3736
subsection of the documentation return a float, but the
38-
documentation doesn't say so. In the documentation for the proposed ``imath`` module the sentence "All
37+
documentation doesn't say so. In the documentation for the proposed ``intmath`` module the sentence "All
3938
return values are integers." would be accurate. In a similar way we
4039
can simplify the description of the accepted arguments for functions in both the
41-
:external+py3.14:mod:`math` and the new module.
40+
new module and in :external+py3.14:mod:`math`.
4241

4342
Apparently, the :external+py3.14:mod:`math` module can't serve as a catch-all place
4443
for mathematical functions since we also have the :external+py3.14:mod:`cmath` and
@@ -53,12 +52,55 @@ comparable with the :external+py3.14:mod:`cmath` (1340LOC), which is *not* a
5352
simple wrapper to the ``libm``, as most functions in the
5453
:external+py3.14:mod:`math` module.
5554

55+
And this situation tends to get worse. When the module split `was first
56+
proposed
57+
<https://mail.python.org/archives/list/[email protected]/thread/YYJ5YJBJNCVXQWK5K3WSVNMPUSV56LOR/>`_,
58+
there were only two integer-related functions:
59+
:external+py3.14:func:`~math.factorial` and :external+py3.14:func:`~math.gcd`.
60+
Now there are six and :external+py3.14:func:`~math.factorial` doesn't accept
61+
:class:`float`'s anymore.
62+
63+
Some possible additions, among those proposed in the initial discussion thread
64+
and issue
65+
`python/cpython#81313 <https://github.com/python/cpython/issues/81313>`_ are:
66+
67+
* ``ceil_div()`` --- for integer ceiling divide, see
68+
`relevant discussion thread <https://discuss.python.org/t/91269>`_.
69+
* ``gcdext()`` --- to solve linear `Diophantine equation <https://en.wikipedia.org/wiki/Diophantine_equation>`_ in two variables (the
70+
:external+py3.14:class:`int` implementation actually includes an extended
71+
Euclidean algorithm)
72+
* ``isqrt_rem()`` --- to return both an integer square root and a remainder (which is non-zero only if
73+
the integer isn't a perfect square)
74+
* ``ilog()`` --- integer logarithm, :external+py3.14:func:`math.log`
75+
has special handling for integer arguments. It's unique (with respect to other module
76+
functions) and not documented so far, see issue
77+
`python/cpython#120950 <https://github.com/python/cpython/issues/120950>`_.
78+
* ``fibonacci()`` --- `Fibonacci sequence <https://en.wikipedia.org/wiki/Fibonacci_sequence>`_.
79+
80+
81+
Rationale
82+
=========
83+
84+
Why not fix the :external+py3.14:mod:`math` module documentation instead?
85+
Sure, we can be much more vague in the module preamble (i.e. roughly say
86+
that "the :external+py3.14:mod:`math` module contains some mathematical
87+
functions"), we can accurately describe input/output for each function
88+
and it's behavior (e.g. whether the :external+py3.14:func:`~math.factorial`
89+
output is exact or not, like e.g. the `scipy.special.factorial <https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.factorial.html#scipy.special.factorial>`_, per default).
90+
91+
But the major issue is that the current module mixes different, almost non-interlaced
92+
application domains. Adding more documentation will just highlight this and
93+
make the issue worse for end users (more text to read/skip). And it will not
94+
fix issue with discoverability (to know in which module to find a function, and
95+
that it can be found at all, you need to look at all the functions in the
96+
module), nor with tab-completion.
97+
5698

5799
Specification
58100
=============
59101

60102
The PEP proposes moving the following integer-related functions to a new
61-
module, called ``imath``:
103+
module, called ``intmath``:
62104

63105
* :external+py3.14:func:`~math.comb`
64106
* :external+py3.14:func:`~math.factorial`
@@ -74,9 +116,23 @@ Module functions will accept integers and objects that implement the
74116
object to an integer number. Suitable functions must be computed exactly,
75117
given sufficient time and memory.
76118

77-
Possible extensions for the new module and its scope are discussed in the
78-
`Open Issues <Open Issues_>`_ section. New functions are not part of this
79-
proposal.
119+
The :pypi:`intmath` package will provide new module for older Python versions.
120+
121+
122+
Possible Extensions
123+
===================
124+
125+
New functions (like mentioned in `Motivation <Motivation_>`_ section) are not
126+
part of this proposal.
127+
128+
Though, we should mention that, unless we can just provide bindings to some
129+
well supported mathematical library like the GMP, the module scope should be
130+
limited. For example, no primality testing and factorization, as
131+
production-quality implementatons will require a decent mathematical background
132+
from contributors and belongs rather to specialized libraries.
133+
134+
When proposed function already exists in the :pypi:`gmpy2`, we should prefer a
135+
compatible interface for the stdlib.
80136

81137

82138
Backwards Compatibility
@@ -107,60 +163,25 @@ Reference Implementation
107163
`python/cpython#133909 <https://github.com/python/cpython/pull/133909>`_
108164

109165

110-
Open Issues
111-
===========
166+
Rejected ideas
167+
==============
112168

113169
Module name
114170
-----------
115171

116-
The chosen name seems consistent with one existing domain-specific mathematical module:
117-
:external+py3.14:mod:`cmath` (for complex numbers).
118-
119-
We note the `Imath
120-
<https://github.com/AcademySoftwareFoundation/Imath>`_ C++ library includes
121-
Python bindings with the same name. There is also an :pypi:`imath` project on
122-
PyPI, but only with two releases, with the most recent one four years ago. Its
123-
repository is no longer accessible.
124-
125-
`Polling showed <https://discuss.python.org/t/91337/35>`_ ``intmath`` as another
126-
popular name. The argument made was that the normal mathematical spelling of
127-
the imaginary unit is ``i``, which makes ``imath`` ambiguous. It also has no conflict
128-
with any PyPI module. On the other hand, ``intmath`` may be confused with
129-
interval math or numerical integration.
172+
`Polling showed <https://discuss.python.org/t/92548/67>`_ ``intmath`` as most
173+
popular candidate with ``imath`` as a second winner.
130174

131175
Other proposed names include ``ntheory`` (like SymPy's submodule),
132-
``integermath`` and ``imaths``.
133-
176+
``integermath``, ``zmath``, ``dmath`` and ``imaths``.
134177

135-
Module scope and possible extensions
136-
------------------------------------
137-
138-
Unless we can just provide bindings to some well supported mathematical library
139-
like the GMP, the module scope should be limited. For example, no primality
140-
testing and factorization, as production-quality implementatons will require a
141-
decent mathematical background from contributors and belongs rather to
142-
specialized libraries.
143-
144-
Some possible additions, among those proposed in the initial discussion thread
145-
(see also issue
146-
`python/cpython#81313 <https://github.com/python/cpython/issues/81313>`_):
147-
148-
* ``ceil_div()`` --- for integer ceiling divide, see
149-
`relevant discussion thread <https://discuss.python.org/t/91269>`_.
150-
* ``gcdext()`` --- to solve linear `Diophantine equation <https://en.wikipedia.org/wiki/Diophantine_equation>`_ in two variables (the
151-
:external+py3.14:class:`int` implementation actually includes an extended
152-
Euclidean algorithm)
153-
* ``isqrt_rem()`` --- to return both an integer square root and a remainder (which is non-zero only if
154-
the integer isn't a perfect square)
155-
* ``ilog()`` --- integer logarithm, :external+py3.14:func:`math.log`
156-
has special handling for integer arguments. It's unique (with respect to other module
157-
functions) and not documented so far, see issue
158-
`python/cpython#120950 <https://github.com/python/cpython/issues/120950>`_.
159-
* ``fibonacci()`` --- `Fibonacci sequence <https://en.wikipedia.org/wiki/Fibonacci_sequence>`_.
178+
As a variant, the new module can be added as a submodule of the
179+
:external+py3.14:mod:`math`: ``integer`` (most preferred), ``discrete``
180+
or ``ntheory``.
160181

161182

162-
Rejected ideas
163-
==============
183+
isqrt() renaming
184+
---------------------------------------------
164185

165186
There was a brief discussion about exposing :external+py3.14:func:`math.isqrt`
166187
as ``imath.sqrt`` in the same way that :external+py3.14:func:`cmath.sqrt` is

0 commit comments

Comments
 (0)