No, but you can use upside-down floor division:ยน
def ceildiv(a, b):
return -(a // -b)
This works because Python's division operator does floor division (unlike in C, where integer division truncates the fractional part).
Here's a demonstration:
>>> from __future__ import division # for Python 2.x compatibility
>>> import math
>>> def ceildiv(a, b):
... return -(a // -b)
...
>>> b = 3
>>> for a in range(-7, 8):
... q1 = math.ceil(a / b) # a/b is float division
... q2 = ceildiv(a, b)
... print("%2d/%d %2d %2d" % (a, b, q1, q2))
...
-7/3 -2 -2
-6/3 -2 -2
-5/3 -1 -1
-4/3 -1 -1
-3/3 -1 -1
-2/3 0 0
-1/3 0 0
0/3 0 0
1/3 1 1
2/3 1 1
3/3 1 1
4/3 2 2
5/3 2 2
6/3 2 2
7/3 3 3
Why this instead of math.ceil?
math.ceil(a / b) can quietly produce incorrect results, because it introduces floating-point error. For example:
>>> from __future__ import division # Python 2.x compat
>>> import math
>>> def ceildiv(a, b):
... return -(a // -b)
...
>>> x = 2**64
>>> y = 2**48
>>> ceildiv(x, y)
65536
>>> ceildiv(x + 1, y)
65537 # Correct
>>> math.ceil(x / y)
65536
>>> math.ceil((x + 1) / y)
65536 # Incorrect!
In general, it's considered good practice to avoid floating-point arithmetic altogether unless you specifically need it. Floating-point math has several tricky edge cases, which tends to introduce bugs if you're not paying close attention. It can also be computationally expensive on small/low-power devices that do not have a hardware FPU.
ยนIn a previous version of this answer, ceildiv was implemented as return -(-a // b) but it was changed to return -(a // -b) after commenters reported that the latter performs slightly better in benchmarks. That makes sense, because the dividend (a) is typically larger than the divisor (b). Since Python uses arbitrary-precision arithmetic to perform these calculations, computing the unary negation -a would almost always involve equal-or-more work than computing -b.
Solution 1: Convert floor to ceiling with negation
def ceiling_division(n, d):
return -(n // -d)
Reminiscent of the Penn & Teller levitation trick, this "turns the world upside down (with negation), uses plain floor division (where the ceiling and floor have been swapped), and then turns the world right-side up (with negation again)"
Solution 2: Let divmod() do the work
def ceiling_division(n, d):
q, r = divmod(n, d)
return q + bool(r)
The divmod() function gives (a // b, a % b) for integers (this may be less reliable with floats due to round-off error). The step with bool(r) adds one to the quotient whenever there is a non-zero remainder.
Solution 3: Adjust the numerator before the division
def ceiling_division(n, d):
return (n + d - 1) // d
Translate the numerator upwards so that floor division rounds down to the intended ceiling. Note, this only works for integers.
Solution 4: Convert to floats to use math.ceil()
def ceiling_division(n, d):
return math.ceil(n / d)
The math.ceil() code is easy to understand, but it converts from ints to floats and back. This isn't very fast and it may have rounding issues. Also, it relies on Python 3 semantics where "true division" produces a float and where the ceil() function returns an integer.
Videos
For your use case, use integer arithmetic. There is a simple technique for converting integer floor division into ceiling division:
items = 102
boxsize = 10
num_boxes = (items + boxsize - 1) // boxsize
Alternatively, use negation to convert floor division to ceiling division:
num_boxes = -(items // -boxsize)
Negate before and after?
>>> -(-102 // 10)
11
Iโll prove the more general result. Let and
be positive integers and
a real number. Let
. Then
and hence
We want to show that
Clearly , so suppose to get a contradiction that
. Then there must be an integer
such that
and hence . But then
, which is absurd.
For the case of floor division (ceiling division is analogous), we can get the result directly using the uniqueness of real Euclidean division:
For divided by
, the result can be written uniquely as
in
, where quotient
and remainder
satisfies
.
Let and
be real numbers (
doesn't have to be integer) and
be a positive integer.
Let , so that
Let , so that
Since and
are integers,
is as well, so we have a stronger bound
.
Then
From our remainder bounds, and
, so together
, that is
is the remainder of
and
is the quotient. Conclude
.
Using floor and ceiling bounds is more flexible, as shown in Concrete Mathematics.
Let
be any continuous, monotonously increasing function with the property that
integer implies
integer. Then
Not immediately obvious is that the nested floor and ceiling identities are a special case of function evaluated at
.
I'm trying to write a function that (kind of) emulates the ceiling function in the math library, in a way that is more useful for my purposes. The code I've written is not working as expected. It should take two integer arguments, divide one by the other, and round up to the nearest integer no matter what the result is.
Ex: 2/2=1 --> answer is 1; 10/3=3.333... --> round up to 4.
This is my code:
int ceiling(int number, int value){
float a = number/value;
int b = number/value;
float c = a - b;
if(c!=0){
b=b+1;
}
return b;
}Using the examples above,
ceiling(2,2); would return 1, as expected.
ceiling(10,3); returns 3, when it should return 4.
My thought process is that if a is a float, then 10/3 should be 3.33333..., and if b is an int, then 10/3 should be 3. If c is a float, a-b should be 0.33333..., then since c!=0, ceiling should increment b and return that value. Where am I going wrong?



