Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ peps/pep-0788.rst @ZeroIntensity @vstinner
# ...
peps/pep-0789.rst @njsmith
peps/pep-0790.rst @hugovk
peps/pep-0791.rst @vstinner
Comment thread
skirpichev marked this conversation as resolved.
# ...
peps/pep-0801.rst @warsaw
# ...
Expand Down
165 changes: 165 additions & 0 deletions peps/pep-0791.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
PEP: 791
Title: imath --- module for number-theoretic functions
Author: Sergey B Kirpichev <skirpichev@gmail.com>
Sponsor: Victor Stinner <vstinner@python.org>
Discussions-To: Pending
Status: Draft
Type: Standards Track
Created: 10-May-2025
Comment thread
skirpichev marked this conversation as resolved.
Outdated
Python-Version: 3.15
Post-History: 02-Jun-2019,
Comment thread
skirpichev marked this conversation as resolved.
Outdated
`09-May-2025 <https://discuss.python.org/t/91337>`__,


Abstract
========

This PEP proposes a new module for number-theoretical, combinatorial and other
integer-valued functions defined for integer arguments, like
:external+py3.14:func:`math.gcd` or :external+py3.14:func:`math.isqrt`.
Comment thread
skirpichev marked this conversation as resolved.
Comment thread
skirpichev marked this conversation as resolved.


Motivation
==========

The :external+py3.14:mod:`math` documentation says: "This module provides access
to the mathematical functions defined by the C standard." But as a state of
Comment thread
skirpichev marked this conversation as resolved.
Outdated
art, over time the module was populated with functions that aren't related to
the C standard or floating-point arithmetics. Now it's much harder to describe
module scope, content and interfaces (returned values or accepted arguments).
Comment thread
skirpichev marked this conversation as resolved.

For example, the :external+py3.14:mod:`math` module documentation says: "Except
when explicitly noted otherwise, all return values are floats." This is not
Comment thread
skirpichev marked this conversation as resolved.
Outdated
longer true: *None* of the functions listed in the "Number-theoretic
functions" [1]_ subsection of the documentation returns a float, but the
documentation doesn't say so. In the proposed module a similar sentence "All
return values are integers." could tell the truth once. In a similar way we
Comment thread
skirpichev marked this conversation as resolved.
Outdated
can simplify description of accepted arguments for both the
Comment thread
skirpichev marked this conversation as resolved.
Outdated
:external+py3.14:mod:`math` and the new module.
Comment thread
skirpichev marked this conversation as resolved.

Apparently, the :external+py3.14:mod:`math` can't serve as a catch-all place
for mathematical functions: we have also the :external+py3.14:mod:`cmath` and
Comment thread
skirpichev marked this conversation as resolved.
Outdated
Comment thread
skirpichev marked this conversation as resolved.
Outdated
the :external+py3.14:mod:`statistics`. Let's make same for integer-related
functions. It would provide shared context, which reduces verbosity in the
documentation and conceptual load. It also aids discoverability through
grouping related functions and IDEs suggesting helpful completions.
Comment thread
skirpichev marked this conversation as resolved.
Outdated


Specification
=============

The PEP proposes moving the following integer-related functions [1]_ in a new
module, called ``imath``:

* :external+py3.14:func:`~math.comb`
* :external+py3.14:func:`~math.factorial`
* :external+py3.14:func:`~math.gcd`
* :external+py3.14:func:`~math.isqrt`
* :external+py3.14:func:`~math.lcm`
* :external+py3.14:func:`~math.perm`

Their aliases in :external+py3.14:mod:`math` will be :term:`soft deprecated`.

Modules functions will accept integers and objects that implement the
Comment thread
skirpichev marked this conversation as resolved.
Outdated
:external+py3.14:meth:`~object.__index__` method which is used to convert the
Comment thread
skirpichev marked this conversation as resolved.
Outdated
object to an integer number.

Possible extensions for the new module and it's scope are discussed in the
Comment thread
skirpichev marked this conversation as resolved.
Outdated
`Open Issues <Open Issues_>`_ section. New functions are not part of the
Comment thread
skirpichev marked this conversation as resolved.
Outdated
proposal.


Backwards Compatibility
=======================

As aliases in :external+py3.14:mod:`math` will be kept for indefinite time
Comment thread
skirpichev marked this conversation as resolved.
Outdated
(their use would be discouraged), there are no anticipated code breaks.


Reference Implementation
========================

https://github.com/python/cpython/pull/133909
Comment thread
skirpichev marked this conversation as resolved.
Outdated


Open Issues
===========

Module name
-----------

Chosen name seems consistent with other domain-specific mathematical module:
Comment thread
skirpichev marked this conversation as resolved.
Outdated
:external+py3.14:mod:`cmath` (for complex numbers).
Comment thread
skirpichev marked this conversation as resolved.

There is already an ``imath`` project on PyPI, but only with two releases, with
the most recent one 4 years ago. Its repository is no longer accessible.
Comment thread
skirpichev marked this conversation as resolved.
Outdated
The `Imath <https://github.com/AcademySoftwareFoundation/Imath>`_ C++ library
include Python bindings with same name.
Comment thread
skirpichev marked this conversation as resolved.
Outdated

`Polling shows <https://discuss.python.org/t/91337/35>`_ ``intmath`` as another
Comment thread
skirpichev marked this conversation as resolved.
Outdated
Comment thread
skirpichev marked this conversation as resolved.
Outdated
popular name. The argument made was that the normal mathematical spelling of
the imaginary unit is ``i``, which makes imath ambiguous. It also has no conflict
Comment thread
skirpichev marked this conversation as resolved.
Outdated
with any PyPI module. On the other hand, ``intmath`` may be confused with
interval math or numerical integration.

Other proposed names include ``ntheory`` (like SymPy's submodule),
``integermath`` and ``imaths``.


Module scope and possible extensions
------------------------------------

Unless we can just provide bindings to some well supported mathematical library
like the GMP, the module scope should be limited. For example, no primality
Comment thread
skirpichev marked this conversation as resolved.
testing and factorization.
Comment thread
skirpichev marked this conversation as resolved.
Outdated
Comment thread
skirpichev marked this conversation as resolved.
Outdated

There are possible additions, among proposed in the initial discussion thread
Comment thread
skirpichev marked this conversation as resolved.
Outdated
(see also [5]_):
Comment thread
skirpichev marked this conversation as resolved.
Outdated

* ``c_div()`` --- for integer ceiling divide, see [2]_, [3]_.
* ``gcdext()`` --- to solve linear Diophantine equation in two variables (the
Comment thread
skirpichev marked this conversation as resolved.
Outdated
:external+py3.14:class:`int` implementation actually include extended
Comment thread
skirpichev marked this conversation as resolved.
Outdated
Euclidean algorithm)
* ``isqrt_rem()`` --- to return both integer square root and a remainder (if
Comment thread
skirpichev marked this conversation as resolved.
Outdated
integer isn't a perfect square)
Comment thread
skirpichev marked this conversation as resolved.
Outdated
* ``ilog()`` --- integer logarithm, currently :external+py3.14:func:`math.log`
Comment thread
skirpichev marked this conversation as resolved.
Outdated
has a special handling for integer arguments. It's unique (wrt other module
Comment thread
skirpichev marked this conversation as resolved.
Outdated
functions) and not documented so far, see [4]_
* ``fibonacci()``.

Comment thread
vstinner marked this conversation as resolved.

Rejected ideas
==============

There was a brief discussion about exposing :external+py3.14:func:`math.isqrt`
as ``imath.sqrt`` in the same way that :external+py3.14:func:`cmath.sqrt` is
the complex version of :external+py3.14:func:`math.sqrt`. However, ``isqrt``
is ultimately a different function: it is the floor of the square root. It
would be confusing to give it the same name (under a different module).


Acknowledgements
================

Thanks to Tim Peters for reviving the idea of the :external+py3.14:mod:`math`
splitting. Thanks to Neil Girdhar for substantial improvements of
Comment thread
skirpichev marked this conversation as resolved.
Outdated
the initial draft.


Footnotes
Comment thread
skirpichev marked this conversation as resolved.
Outdated
=========

.. [1] Number-theoretic functions
(https://docs.python.org/3.14/library/math.html#number-theoretic-functions)
.. [2] Integer ceiling divide
(https://discuss.python.org/t/91269)
.. [3] https://gmpy2.readthedocs.io/en/stable/mpz.html#gmpy2.c_div
.. [4] https://github.com/python/cpython/issues/120950
.. [5] https://github.com/python/cpython/issues/81313


Copyright
=========

This document is placed in the public domain or under the
CC0-1.0-Universal license, whichever is more permissive.