Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
6e47acd
gh-81313: Add the imath module
serhiy-storchaka May 11, 2025
62cb235
Fix Sphinx warning.
serhiy-storchaka May 11, 2025
091dab6
Support multiple interpreters.
serhiy-storchaka May 12, 2025
71d7f4f
_PyLong_NumBits returns int64_t now.
serhiy-storchaka May 12, 2025
7b61c5e
Fix build on Windows.
serhiy-storchaka May 12, 2025
eaea554
Add in a toctree.
serhiy-storchaka May 12, 2025
da3ceab
Regen generated files.
serhiy-storchaka May 12, 2025
2ab90a5
Merge branch 'main' into imath3
serhiy-storchaka Jun 1, 2025
3299bf7
imath -> intmath
serhiy-storchaka Jun 1, 2025
9b6a151
Remove ilog2().
serhiy-storchaka Jun 1, 2025
7b327ec
Remove unneeded import.
serhiy-storchaka Jun 1, 2025
3194384
Apply suggestions from code review
serhiy-storchaka Jun 1, 2025
59b1be1
Apply suggestions from code review
serhiy-storchaka Jun 1, 2025
8137058
Update clinic.
serhiy-storchaka Jun 1, 2025
ee0dbb6
make regen-stdlib-module-names
serhiy-storchaka Jun 1, 2025
6f5061f
Merge branch 'main' into imath3
serhiy-storchaka Oct 23, 2025
2a9341d
Apply suggestions from code review
serhiy-storchaka Oct 23, 2025
b6a1078
Update Modules/intmathmodule.c
serhiy-storchaka Oct 23, 2025
b6a6164
egenerate clinic.
serhiy-storchaka Oct 23, 2025
5bdee0c
intmath -> math.integer
serhiy-storchaka Oct 23, 2025
f7d2a8d
Fix Python/stdlib_module_names.h.
serhiy-storchaka Oct 28, 2025
3b7bd99
Merge branch 'main' into imath3
serhiy-storchaka Oct 29, 2025
a5bac9c
Fix the module name.
serhiy-storchaka Oct 29, 2025
4425774
Rewrite the math module documentation.
serhiy-storchaka Oct 29, 2025
21791cd
Fix test_capi on macOS.
serhiy-storchaka Oct 29, 2025
835a46c
Update Misc/NEWS.d/next/Library/2019-06-02-13-56-16.gh-issue-81313.ax…
serhiy-storchaka Oct 29, 2025
5599504
Apply suggestions from code review
serhiy-storchaka Oct 31, 2025
f7a06e5
soft deprecated
serhiy-storchaka Oct 31, 2025
badeb5c
Move the nbsp definition.
serhiy-storchaka Oct 31, 2025
0f385a5
Update documentation.
serhiy-storchaka Oct 31, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions Doc/library/math.integer.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
:mod:`math.integer` --- integer-specific mathematics functions
==============================================================

.. module:: math.integer
:synopsis: Integer-specific mathematics functions.

.. versionadded:: next

--------------

This module provides access to the mathematical functions defined for integer arguments.
These functions accept integers and objects that implement the
:meth:`~object.__index__` method which is used to convert the object to an integer
number.

The following functions are provided by this module. All return values are
computed exactly and are integers.


.. function:: comb(n, k, /)

Return the number of ways to choose *k* items from *n* items without repetition
and without order.

Evaluates to ``n! / (k! * (n - k)!)`` when ``k <= n`` and evaluates
to zero when ``k > n``.

Also called the binomial coefficient because it is equivalent
to the coefficient of k-th term in polynomial expansion of
``(1 + x)ⁿ``.

Raises :exc:`ValueError` if either of the arguments are negative.


.. function:: factorial(n, /)

Return factorial of the nonnegative integer *n*.


.. function:: gcd(*integers)

Return the greatest common divisor of the specified integer arguments.
If any of the arguments is nonzero, then the returned value is the largest
positive integer that is a divisor of all arguments. If all arguments
are zero, then the returned value is ``0``. ``gcd()`` without arguments
returns ``0``.


.. function:: isqrt(n, /)

Return the integer square root of the nonnegative integer *n*. This is the
floor of the exact square root of *n*, or equivalently the greatest integer
*a* such that *a*\ ² |nbsp| ≤ |nbsp| *n*.

For some applications, it may be more convenient to have the least integer
*a* such that *n* |nbsp| ≤ |nbsp| *a*\ ², or in other words the ceiling of
the exact square root of *n*. For positive *n*, this can be computed using
``a = 1 + isqrt(n - 1)``.


.. |nbsp| unicode:: 0xA0
:trim:


.. function:: lcm(*integers)

Return the least common multiple of the specified integer arguments.
If all arguments are nonzero, then the returned value is the smallest
positive integer that is a multiple of all arguments. If any of the arguments
is zero, then the returned value is ``0``. ``lcm()`` without arguments
returns ``1``.


.. function:: perm(n, k=None, /)

Return the number of ways to choose *k* items from *n* items
without repetition and with order.

Evaluates to ``n! / (n - k)!`` when ``k <= n`` and evaluates
to zero when ``k > n``.

If *k* is not specified or is ``None``, then *k* defaults to *n*
and the function returns ``n!``.

Raises :exc:`ValueError` if either of the arguments are negative.
168 changes: 71 additions & 97 deletions Doc/library/math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ noted otherwise, all return values are floats.


==================================================== ============================================
**Number-theoretic functions**
--------------------------------------------------------------------------------------------------
:func:`comb(n, k) <comb>` Number of ways to choose *k* items from *n* items without repetition and without order
:func:`factorial(n) <factorial>` *n* factorial
:func:`gcd(*integers) <gcd>` Greatest common divisor of the integer arguments
:func:`isqrt(n) <isqrt>` Integer square root of a nonnegative integer *n*
:func:`lcm(*integers) <lcm>` Least common multiple of the integer arguments
:func:`perm(n, k) <perm>` Number of ways to choose *k* items from *n* items without repetition and with order

**Floating point arithmetic**
--------------------------------------------------------------------------------------------------
:func:`ceil(x) <ceil>` Ceiling of *x*, the smallest integer greater than or equal to *x*
Expand Down Expand Up @@ -126,92 +117,6 @@ noted otherwise, all return values are floats.
==================================================== ============================================


Number-theoretic functions
--------------------------

.. function:: comb(n, k)

Return the number of ways to choose *k* items from *n* items without repetition
and without order.

Evaluates to ``n! / (k! * (n - k)!)`` when ``k <= n`` and evaluates
to zero when ``k > n``.

Also called the binomial coefficient because it is equivalent
to the coefficient of k-th term in polynomial expansion of
``(1 + x)ⁿ``.

Raises :exc:`TypeError` if either of the arguments are not integers.
Raises :exc:`ValueError` if either of the arguments are negative.

.. versionadded:: 3.8


.. function:: factorial(n)

Return factorial of the nonnegative integer *n*.

.. versionchanged:: 3.10
Floats with integral values (like ``5.0``) are no longer accepted.


.. function:: gcd(*integers)

Return the greatest common divisor of the specified integer arguments.
If any of the arguments is nonzero, then the returned value is the largest
positive integer that is a divisor of all arguments. If all arguments
are zero, then the returned value is ``0``. ``gcd()`` without arguments
returns ``0``.

.. versionadded:: 3.5

.. versionchanged:: 3.9
Added support for an arbitrary number of arguments. Formerly, only two
arguments were supported.


.. function:: isqrt(n)

Return the integer square root of the nonnegative integer *n*. This is the
floor of the exact square root of *n*, or equivalently the greatest integer
*a* such that *a*\ ² |nbsp| ≤ |nbsp| *n*.

For some applications, it may be more convenient to have the least integer
*a* such that *n* |nbsp| ≤ |nbsp| *a*\ ², or in other words the ceiling of
the exact square root of *n*. For positive *n*, this can be computed using
``a = 1 + isqrt(n - 1)``.

.. versionadded:: 3.8


.. function:: lcm(*integers)

Return the least common multiple of the specified integer arguments.
If all arguments are nonzero, then the returned value is the smallest
positive integer that is a multiple of all arguments. If any of the arguments
is zero, then the returned value is ``0``. ``lcm()`` without arguments
returns ``1``.

.. versionadded:: 3.9


.. function:: perm(n, k=None)

Return the number of ways to choose *k* items from *n* items
without repetition and with order.

Evaluates to ``n! / (n - k)!`` when ``k <= n`` and evaluates
to zero when ``k > n``.

If *k* is not specified or is ``None``, then *k* defaults to *n*
and the function returns ``n!``.

Raises :exc:`TypeError` if either of the arguments are not integers.
Raises :exc:`ValueError` if either of the arguments are negative.

.. versionadded:: 3.8


Floating point arithmetic
-------------------------

Expand Down Expand Up @@ -812,6 +717,75 @@ Special functions
.. versionadded:: 3.2


Number-theoretic functions
--------------------------

For backward compatibility, the :mod:`math` module provides also aliases of
the following functions from the :mod:`math.integer` module:

.. list-table::

* - .. function:: comb(n, k)
:no-typesetting:

:func:`comb(n, k) <math.integer.comb>`
- Number of ways to choose *k* items from *n* items without repetition
and without order

* - .. function:: factorial(n)
:no-typesetting:

:func:`factorial(n) <math.integer.factorial>`
- *n* factorial

* - .. function:: gcd(*integers)
:no-typesetting:

:func:`gcd(*integers) <math.integer.gcd>`
- Greatest common divisor of the integer arguments

* - .. function:: isqrt(n)
:no-typesetting:

:func:`isqrt(n) <math.integer.isqrt>`
- Integer square root of a nonnegative integer *n*

* - .. function:: lcm(*integers)
:no-typesetting:

:func:`lcm(*integers) <math.integer.lcm>`
- Least common multiple of the integer arguments

* - .. function:: perm(n, k)
:no-typesetting:

:func:`perm(n, k) <math.integer.perm>`
- Number of ways to choose *k* items from *n* items without repetition
and with order

.. versionadded:: 3.5
The :func:`gcd` function.

.. versionadded:: 3.8
The :func:`comb`, :func:`perm` and :func:`isqrt` functions.

.. versionadded:: 3.9
The :func:`lcm` function.

.. versionchanged:: 3.9
Added support for an arbitrary number of arguments in the :func:`gcd`
function.
Formerly, only two arguments were supported.

.. versionchanged:: 3.10
Floats with integral values (like ``5.0``) are no longer accepted in the
:func:`factorial` function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a versionchanged to mention that these functions became aliases to math.integer functions?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This did not change their behavior.

.. deprecated:: next
These aliases are :term:`soft deprecated` in favor of the
:mod:`math.integer` functions.


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add .. deprecated:: 3.15 to repeat that these functions are soft deprecated and math.integer functions should be used instead.

Constants
---------

Expand Down Expand Up @@ -894,5 +868,5 @@ Constants
Module :mod:`cmath`
Complex number versions of many of these functions.

.. |nbsp| unicode:: 0xA0
:trim:
Module :mod:`math.integer`
Integer-specific mathematics functions.
1 change: 1 addition & 0 deletions Doc/library/numeric.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ The following modules are documented in this chapter:

numbers.rst
math.rst
math.integer.rst
cmath.rst
decimal.rst
fractions.rst
Expand Down
7 changes: 6 additions & 1 deletion Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,12 @@ Other language changes
New modules
===========

* None yet.
math.integer
------------

This module provides access to the mathematical functions for integer
arguments (:pep:`791`).
(Contributed by Serhiy Storchaka in :gh:`81313`.)


Improved modules
Expand Down
3 changes: 2 additions & 1 deletion Include/internal/pycore_import.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ extern int _PyImport_IsInitialized(PyInterpreterState *);
// Export for 'pyexpat' shared extension
PyAPI_FUNC(int) _PyImport_SetModule(PyObject *name, PyObject *module);

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

extern void _PyImport_AcquireLock(PyInterpreterState *interp);
extern void _PyImport_ReleaseLock(PyInterpreterState *interp);
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_capi/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def test_getitem_with_error(self):
CURRENT_THREAD_REGEX +
r' File .*, line 6 in <module>\n'
r'\n'
r'Extension modules: _testcapi \(total: 1\)\n')
r'Extension modules: ')
else:
# Python built with NDEBUG macro defined:
# test _Py_CheckFunctionResult() instead.
Expand Down
Loading
Loading