Skip to content

gh-81313: Add the intmath module (PEP-791) #133909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
86 changes: 86 additions & 0 deletions Doc/library/intmath.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
:mod:`intmath` --- integer-specific mathematics functions
=========================================================

.. module:: intmath
: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. They cannot be used with floating-point numbers or complex
numbers.
Comment on lines +14 to +15
Copy link
Contributor

Choose a reason for hiding this comment

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

Last sentence looks redundant, though maybe it worth to be here.


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(a, b)
Copy link
Member

Choose a reason for hiding this comment

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

Isn't it gcd(*integers)?

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
.. function:: gcd(a, b)
.. 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)``.


.. 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.


.. |nbsp| unicode:: 0xA0
:trim:
54 changes: 9 additions & 45 deletions Doc/library/math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -124,39 +124,27 @@ 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``.
These functions are aliases of corresponding functions in the
:mod:`intmath` module.

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

Raises :exc:`TypeError` if either of the arguments are not integers.
Raises :exc:`ValueError` if either of the arguments are negative.
An alias of :func:`intmath.comb`.

.. versionadded:: 3.8


.. function:: factorial(n)

Return factorial of the nonnegative integer *n*.
An alias of :func:`intmath.factorial`.

.. 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``.
An alias of :func:`intmath.gcd`.

.. versionadded:: 3.5

Expand All @@ -167,42 +155,21 @@ Number-theoretic functions

.. 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)``.
An alias of :func:`intmath.isqrt`.

.. 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``.
An alias of :func:`intmath.lcm`.

.. 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.
An alias of :func:`intmath.perm`.

.. versionadded:: 3.8

Expand Down Expand Up @@ -837,6 +804,3 @@ Constants

Module :mod:`cmath`
Complex number versions of many of these functions.

.. |nbsp| unicode:: 0xA0
:trim:
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
intmath.rst
cmath.rst
decimal.rst
fractions.rst
Expand Down
6 changes: 5 additions & 1 deletion Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ Other language changes
New modules
===========

* None yet.
intmath
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 an entry under the PEP-sized items as well?

Copy link
Contributor

Choose a reason for hiding this comment

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

Not all PEP's mentioned in release Summary (e.g. PEP 757 for the 3.13).

Copy link
Member

Choose a reason for hiding this comment

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

Yes, but for 3.14, I think we're mentioning them all? @hugovk do you want to mention the PEP? Or at least, we should have a link to the PEP. There is none at the moment (annotationlib got one)

Copy link
Member

Choose a reason for hiding this comment

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

Ask me after the PEP has been accepted :)

-------

This module provides access to the mathematical functions for integer arguments.
(Contributed by Serhiy Storchaka in :gh:`81313`.)
Comment on lines +89 to +90
Copy link
Contributor

Choose a reason for hiding this comment

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

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



Improved modules
Expand Down
Loading
Loading