# Split out integer functions from the math module

**URL:** <https://discuss.python.org/t/split-out-integer-functions-from-the-math-module/91337>\
**Category:** Ideas\
**Created:** [May 9, 2025, 6:54am UTC](https://discuss.python.org/t/split-out-integer-functions-from-the-math-module/91337 "2025-05-09T06:54:07Z")\
**Posts on this page:** 17\
**Page:** 4

<div class="post-metadata">

**Author:** ![NeilGirdhar](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/neilgirdhar/32/5776_2.png) [@NeilGirdhar](https://discuss.python.org/u/NeilGirdhar)\
**Post date:** [May 12, 2025, 7:19pm UTC](https://discuss.python.org/t/split-out-integer-functions-from-the-math-module/91337/62 "2025-05-12T19:19:27Z")

</div>

> [@Wombat](#):
>
> Once users learn that `math.lcm` still works, do you think they will switch to `imath.lcm` out of a need for purity?

For users of linters, yes. Every time I upgrade minimum Python version for a project, I run Ruff to automatically upgrade everything to be most idiomatic.

> [@Wombat](#):
>
> Also, third-party module maintainers have a strong disincentive to switch because their code needs to work across python versions.

Right, the switch happens when you increase the Python minimum version.

> [@Wombat](#):
>
> No one really wants years of,

No one needs try-except like that. The switch doesn’t happen until the minimum Python version guarantees that `imath` has the functions you want.

---

<div class="post-metadata">

**Author:** ![skirpichev](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/skirpichev/32/39377_2.png) [@skirpichev](https://discuss.python.org/u/skirpichev)\
**Post date:** [May 13, 2025, 5:13am UTC](https://discuss.python.org/t/split-out-integer-functions-from-the-math-module/91337/63 "2025-05-13T05:13:57Z")

</div>

> [@tim.one](#):
>
> `prod()` and `sumprod()` work for any numeric types, including complex. They “merely” treat ints and floats specially

Small ints, yes.

My point was, that this “work” isn’t too helpful. Simple benchmark:

```python
prod(i): Mean +- std dev: 389 us +- 24 us
reduce(mul, i, 1): Mean +- std dev: 384 us +- 0 us
prod(f): Mean +- std dev: 3.09 us +- 0.11 us
reduce(mul, f, 1): Mean +- std dev: 8.12 us +- 0.31 us
prod(c): Mean +- std dev: 13.8 us +- 0.0 us
reduce(mul, c, 1): Mean +- std dev: 13.0 us +- 0.1 us
prod(r): Mean +- std dev: 2.25 ms +- 0.01 ms
reduce(mul, r, 1): Mean +- std dev: 2.28 ms +- 0.01 ms
prod(d): Mean +- std dev: 87.0 us +- 1.2 us
reduce(mul, d, 1): Mean +- std dev: 87.6 us +- 1.0 us

```

There is even a speed loss for complex or bigints (which is rather artificIal example). So, really these “generics” do something helpful only for the libm’s-style math module.

> ****
>
> ```py
> import pyperf
> 
> import fractions
> import decimal
> import random
> from operator import mul
> from functools import reduce
> from math import prod
> 
> random.seed(1)
> data = [random.random() for _ in range(100)]
> f = data.copy()
> c = [complex(_) for _ in data]
> r = [fractions.Fraction(_) for _ in data]
> d = [decimal.Decimal(_) for _ in data]
> i = [random.randint(1<<100, 1<<128) for _ in range(100)]
> 
> runner = pyperf.Runner()
> 
> runner.bench_func('prod(i)', prod, i)
> runner.bench_func('reduce(mul, i, 1)', reduce, mul, i, 1)
> 
> runner.bench_func('prod(c)', prod, c)
> runner.bench_func('reduce(mul, c, 1)', reduce, mul, c, 1)
> 
> runner.bench_func('prod(r)', prod, r)
> runner.bench_func('reduce(mul, r, 1)', reduce, mul, r, 1)
> 
> runner.bench_func('prod(d)', prod, d)
> runner.bench_func('reduce(mul, d, 1)', reduce, mul, d, 1)
> 
> ```

---

<div class="post-metadata">

**Author:** ![skirpichev](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/skirpichev/32/39377_2.png) [@skirpichev](https://discuss.python.org/u/skirpichev)\
**Post date:** [May 13, 2025, 7:58am UTC](https://discuss.python.org/t/split-out-integer-functions-from-the-math-module/91337/64 "2025-05-13T07:58:20Z")

</div>

> [@tim.one](#):
>
> BTW, “why weren’t they made methods _of_ `int` instead?” would be a good question nobody has asked

JFR, this it something I was thinking of. Cons of putting this in “Open Issues” of the PEP is that it’s yet another minor point to debate;-) But maybe it worth?

Currently we have both (1) some int’s methods and (2) few functions in the math. For me it seems, that (2) offer more generic API (works for int-like objects) and (1) is for more basic operations. Maybe it’s a good idea to keep (1) being minimal. (E.g. gmpy2’s mpz API-is compatible with the int, but not all functions from (2) are implemented.) But in the end — this distinction is more or less arbitrary.

---

<div class="post-metadata">

**Author:** ![storchaka](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/storchaka/32/217_2.png) [@storchaka](https://discuss.python.org/u/storchaka)\
**Post date:** [May 13, 2025, 8:12am UTC](https://discuss.python.org/t/split-out-integer-functions-from-the-math-module/91337/65 "2025-05-13T08:12:36Z")

</div>

> [@Wombat](#):
>
> So the “littering” has already occurred and cannot be “unlittered”.

It will save us from future littering.

Initially, addition of the `imath` module was proposed when only `factorial()` `gcd()` were in `math`. And `math.factorial()` accepted float, while `imath.factorial()` would only accept integers. Second time it was proposed when `isqrt()`, `comb()` and `prod()` was added im `math`. They could be added in `imath` instead, so no littering. Then `math.lcm()` and `int.bit_count()` was added, they could be in `imath`. The more we postpone this decision, the more the module becomes littered, the more difficult the future transition will be.

Currently, 6 integer specific functions take up a third of the code in the math module. If we add 6 more, it could be half. They literally litter the code from a maintainer’s perspective, not only from a user’s perspective.

---

<div class="post-metadata">

**Author:** ![NeilGirdhar](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/neilgirdhar/32/5776_2.png) [@NeilGirdhar](https://discuss.python.org/u/NeilGirdhar)\
**Post date:** [May 13, 2025, 11:41am UTC](https://discuss.python.org/t/split-out-integer-functions-from-the-math-module/91337/66 "2025-05-13T11:41:25Z")

</div>

It’s not an “open issue” for the PEP since the PEP is recommending against that. It’s _rejected ideas_, at best.

Also, making any of these functions methods of `int` goes against at least some OO textbook principles, namely that:

- the interface of a class should not contain methods that could be implemented using the public interface of the class; and,
- monolithic classes should be avoided.

Python doesn’t always follow this, but it typically only creates methods for things that

- can’t be implemented using the public interface (required), or
- are sufficiently popular that adding an import would be too annoying (e.g., `str.format`).

---

<div class="post-metadata">

**Author:** ![tjreedy](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tjreedy/32/137_2.png) [@tjreedy](https://discuss.python.org/u/tjreedy)\
**Post date:** [May 14, 2025, 7:25am UTC](https://discuss.python.org/t/split-out-integer-functions-from-the-math-module/91337/67 "2025-05-14T07:25:02Z")

</div>

> [@storchaka](#):
>
> Currently, 6 integer specific functions take up a third of the code in the math module. If we add 6 more, it could be half. They literally litter the code from a maintainer’s perspective, not only from a user’s perspective.

As a maintainer, though not of math.c, this convinces me.

---

<div class="post-metadata">

**Author:** ![tim.one](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tim.one/32/282_2.png) [@tim.one](https://discuss.python.org/u/tim.one)\
**Post date:** [May 14, 2025, 4:50pm UTC](https://discuss.python.org/t/split-out-integer-functions-from-the-math-module/91337/68 "2025-05-14T16:50:08Z")

</div>

> [@skirpichev](#):
>
> > [@tim.one](#):
> >
> > BTW, “why weren’t they made methods _of_ `int` instead?” would be a good question nobody has asked
> 
> JFR, this it something I was thinking of. Cons of putting this in “Open Issues” of the PEP is that it’s yet another minor point to debate;-) But maybe it worth?

I don’t think so. I’m the only one who mentioned it, and added that there “are reasons” for making these things functions instead. There really hasn’t been any argument.

Indeed, if we _had_ had an `imath` module all along, I expect `int.bit_length()` and `int.bit_count()` would have ended up there instead.

---

<div class="post-metadata">

**Author:** ![tjreedy](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tjreedy/32/137_2.png) [@tjreedy](https://discuss.python.org/u/tjreedy)\
**Post date:** [May 14, 2025, 9:22pm UTC](https://discuss.python.org/t/split-out-integer-functions-from-the-math-module/91337/69 "2025-05-14T21:22:50Z")

</div>

Literal numbers and methods do not play well together. `37.bit_count()` raises “SyntaxError: invalid decimal literal”. One must either insert a space, `37 .bit_count()`, or wrap with parentheses, (37).bit\_count(), or call the function directly, `int.bit_count(37)`. Same would be true of calling methods on floats. [edit: not true]

---

<div class="post-metadata">

**Author:** ![Stefan2](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stefan2/32/18492_2.png) [@Stefan2](https://discuss.python.org/u/Stefan2)\
**Post date:** [May 14, 2025, 9:29pm UTC](https://discuss.python.org/t/split-out-integer-functions-from-the-math-module/91337/70 "2025-05-14T21:29:48Z")

</div>

> [@tjreedy](#):
>
> Same would be true of calling methods on floats.

Can you clarify that? For example `1.5.as_integer_ratio()` and  
`1..as_integer_ratio()` and `1e2.as_integer_ratio()` all work fine.

---

<div class="post-metadata">

**Author:** ![tim.one](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tim.one/32/282_2.png) [@tim.one](https://discuss.python.org/u/tim.one)\
**Post date:** [May 14, 2025, 9:41pm UTC](https://discuss.python.org/t/split-out-integer-functions-from-the-math-module/91337/71 "2025-05-14T21:41:11Z")

</div>

> [@Stefan2](#):
>
> Can you clarify that? For example `1.5.as_integer_ratio()` and  
> `1..as_integer_ratio()` and `1e2.as_integer_ratio()` all work fine.

It’s still fiddly. I think most people don’t realize that negative numeric literals don’t exist in Python’s grammar. So, e.g.,

```python
>>> 1..as_integer_ratio()
(1, 1)
>>> -1..as_integer_ratio()
Traceback (most recent call last):
    ...
TypeError: bad operand type for unary -: 'tuple'

```

Putting a space between the periods doesn’t help with that either - parens are needed to force the intended order. Same for ints:

```python
>>> -1 .bit_count()
-1
>>> (-1) .bit_count()
1

```

Maybe that’s one for @Rosuav’s next Python quiz 😉.

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [May 14, 2025, 9:50pm UTC](https://discuss.python.org/t/split-out-integer-functions-from-the-math-module/91337/72 "2025-05-14T21:50:59Z")

</div>

> [@tim.one](#):
>
> Maybe that’s one for @Rosuav’s next Python quiz 😉.

Dangit, I just made one 🤣 That would actually have been a good one to include.

---

<div class="post-metadata">

**Author:** ![tjreedy](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tjreedy/32/137_2.png) [@tjreedy](https://discuss.python.org/u/tjreedy)\
**Post date:** [May 15, 2025, 1:09am UTC](https://discuss.python.org/t/split-out-integer-functions-from-the-math-module/91337/73 "2025-05-15T01:09:54Z")

</div>

You are right. Added error note. Not sure is this changed or I just mis-remembered.

---

<div class="post-metadata">

**Author:** ![guido](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/guido/32/21_2.png) [@guido](https://discuss.python.org/u/guido)\
**Post date:** [May 19, 2025, 7:02pm UTC](https://discuss.python.org/t/split-out-integer-functions-from-the-math-module/91337/74 "2025-05-19T19:02:35Z")

</div>

> [@skirpichev](#):
>
> BTW, maybe this proposal does require a PEP?

> [@tim.one](#):
>
> I think it does, yes.

It is now a rule that new (top-level) modules (and new builtins) require a PEP, see [PEP 2 – Procedure for Adding New Modules | peps.python.org](https://peps.python.org/pep-0002/)

---

<div class="post-metadata">

**Author:** ![PeterL](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/peterl/32/2076_2.png) [@PeterL](https://discuss.python.org/u/PeterL)\
**Post date:** [May 19, 2025, 11:08pm UTC](https://discuss.python.org/t/split-out-integer-functions-from-the-math-module/91337/75 "2025-05-19T23:08:35Z")

</div>

Sorry, late to the discussion. As an instructor, I disagree that "It doesn’t matter if beginners look there”. It’s very important that beginners get a language that reflects their knowledge of math. High school doesn’t (generally) teach ints versus floats. It’s only us computer people that worry about those.  
To beginners, number / number gives you an answer - knowing about ints and floats at that stage muddies the waters. Python 3 made a big shift when real division was introduced, and helps beginners enormously.

---

<div class="post-metadata">

**Author:** ![skirpichev](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/skirpichev/32/39377_2.png) [@skirpichev](https://discuss.python.org/u/skirpichev)\
**Post date:** [May 20, 2025, 5:18am UTC](https://discuss.python.org/t/split-out-integer-functions-from-the-math-module/91337/76 "2025-05-20T05:18:46Z")

</div>

> [@PeterL](#):
>
> Sorry, late to the discussion.

I think you should go to the new PEP thread (I did update of the description).

> [@PeterL](#):
>
> High school doesn’t (generally) teach ints versus floats.

Is that true? I think that at least integers, rationals and fixed-point arithmetic (decimal, of course) — parts of basic education in most countries.

> [@PeterL](#):
>
> To beginners, number / number gives you an answer - knowing about ints and floats at that stage muddies the waters.

This “answer” below should be meaningless for beginners, assuming declared above level of knowledge (no floats):

```pycon
>>> 2/200000
1e-05

```

c.f. `1/100000`.

---

<div class="post-metadata">

**Author:** ![hugovk](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hugovk/32/14505_2.png) [@hugovk](https://discuss.python.org/u/hugovk)\
**Post date:** [May 20, 2025, 9:10am UTC](https://discuss.python.org/t/split-out-integer-functions-from-the-math-module/91337/77 "2025-05-20T09:10:19Z")

</div>

> [@skirpichev](#):
>
> I think you should go to the new PEP thread (I did update of the description).

👍

[@moderators](https://discuss.python.org/groups/moderators) Please lock this thread so we continue discussion in the new thread: [PEP 791: imath --- module for integer-specific mathematics functions](https://discuss.python.org/t/pep-791-imath-module-for-integer-specific-mathematics-functions/92548)

---

<div class="post-metadata">

**Author:** ![davidism](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/davidism/32/3295_2.png) [@davidism](https://discuss.python.org/u/davidism)\
**Post date:** [May 20, 2025, 9:45pm UTC](https://discuss.python.org/t/split-out-integer-functions-from-the-math-module/91337/78 "2025-05-20T21:45:34Z")

</div>

Closed on May 20, 2025, 9:45pm UTC



[Previous page](https://discuss.python.org/t/split-out-integer-functions-from-the-math-module/91337.md?page=3)
