@@ -16,27 +16,27 @@ Abstract
1616========
1717
1818This 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
2323Motivation
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
3030module scope, content and interfaces (returned values or accepted arguments).
3131
32- For example, following statement from the documentation: "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, 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
4040new module and in :external+py3.14:mod: `math `.
4141
4242Now 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`)
4545or pocket calculators have functions with same or similar name, that return a
4646floating-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
5556Currently the :external+py3.14:mod: `math ` module code in the CPython is around
56574200LOC, from which the new module code is roughly 1/3 (1300LOC). This is
@@ -62,54 +63,59 @@ And this situation tends to get worse. When the module split `was first
6263proposed
6364<https://mail.python.org/archives/list/python-ideas@python.org/thread/YYJ5YJBJNCVXQWK5K3WSVNMPUSV56LOR/> `_,
6465there 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
7071was `proposed second time <https://github.com/python/cpython/issues/81313 >`_,
7172so 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
7677Some 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
8384 several times in the stdlib, e.g. in the :mod: `datetime ` and the
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
97101Rationale
98102=========
99103
100104Why 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 it's behavior
108+ (e.g. whether the :external+py3.14:func: `~math.factorial ` output is exact or
109+ not, like e.g. 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 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
115121Specification
@@ -163,8 +169,8 @@ How to Teach This
163169=================
164170
165171The 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
172+ :external+py3.14:class: `int `-like arguments and also return integers, and 2)
173+ are also in the field of arbitrary-precision integer arithmetic, i.e. have no
168174dependency on the platform floating-point format or behaviour and/or on the
169175platform math library (``libm ``).
170176
@@ -193,8 +199,8 @@ Other proposed names include ``ntheory`` (like SymPy's submodule),
193199``integermath ``, ``zmath ``, ``dmath `` and ``imaths ``.
194200
195201As 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 ``.
202+ :external+py3.14:mod: `math `: ``integer `` (most preferred), ``discrete `` or
203+ ``ntheory ``.
198204
199205
200206isqrt() renaming
@@ -210,13 +216,13 @@ would be confusing to give it the same name (under a different module).
210216Acknowledgements
211217================
212218
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.
219+ Thanks to Tim Peters for reviving the idea of splitting the
220+ :external+py3.14:mod: ` math ` module. Thanks to Neil Girdhar for substantial
221+ improvements of the initial draft.
216222
217223
218224Copyright
219225=========
220226
221- This document is placed in the public domain or under the
222- CC0-1.0-Universal license, whichever is more permissive.
227+ This document is placed in the public domain or under the CC0-1.0-Universal
228+ license, whichever is more permissive.
0 commit comments