Skip to content

Commit 96bb9f2

Browse files
skirpichevhugovkvstinner
authored
PEP 791: final edits (#4586)
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 3ee1626 commit 96bb9f2

File tree

1 file changed

+78
-71
lines changed

1 file changed

+78
-71
lines changed

peps/pep-0791.rst

Lines changed: 78 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,27 @@ Abstract
1616
========
1717

1818
This PEP proposes a new module for number-theoretical, combinatorial and other
19-
functions defined for integer arguments, like
20-
:external+py3.14:func:`math.gcd` or :external+py3.14:func:`math.isqrt`.
19+
functions defined for integer arguments, like :external+py3.14:func:`math.gcd`
20+
or :external+py3.14:func:`math.isqrt`.
2121

2222

2323
Motivation
2424
==========
2525

26-
The :external+py3.14:mod:`math` documentation says: "This module provides access
27-
to the mathematical functions defined by the C standard." But,
28-
over time the module was populated with functions that aren't related to
29-
the C standard or floating-point arithmetics. Now it's much harder to describe
26+
The :external+py3.14:mod:`math` documentation says: "This module provides
27+
access to the mathematical functions defined by the C standard." But, over
28+
time the module was populated with functions that aren't related to the C
29+
standard or floating-point arithmetics. Now it's much harder to describe
3030
module scope, content and interfaces (returned values or accepted arguments).
3131

32-
For example, the :external+py3.14:mod:`math` module documentation says: "Except
33-
when explicitly noted otherwise, all return values are floats." This is no
34-
longer true: *None* of the functions listed in the `Number-theoretic
35-
functions <https://docs.python.org/3.14/library/math.html#number-theoretic-functions>`_
36-
subsection of the documentation return a float, but the
37-
documentation doesn't say so. In the documentation for the proposed ``intmath`` module the sentence "All
38-
return values are integers." would be accurate. In a similar way we
39-
can simplify the description of the accepted arguments for functions in both the
32+
For example, the following statement from the documentation: "Except when
33+
explicitly noted otherwise, all return values are floats." This is no longer
34+
true: *None* of the functions listed in the `Number-theoretic functions
35+
<https://docs.python.org/3.14/library/math.html#number-theoretic-functions>`_
36+
subsection of the documentation return a float, but the documentation doesn't
37+
say so. In the documentation for the proposed ``intmath`` module the sentence
38+
"All return values are integers" would be accurate. In a similar way we can
39+
simplify the description of the accepted arguments for functions in both the
4040
new module and in :external+py3.14:mod:`math`.
4141

4242
Now it's a lot harder to satisfy people's expectations about the module
@@ -45,12 +45,13 @@ return an exact answer? Many languages, Python packages (like :pypi:`scipy`)
4545
or pocket calculators have functions with same or similar name, that return a
4646
floating-point value, which is only an approximation in this example.
4747

48-
Apparently, the :external+py3.14:mod:`math` module can't serve as a catch-all place
49-
for mathematical functions since we also have the :external+py3.14:mod:`cmath` and
50-
:external+py3.14:mod:`statistics` modules. Let's do the same for integer-related
51-
functions. It provides shared context, which reduces verbosity in the
52-
documentation and conceptual load. It also aids discoverability through
53-
grouping related functions and makes IDE suggestions more helpful.
48+
Apparently, the :external+py3.14:mod:`math` module can't serve as a catch-all
49+
place for mathematical functions since we also have the
50+
:external+py3.14:mod:`cmath` and :external+py3.14:mod:`statistics` modules.
51+
Let's do the same for integer-related functions. It provides shared context,
52+
which reduces verbosity in the documentation and conceptual load. It also aids
53+
discoverability through grouping related functions and makes IDE suggestions
54+
more helpful.
5455

5556
Currently the :external+py3.14:mod:`math` module code in the CPython is around
5657
4200LOC, from which the new module code is roughly 1/3 (1300LOC). This is
@@ -62,61 +63,67 @@ And this situation tends to get worse. When the module split `was first
6263
proposed
6364
<https://mail.python.org/archives/list/python-ideas@python.org/thread/YYJ5YJBJNCVXQWK5K3WSVNMPUSV56LOR/>`_,
6465
there were only two integer-related functions:
65-
:external+py3.14:func:`~math.factorial` (accepting also :class:`float`'s, like other
66-
functions in the module) and :external+py3.14:func:`~math.gcd` (moved from the
67-
::external+py3.14:mod:`fractions` module). Then
66+
:external+py3.14:func:`~math.factorial` (accepting also :class:`float`'s, like
67+
other functions in the module) and :external+py3.14:func:`~math.gcd` (moved
68+
from the :external+py3.14:mod:`fractions` module). Then
6869
:external+py3.14:func:`~math.isqrt`, :external+py3.14:func:`~math.comb` and
6970
:external+py3.14:func:`~math.perm` were added, and addition of the new module
7071
was `proposed second time <https://github.com/python/cpython/issues/81313>`_,
7172
so all new functions would go directly to it, without littering the
72-
:external+py3.14:mod:`math` namespace.
73-
Now there are six functions and :external+py3.14:func:`~math.factorial` doesn't accept
74-
:class:`float`'s anymore.
73+
:external+py3.14:mod:`math` namespace. Now there are six functions and
74+
:external+py3.14:func:`~math.factorial` doesn't accept :class:`float`\ s
75+
anymore.
7576

7677
Some possible additions, among those proposed in the initial discussion thread
77-
and issue
78-
`python/cpython#81313 <https://github.com/python/cpython/issues/81313>`_ are:
78+
and issue `python/cpython#81313
79+
<https://github.com/python/cpython/issues/81313>`_ are:
7980

8081
* ``c_div()`` and ``n_div()`` --- for integer division with rounding towards
8182
positive infinity (ceiling divide) and to the nearest integer, see `relevant
8283
discussion thread <https://discuss.python.org/t/91269>`_. This is reinvented
83-
several times in the stdlib, e.g. in the :mod:`datetime` and the
84+
several times in the stdlib, e.g. in :mod:`datetime` and
8485
:mod:`fractions`.
85-
* ``gcdext()`` --- to solve linear `Diophantine equation <https://en.wikipedia.org/wiki/Diophantine_equation>`_ in two variables (the
86+
* ``gcdext()`` --- to solve linear `Diophantine equation
87+
<https://en.wikipedia.org/wiki/Diophantine_equation>`_ in two variables (the
8688
:external+py3.14:class:`int` implementation actually includes an extended
8789
Euclidean algorithm)
88-
* ``isqrt_rem()`` --- to return both an integer square root and a remainder (which is non-zero only if
89-
the integer isn't a perfect square)
90-
* ``ilog()`` --- integer logarithm, :external+py3.14:func:`math.log`
91-
has special handling for integer arguments. It's unique (with respect to other module
92-
functions) and not documented so far, see issue
93-
`python/cpython#120950 <https://github.com/python/cpython/issues/120950>`_.
94-
* ``fibonacci()`` --- `Fibonacci sequence <https://en.wikipedia.org/wiki/Fibonacci_sequence>`_.
90+
* ``isqrt_rem()`` --- to return both an integer square root and a remainder
91+
(which is non-zero only if the integer isn't a perfect square)
92+
93+
* ``ilog()`` --- integer logarithm, :external+py3.14:func:`math.log` has
94+
special handling for integer arguments. It's unique (with respect to other
95+
module functions) and not documented so far, see issue `python/cpython#120950
96+
<https://github.com/python/cpython/issues/120950>`_.
97+
* ``fibonacci()`` --- `Fibonacci sequence
98+
<https://en.wikipedia.org/wiki/Fibonacci_sequence>`_.
9599

96100

97101
Rationale
98102
=========
99103

100104
Why not fix the :external+py3.14:mod:`math` module documentation instead?
101-
Sure, we can be much more vague in the module preamble (i.e. roughly say
102-
that "the :external+py3.14:mod:`math` module contains some mathematical
103-
functions"), we can accurately describe input/output for each function
104-
and it's behavior (e.g. whether the :external+py3.14:func:`~math.factorial`
105-
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).
106-
107-
But the major issue is that the current module mixes different, almost non-interlaced
108-
application domains. Adding more documentation will just highlight this and
109-
make the issue worse for end users (more text to read/skip). And it will not
110-
fix issue with discoverability (to know in which module to find a function, and
111-
that it can be found at all, you need to look at all the functions in the
112-
module), nor with tab-completion.
105+
Sure, we can be much more vague in the module preamble (i.e. roughly say that
106+
"the :external+py3.14:mod:`math` module contains some mathematical functions"),
107+
we can accurately describe input/output for each function and its behavior
108+
(e.g. whether the :external+py3.14:func:`~math.factorial` output is exact or
109+
not, like the `scipy.special.factorial
110+
<https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.factorial.html#scipy.special.factorial>`_,
111+
per default).
112+
113+
But the major issue is that the current module mixes different, almost
114+
non-interlaced application domains. Adding more documentation will just
115+
highlight this and make the issue worse for end users (more text to read/skip).
116+
And it will not fix the issue with discoverability (to know in which module to find
117+
a function, and that it can be found at all, you need to look at all the
118+
functions in the module), nor with tab-completion.
113119

114120

115121
Specification
116122
=============
117123

118124
The PEP proposes moving the following integer-related functions to a new
119-
module, called ``intmath``:
125+
module, called ``intmath`` (see `Open Issues`_ for alternatives for
126+
it's name):
120127

121128
* :external+py3.14:func:`~math.comb`
122129
* :external+py3.14:func:`~math.factorial`
@@ -163,8 +170,8 @@ How to Teach This
163170
=================
164171

165172
The new module will be a place for functions, that 1) accept
166-
:external+py3.14:class:`int`-like arguments and also return integers, and 2) are
167-
also in the field of arbitrary-precision integer arithmetic, i.e. have no
173+
:external+py3.14:class:`int`-like arguments and also return integers, and 2)
174+
are also in the field of arbitrary-precision integer arithmetic, i.e. have no
168175
dependency on the platform floating-point format or behaviour and/or on the
169176
platform math library (``libm``).
170177

@@ -183,20 +190,6 @@ Reference Implementation
183190
Rejected ideas
184191
==============
185192

186-
Module name
187-
-----------
188-
189-
`Polling showed <https://discuss.python.org/t/92548/67>`_ ``intmath`` as most
190-
popular candidate with ``imath`` as a second winner.
191-
192-
Other proposed names include ``ntheory`` (like SymPy's submodule),
193-
``integermath``, ``zmath``, ``dmath`` and ``imaths``.
194-
195-
As a variant, the new module can be added as a submodule of the
196-
:external+py3.14:mod:`math`: ``integer`` (most preferred), ``discrete``
197-
or ``ntheory``.
198-
199-
200193
isqrt() renaming
201194
---------------------------------------------
202195

@@ -207,16 +200,30 @@ is ultimately a different function: it is the floor of the square root. It
207200
would be confusing to give it the same name (under a different module).
208201

209202

203+
Open Issues
204+
===========
205+
206+
`Polling showed <https://discuss.python.org/t/92548/67>`_ ``intmath`` as most
207+
popular candidate with ``imath`` as a second winner.
208+
209+
Other proposed names include ``ntheory`` (like SymPy's submodule),
210+
``integermath``, ``zmath``, ``dmath`` and ``imaths``.
211+
212+
As a variant, the new module can be added as a submodule of the
213+
:external+py3.14:mod:`math`: ``integer`` (most preferred), ``discrete`` or
214+
``ntheory`` (author preference).
215+
216+
210217
Acknowledgements
211218
================
212219

213-
Thanks to Tim Peters for reviving the idea of splitting the :external+py3.14:mod:`math`
214-
module. Thanks to Neil Girdhar for substantial improvements of
215-
the initial draft.
220+
Thanks to Tim Peters for reviving the idea of splitting the
221+
:external+py3.14:mod:`math` module. Thanks to Neil Girdhar for substantial
222+
improvements of the initial draft.
216223

217224

218225
Copyright
219226
=========
220227

221-
This document is placed in the public domain or under the
222-
CC0-1.0-Universal license, whichever is more permissive.
228+
This document is placed in the public domain or under the CC0-1.0-Universal
229+
license, whichever is more permissive.

0 commit comments

Comments
 (0)