Skip to content

Commit dcf3cc5

Browse files
gh-81313: Add the math.integer module (PEP-791) (GH-133909)
1 parent 680a5d0 commit dcf3cc5

File tree

22 files changed

+2091
-1748
lines changed

22 files changed

+2091
-1748
lines changed

Doc/library/math.integer.rst

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
:mod:`math.integer` --- integer-specific mathematics functions
2+
==============================================================
3+
4+
.. module:: math.integer
5+
:synopsis: Integer-specific mathematics functions.
6+
7+
.. versionadded:: next
8+
9+
--------------
10+
11+
This module provides access to the mathematical functions defined for integer arguments.
12+
These functions accept integers and objects that implement the
13+
:meth:`~object.__index__` method which is used to convert the object to an integer
14+
number.
15+
16+
The following functions are provided by this module. All return values are
17+
computed exactly and are integers.
18+
19+
20+
.. function:: comb(n, k, /)
21+
22+
Return the number of ways to choose *k* items from *n* items without repetition
23+
and without order.
24+
25+
Evaluates to ``n! / (k! * (n - k)!)`` when ``k <= n`` and evaluates
26+
to zero when ``k > n``.
27+
28+
Also called the binomial coefficient because it is equivalent
29+
to the coefficient of k-th term in polynomial expansion of
30+
``(1 + x)ⁿ``.
31+
32+
Raises :exc:`ValueError` if either of the arguments are negative.
33+
34+
35+
.. function:: factorial(n, /)
36+
37+
Return factorial of the nonnegative integer *n*.
38+
39+
40+
.. function:: gcd(*integers)
41+
42+
Return the greatest common divisor of the specified integer arguments.
43+
If any of the arguments is nonzero, then the returned value is the largest
44+
positive integer that is a divisor of all arguments. If all arguments
45+
are zero, then the returned value is ``0``. ``gcd()`` without arguments
46+
returns ``0``.
47+
48+
49+
.. function:: isqrt(n, /)
50+
51+
Return the integer square root of the nonnegative integer *n*. This is the
52+
floor of the exact square root of *n*, or equivalently the greatest integer
53+
*a* such that *a*\ ² |nbsp| ≤ |nbsp| *n*.
54+
55+
For some applications, it may be more convenient to have the least integer
56+
*a* such that *n* |nbsp| ≤ |nbsp| *a*\ ², or in other words the ceiling of
57+
the exact square root of *n*. For positive *n*, this can be computed using
58+
``a = 1 + isqrt(n - 1)``.
59+
60+
61+
.. |nbsp| unicode:: 0xA0
62+
:trim:
63+
64+
65+
.. function:: lcm(*integers)
66+
67+
Return the least common multiple of the specified integer arguments.
68+
If all arguments are nonzero, then the returned value is the smallest
69+
positive integer that is a multiple of all arguments. If any of the arguments
70+
is zero, then the returned value is ``0``. ``lcm()`` without arguments
71+
returns ``1``.
72+
73+
74+
.. function:: perm(n, k=None, /)
75+
76+
Return the number of ways to choose *k* items from *n* items
77+
without repetition and with order.
78+
79+
Evaluates to ``n! / (n - k)!`` when ``k <= n`` and evaluates
80+
to zero when ``k > n``.
81+
82+
If *k* is not specified or is ``None``, then *k* defaults to *n*
83+
and the function returns ``n!``.
84+
85+
Raises :exc:`ValueError` if either of the arguments are negative.

Doc/library/math.rst

Lines changed: 71 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,6 @@ noted otherwise, all return values are floats.
2727

2828

2929
==================================================== ============================================
30-
**Number-theoretic functions**
31-
--------------------------------------------------------------------------------------------------
32-
:func:`comb(n, k) <comb>` Number of ways to choose *k* items from *n* items without repetition and without order
33-
:func:`factorial(n) <factorial>` *n* factorial
34-
:func:`gcd(*integers) <gcd>` Greatest common divisor of the integer arguments
35-
:func:`isqrt(n) <isqrt>` Integer square root of a nonnegative integer *n*
36-
:func:`lcm(*integers) <lcm>` Least common multiple of the integer arguments
37-
:func:`perm(n, k) <perm>` Number of ways to choose *k* items from *n* items without repetition and with order
38-
3930
**Floating point arithmetic**
4031
--------------------------------------------------------------------------------------------------
4132
:func:`ceil(x) <ceil>` Ceiling of *x*, the smallest integer greater than or equal to *x*
@@ -126,92 +117,6 @@ noted otherwise, all return values are floats.
126117
==================================================== ============================================
127118

128119

129-
Number-theoretic functions
130-
--------------------------
131-
132-
.. function:: comb(n, k)
133-
134-
Return the number of ways to choose *k* items from *n* items without repetition
135-
and without order.
136-
137-
Evaluates to ``n! / (k! * (n - k)!)`` when ``k <= n`` and evaluates
138-
to zero when ``k > n``.
139-
140-
Also called the binomial coefficient because it is equivalent
141-
to the coefficient of k-th term in polynomial expansion of
142-
``(1 + x)ⁿ``.
143-
144-
Raises :exc:`TypeError` if either of the arguments are not integers.
145-
Raises :exc:`ValueError` if either of the arguments are negative.
146-
147-
.. versionadded:: 3.8
148-
149-
150-
.. function:: factorial(n)
151-
152-
Return factorial of the nonnegative integer *n*.
153-
154-
.. versionchanged:: 3.10
155-
Floats with integral values (like ``5.0``) are no longer accepted.
156-
157-
158-
.. function:: gcd(*integers)
159-
160-
Return the greatest common divisor of the specified integer arguments.
161-
If any of the arguments is nonzero, then the returned value is the largest
162-
positive integer that is a divisor of all arguments. If all arguments
163-
are zero, then the returned value is ``0``. ``gcd()`` without arguments
164-
returns ``0``.
165-
166-
.. versionadded:: 3.5
167-
168-
.. versionchanged:: 3.9
169-
Added support for an arbitrary number of arguments. Formerly, only two
170-
arguments were supported.
171-
172-
173-
.. function:: isqrt(n)
174-
175-
Return the integer square root of the nonnegative integer *n*. This is the
176-
floor of the exact square root of *n*, or equivalently the greatest integer
177-
*a* such that *a*\ ² |nbsp| ≤ |nbsp| *n*.
178-
179-
For some applications, it may be more convenient to have the least integer
180-
*a* such that *n* |nbsp| ≤ |nbsp| *a*\ ², or in other words the ceiling of
181-
the exact square root of *n*. For positive *n*, this can be computed using
182-
``a = 1 + isqrt(n - 1)``.
183-
184-
.. versionadded:: 3.8
185-
186-
187-
.. function:: lcm(*integers)
188-
189-
Return the least common multiple of the specified integer arguments.
190-
If all arguments are nonzero, then the returned value is the smallest
191-
positive integer that is a multiple of all arguments. If any of the arguments
192-
is zero, then the returned value is ``0``. ``lcm()`` without arguments
193-
returns ``1``.
194-
195-
.. versionadded:: 3.9
196-
197-
198-
.. function:: perm(n, k=None)
199-
200-
Return the number of ways to choose *k* items from *n* items
201-
without repetition and with order.
202-
203-
Evaluates to ``n! / (n - k)!`` when ``k <= n`` and evaluates
204-
to zero when ``k > n``.
205-
206-
If *k* is not specified or is ``None``, then *k* defaults to *n*
207-
and the function returns ``n!``.
208-
209-
Raises :exc:`TypeError` if either of the arguments are not integers.
210-
Raises :exc:`ValueError` if either of the arguments are negative.
211-
212-
.. versionadded:: 3.8
213-
214-
215120
Floating point arithmetic
216121
-------------------------
217122

@@ -812,6 +717,75 @@ Special functions
812717
.. versionadded:: 3.2
813718

814719

720+
Number-theoretic functions
721+
--------------------------
722+
723+
For backward compatibility, the :mod:`math` module provides also aliases of
724+
the following functions from the :mod:`math.integer` module:
725+
726+
.. list-table::
727+
728+
* - .. function:: comb(n, k)
729+
:no-typesetting:
730+
731+
:func:`comb(n, k) <math.integer.comb>`
732+
- Number of ways to choose *k* items from *n* items without repetition
733+
and without order
734+
735+
* - .. function:: factorial(n)
736+
:no-typesetting:
737+
738+
:func:`factorial(n) <math.integer.factorial>`
739+
- *n* factorial
740+
741+
* - .. function:: gcd(*integers)
742+
:no-typesetting:
743+
744+
:func:`gcd(*integers) <math.integer.gcd>`
745+
- Greatest common divisor of the integer arguments
746+
747+
* - .. function:: isqrt(n)
748+
:no-typesetting:
749+
750+
:func:`isqrt(n) <math.integer.isqrt>`
751+
- Integer square root of a nonnegative integer *n*
752+
753+
* - .. function:: lcm(*integers)
754+
:no-typesetting:
755+
756+
:func:`lcm(*integers) <math.integer.lcm>`
757+
- Least common multiple of the integer arguments
758+
759+
* - .. function:: perm(n, k)
760+
:no-typesetting:
761+
762+
:func:`perm(n, k) <math.integer.perm>`
763+
- Number of ways to choose *k* items from *n* items without repetition
764+
and with order
765+
766+
.. versionadded:: 3.5
767+
The :func:`gcd` function.
768+
769+
.. versionadded:: 3.8
770+
The :func:`comb`, :func:`perm` and :func:`isqrt` functions.
771+
772+
.. versionadded:: 3.9
773+
The :func:`lcm` function.
774+
775+
.. versionchanged:: 3.9
776+
Added support for an arbitrary number of arguments in the :func:`gcd`
777+
function.
778+
Formerly, only two arguments were supported.
779+
780+
.. versionchanged:: 3.10
781+
Floats with integral values (like ``5.0``) are no longer accepted in the
782+
:func:`factorial` function.
783+
784+
.. deprecated:: next
785+
These aliases are :term:`soft deprecated` in favor of the
786+
:mod:`math.integer` functions.
787+
788+
815789
Constants
816790
---------
817791

@@ -894,5 +868,5 @@ Constants
894868
Module :mod:`cmath`
895869
Complex number versions of many of these functions.
896870

897-
.. |nbsp| unicode:: 0xA0
898-
:trim:
871+
Module :mod:`math.integer`
872+
Integer-specific mathematics functions.

Doc/library/numeric.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ The following modules are documented in this chapter:
1919

2020
numbers.rst
2121
math.rst
22+
math.integer.rst
2223
cmath.rst
2324
decimal.rst
2425
fractions.rst

Doc/whatsnew/3.15.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,12 @@ Other language changes
311311
New modules
312312
===========
313313

314-
* None yet.
314+
math.integer
315+
------------
316+
317+
This module provides access to the mathematical functions for integer
318+
arguments (:pep:`791`).
319+
(Contributed by Serhiy Storchaka in :gh:`81313`.)
315320

316321

317322
Improved modules

Include/internal/pycore_import.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ extern int _PyImport_IsInitialized(PyInterpreterState *);
1717
// Export for 'pyexpat' shared extension
1818
PyAPI_FUNC(int) _PyImport_SetModule(PyObject *name, PyObject *module);
1919

20-
extern int _PyImport_SetModuleString(const char *name, PyObject* module);
20+
// Export for 'math' shared extension
21+
PyAPI_FUNC(int) _PyImport_SetModuleString(const char *name, PyObject* module);
2122

2223
extern void _PyImport_AcquireLock(PyInterpreterState *interp);
2324
extern void _PyImport_ReleaseLock(PyInterpreterState *interp);

Lib/test/test_capi/test_misc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ def test_getitem_with_error(self):
307307
CURRENT_THREAD_REGEX +
308308
r' File .*, line 6 in <module>\n'
309309
r'\n'
310-
r'Extension modules: _testcapi \(total: 1\)\n')
310+
r'Extension modules: ')
311311
else:
312312
# Python built with NDEBUG macro defined:
313313
# test _Py_CheckFunctionResult() instead.

0 commit comments

Comments
 (0)