# Integer ceiling divide

**URL:** <https://discuss.python.org/t/integer-ceiling-divide/91269>\
**Category:** Ideas\
**Created:** [May 8, 2025, 5:23pm UTC](https://discuss.python.org/t/integer-ceiling-divide/91269 "2025-05-08T17:23:39Z")\
**Posts on this page:** 1\
**Showing post:** 27

<div class="post-metadata">

**Author:** ![jb2170](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jb2170/32/24298_2.png) [@jb2170](https://discuss.python.org/u/jb2170)\
**Post date:** [June 1, 2026, 2:03am UTC](https://discuss.python.org/t/integer-ceiling-divide/91269/27 "2026-06-01T02:03:25Z")

</div>

I’ve been using `ceildiv` recently for the purpose of

> [@madeleineth](#):
>
> “I have `x` objects and containers of size `y`. How many containers do I need?”

I called it [`ceil_div`](https://github.com/UTF-8000/UTF-8000-Python/blob/c67ed64a0676988e7d59f2a7d17ac1d9e59a1edc/src/UTF8000/UTF8000Byte.py#L56) because I’m overly verbose in my naming sometimes 😅 . I’m speciously reading the `c` in `cdiv` or `c_div` like the C programming language, as though it has only finite precision compared to Python’s infinite precision. I think `ceildiv` would be the best name, located at `math.integer.ceildiv`.

As for the implementation I must admit I googled it for the sake of getting a computationally efficient one.

```py
def ceil_div(x: int, y: int) -> int:
    return -(x // -y)

```

is what I’m using but I haven’t even considered how it should be implemented for negative numbers. So to reply to

> [@ncoghlan](#):
>
> I don’t have a strong opinion on whether `ceildiv` is sufficiently common to be worth adding to the standard library

I do think the use case is common enough for `x>=0` and `y>=1`, and makes things less error prone for the end user if the implementation is taken care of by the stdlib.

It’s just the behavior for negative `x` or `y` that I’d have to see different real-use cases / implementations for. Like we can’t just think about `x = qy + r` with `0 <= r < b` Euclid’s Algorithm style when `y < 0`; we usually make `x` and `y` positive and find the gcd of those.

---

_[View the full topic](https://discuss.python.org/t/integer-ceiling-divide/91269)._
