PEP 791: imath --- module for integer-specific mathematics functions

When this module was first proposed 7 years ago, there were only two integer-related functions in math: factorial() and gcd(). It would be much easier to add it at that time. Since then, 4 new functions were added, factorial() stopped supporting floats, gcd() was extended. Now, implementation of these 6 functions takes 1/3 of the math module volume.

Several functions that I planned to add to the new module have not yet been added to math, partially because it is difficult to support them intermixed with floating point functions in one file:

  • ilog() – determines the number of digits of a number in a given number system.
  • as_integer_ratio() – represents a number as an integer ratio.
  • Integer division with rounding up and to the nearest integer. Useful for datetime and fractions.
  • isprime() predicate and primes() generator.
  • Variants of isqrt() and ilog() with rounding up.

The more we delay adding a new module, the higher the price.

18 Likes