-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
serhiy-storchaka
wants to merge
15
commits into
python:main
Choose a base branch
from
serhiy-storchaka:imath3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6e47acd
gh-81313: Add the imath module
serhiy-storchaka 62cb235
Fix Sphinx warning.
serhiy-storchaka 091dab6
Support multiple interpreters.
serhiy-storchaka 71d7f4f
_PyLong_NumBits returns int64_t now.
serhiy-storchaka 7b61c5e
Fix build on Windows.
serhiy-storchaka eaea554
Add in a toctree.
serhiy-storchaka da3ceab
Regen generated files.
serhiy-storchaka 2ab90a5
Merge branch 'main' into imath3
serhiy-storchaka 3299bf7
imath -> intmath
serhiy-storchaka 9b6a151
Remove ilog2().
serhiy-storchaka 7b327ec
Remove unneeded import.
serhiy-storchaka 3194384
Apply suggestions from code review
serhiy-storchaka 59b1be1
Apply suggestions from code review
serhiy-storchaka 8137058
Update clinic.
serhiy-storchaka ee0dbb6
make regen-stdlib-module-names
serhiy-storchaka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
:mod:`imath` --- Mathematical functions for integer numbers | ||
=========================================================== | ||
|
||
.. module:: imath | ||
:synopsis: Mathematical functions for integer numbers. | ||
|
||
.. versionadded:: next | ||
|
||
-------------- | ||
|
||
This module provides access to the mathematical functions 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. | ||
|
||
The following functions are provided by this module. All return values 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:`TypeError` if either of the arguments are not integers. | ||
Raises :exc:`ValueError` if either of the arguments are negative. | ||
|
||
|
||
.. function:: factorial(n) | ||
|
||
Return *n* factorial as an integer. Raises :exc:`ValueError` if *n* is not integral or | ||
is negative. | ||
|
||
|
||
.. function:: gcd(a, b) | ||
|
||
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:: ilog2(n) | ||
|
||
Return the integer base 2 logarithm of the positive integer *n*. This is the | ||
floor of the exact base 2 logarithm root of *n*, or equivalently the | ||
greatest integer *k* such that | ||
2\ :sup:`k` |nbsp| ≤ |nbsp| *n* |nbsp| < |nbsp| 2\ :sup:`k+1`. | ||
|
||
It is equivalent to ``n.bit_length() - 1`` for positive *n*. | ||
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
.. 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:`TypeError` if either of the arguments are not integers. | ||
Raises :exc:`ValueError` if either of the arguments are negative. | ||
|
||
|
||
.. |nbsp| unicode:: 0xA0 | ||
:trim: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -83,7 +83,11 @@ Other language changes | |||||||||||
New modules | ||||||||||||
=========== | ||||||||||||
|
||||||||||||
* None yet. | ||||||||||||
imath | ||||||||||||
----- | ||||||||||||
|
||||||||||||
This module provides access to the mathematical functions for integer arguments. | ||||||||||||
(Contributed by Serhiy Storchaka in :gh:`81313`.) | ||||||||||||
Comment on lines
+89
to
+90
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
|
||||||||||||
Improved modules | ||||||||||||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the same way we could simplify now description of argument processing in the math module. Except for a special snowflake "log" - all use PyFloat_AsDouble(). (Well, few functions also try special dunder methods.)
Though, probably it's for following prs.