11PEP: 791
2- Title: intmath --- module for integer-specific mathematics functions
2+ Title: math.integer --- submodule for integer-specific mathematics functions
33Author: Sergey B Kirpichev <skirpichev@gmail.com>
44Sponsor: Victor Stinner <vstinner@python.org>
55Discussions-To: https://discuss.python.org/t/92548
@@ -15,9 +15,9 @@ Post-History: `12-Jul-2018 <https://mail.python.org/archives/list/python-ideas@p
1515Abstract
1616========
1717
18- This PEP proposes a new module for number-theoretical, combinatorial and other
19- functions defined for integer arguments, like :external+py3.14:func: ` math.gcd `
20- or :external+py3.14:func: `math.isqrt `.
18+ This PEP proposes a new submodule for number-theoretical, combinatorial and
19+ other functions defined for integer arguments, like
20+ :external+py3.14:func: ` math.gcd ` or :external+py3.14:func: `math.isqrt `.
2121
2222
2323Motivation
@@ -34,10 +34,10 @@ explicitly noted otherwise, all return values are floats." This is no longer
3434true: *None * of the functions listed in the `Number-theoretic functions
3535<https://docs.python.org/3.14/library/math.html#number-theoretic-functions> `_
3636subsection of the documentation return a float, but the documentation doesn't
37- say so. In the documentation for the proposed ``intmath `` module the sentence
37+ say so. In the documentation for the proposed ``math.integer `` submodule the sentence
3838"All return values are integers" would be accurate. In a similar way we can
3939simplify the description of the accepted arguments for functions in both the
40- new module and in :external+py3.14:mod: `math `.
40+ new submodule and in :external+py3.14:mod: `math `.
4141
4242Now it's a lot harder to satisfy people's expectations about the module
4343content. For example, should they expect that ``math.factorial(100) `` will
@@ -50,8 +50,8 @@ place for mathematical functions since we also have the
5050:external+py3.14:mod: `cmath ` and :external+py3.14:mod: `statistics ` modules.
5151Let's do the same for integer-related functions. It provides shared context,
5252which reduces verbosity in the documentation and conceptual load. It also aids
53- discoverability through grouping related functions and makes IDE suggestions
54- more helpful.
53+ discoverability through grouping related functions and makes IDE (e.g. new
54+ CPython's REPL) suggestions more helpful.
5555
5656Currently the :external+py3.14:mod: `math ` module code in the CPython is around
57574200LOC, from which the new module code is roughly 1/3 (1300LOC). This is
@@ -81,8 +81,8 @@ and issue `python/cpython#81313
8181* ``c_div() `` and ``n_div() `` --- for integer division with rounding towards
8282 positive infinity (ceiling divide) and to the nearest integer, see `relevant
8383 discussion thread <https://discuss.python.org/t/91269> `_. This is reinvented
84- several times in the stdlib, e.g. in :mod: `datetime ` and
85- :mod: ` fractions ` .
84+ several times in the stdlib, e.g. in :mod: `datetime ` and :mod: ` fractions `.
85+ And it's easy to do this wrongly, as demonstrated by the thread .
8686* ``gcdext() `` --- to solve linear `Diophantine equation
8787 <https://en.wikipedia.org/wiki/Diophantine_equation> `_ in two variables (the
8888 :external+py3.14:class: `int ` implementation actually includes an extended
@@ -97,16 +97,23 @@ and issue `python/cpython#81313
9797* ``fibonacci() `` --- `Fibonacci sequence
9898 <https://en.wikipedia.org/wiki/Fibonacci_sequence> `_.
9999
100+ Separated namespace eliminates possible name clash with existing
101+ :external+py3.14:mod: `math `'s module functions. For example, possible names
102+ ``ceil_div() `` or ``ceildiv() `` for integer ceiling division will interfere
103+ with the :external+py3.14:func: `~math.ceil ` (which is for :class: `float `'s and
104+ *sometimes * does right things for integer division, as an accident --- but
105+ `usually not <https://discuss.python.org/t/91269/6 >`_).
106+
100107
101108Rationale
102109=========
103110
104- Why not fix the :external+py3.14:mod: ` math ` module documentation instead?
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
111+ Is this all about documentation, why not fix it instead? No, it isn't. Sure,
112+ we can be much more vague in the module preamble (i.e. roughly say that "the
113+ :external+py3.14:mod: `math ` module contains some mathematical functions"), we
114+ can accurately describe input/output for each function and its behavior (e.g.
115+ whether the :external+py3.14:func: `~math.factorial ` output is exact or not,
116+ like the `scipy.special.factorial
110117<https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.factorial.html#scipy.special.factorial> `_,
111118per default).
112119
@@ -122,8 +129,7 @@ Specification
122129=============
123130
124131The PEP proposes moving the following integer-related functions to a new
125- module, called ``intmath `` (see `Open Issues `_ for alternatives for
126- it's name):
132+ submodule, called ``math.integer ``:
127133
128134* :external+py3.14:func: `~math.comb `
129135* :external+py3.14:func: `~math.factorial `
@@ -140,7 +146,8 @@ Module functions will accept integers and objects that implement the
140146object to an integer number. Suitable functions must be computed exactly,
141147given sufficient time and memory.
142148
143- The :pypi: `intmath ` package will provide new module for older Python versions.
149+ The :pypi: `intmath ` package will provide new submodule content for older Python
150+ versions.
144151
145152
146153Possible Extensions
@@ -150,7 +157,7 @@ New functions (like mentioned in `Motivation <Motivation_>`_ section) are not
150157part of this proposal.
151158
152159Though, we should mention that, unless we can just provide bindings to some
153- well supported mathematical library like the GMP, the module scope should be
160+ well supported mathematical library like the GMP, the submodule scope should be
154161limited. For example, no primality testing and factorization, as
155162production-quality implementatons will require a decent mathematical background
156163from contributors and belongs rather to specialized libraries.
@@ -169,7 +176,7 @@ As aliases in :external+py3.14:mod:`math` will be kept for an indefinite time
169176How to Teach This
170177=================
171178
172- The new module will be a place for functions, that 1) accept
179+ The new submodule will be a place for functions, that 1) accept
173180:external+py3.14:class: `int `-like arguments and also return integers, and 2)
174181are also in the field of arbitrary-precision integer arithmetic, i.e. have no
175182dependency on the platform floating-point format or behaviour and/or on the
@@ -200,18 +207,18 @@ is ultimately a different function: it is the floor of the square root. It
200207would be confusing to give it the same name (under a different module).
201208
202209
203- Open Issues
204- ===========
210+ Module name
211+ -----------
205212
206213`Polling showed <https://discuss.python.org/t/92548/67 >`_ ``intmath `` as most
207214popular candidate with ``imath `` as a second winner.
208215
209216Other proposed names include ``ntheory `` (like SymPy's submodule),
210217``integermath ``, ``zmath ``, ``dmath `` and ``imaths ``.
211218
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).
219+ But the SC prefers a submodule rather than a new top-level module. Most
220+ popular variants of the :external+py3.14:mod: `math `'s submodule are:
221+ ``integer ``, `` discrete `` or `` ntheory `` (author preference).
215222
216223
217224Acknowledgements
0 commit comments