I’ve been using ceildiv recently for the purpose of
I called it ceil_div 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.
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
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.