Skip to content

Commit 6e47acd

Browse files
gh-81313: Add the imath module
1 parent 13cb8ca commit 6e47acd

File tree

17 files changed

+2054
-1694
lines changed

17 files changed

+2054
-1694
lines changed

Doc/library/imath.rst

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

Doc/library/math.rst

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -124,40 +124,27 @@ noted otherwise, all return values are floats.
124124
Number-theoretic functions
125125
--------------------------
126126

127-
.. function:: comb(n, k)
128-
129-
Return the number of ways to choose *k* items from *n* items without repetition
130-
and without order.
131-
132-
Evaluates to ``n! / (k! * (n - k)!)`` when ``k <= n`` and evaluates
133-
to zero when ``k > n``.
127+
These functions are aliases of corresponding functions in the
128+
:mod:`imath` module.
134129

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

139-
Raises :exc:`TypeError` if either of the arguments are not integers.
140-
Raises :exc:`ValueError` if either of the arguments are negative.
132+
An alias of :func:`imath.comb`.
141133

142134
.. versionadded:: 3.8
143135

144136

145137
.. function:: factorial(n)
146138

147-
Return *n* factorial as an integer. Raises :exc:`ValueError` if *n* is not integral or
148-
is negative.
139+
An alias of :func:`imath.factorial`.
149140

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

153144

154145
.. function:: gcd(*integers)
155146

156-
Return the greatest common divisor of the specified integer arguments.
157-
If any of the arguments is nonzero, then the returned value is the largest
158-
positive integer that is a divisor of all arguments. If all arguments
159-
are zero, then the returned value is ``0``. ``gcd()`` without arguments
160-
returns ``0``.
147+
An alias of :func:`imath.gcd`.
161148

162149
.. versionadded:: 3.5
163150

@@ -168,42 +155,21 @@ Number-theoretic functions
168155

169156
.. function:: isqrt(n)
170157

171-
Return the integer square root of the nonnegative integer *n*. This is the
172-
floor of the exact square root of *n*, or equivalently the greatest integer
173-
*a* such that *a*\ ² |nbsp| ≤ |nbsp| *n*.
174-
175-
For some applications, it may be more convenient to have the least integer
176-
*a* such that *n* |nbsp| ≤ |nbsp| *a*\ ², or in other words the ceiling of
177-
the exact square root of *n*. For positive *n*, this can be computed using
178-
``a = 1 + isqrt(n - 1)``.
158+
An alias of :func:`imath.isqrt`.
179159

180160
.. versionadded:: 3.8
181161

182162

183163
.. function:: lcm(*integers)
184164

185-
Return the least common multiple of the specified integer arguments.
186-
If all arguments are nonzero, then the returned value is the smallest
187-
positive integer that is a multiple of all arguments. If any of the arguments
188-
is zero, then the returned value is ``0``. ``lcm()`` without arguments
189-
returns ``1``.
165+
An alias of :func:`imath.lcm`.
190166

191167
.. versionadded:: 3.9
192168

193169

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

196-
Return the number of ways to choose *k* items from *n* items
197-
without repetition and with order.
198-
199-
Evaluates to ``n! / (n - k)!`` when ``k <= n`` and evaluates
200-
to zero when ``k > n``.
201-
202-
If *k* is not specified or is ``None``, then *k* defaults to *n*
203-
and the function returns ``n!``.
204-
205-
Raises :exc:`TypeError` if either of the arguments are not integers.
206-
Raises :exc:`ValueError` if either of the arguments are negative.
172+
An alias of :func:`imath.perm`.
207173

208174
.. versionadded:: 3.8
209175

@@ -838,6 +804,3 @@ Constants
838804

839805
Module :mod:`cmath`
840806
Complex number versions of many of these functions.
841-
842-
.. |nbsp| unicode:: 0xA0
843-
:trim:

Doc/whatsnew/3.15.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ Other language changes
8383
New modules
8484
===========
8585

86-
* None yet.
86+
imath
87+
-----
88+
89+
This module provides access to the mathematical functions for integer arguments.
90+
(Contributed by Serhiy Storchaka in :gh:`81313`.)
8791

8892

8993
Improved modules

0 commit comments

Comments
 (0)