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.

Answer from dlitz on Stack Overflow
Top answer
1 of 9
490

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.

2 of 9
83

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.

🌐
GeeksforGeeks
geeksforgeeks.org › python › floor-ceil-function-python
floor() and ceil() function Python - GeeksforGeeks
ceil(): Rounds a number up to the nearest integer. Example: ceil() of 3.3 will be 4. Note: Both functions require importing the math module: import math ... Both return an integer.
Published   January 10, 2018
🌐
Mathspp
mathspp.com › blog › til › 001
TIL #001 – ceiling division in Python | mathspp
This operator is equivalent to doing regular division and then flooring down: >>> # How many years in 10_000 days? >>> from math import floor; floor(10_000 / 365) 27 >>> 10_000 // 365 27 · Then, someone asked if Python also had a built-in for ceiling division, that is, an operator that divided the operands and then rounded up.
🌐
PKH Me
blog.pkh.me › p › 36-figuring-out-round,-floor-and-ceil-with-integer-division.html
Figuring out round, floor and ceil with integer division
In Python 2 and 3, the integer division is toward -∞, which means it is directly equivalent to how the floor() function behaves. In C, the integer division is equivalent to floor() only for positive numbers, otherwise it behaves the same as ceil().
🌐
Medium
medium.com › @kevingxyz › the-art-of-pythons-ceiling-and-floor-notation-684d4d354e1e
The Art of Python’s Ceiling and Floor using Operator | by Kevin | Medium
August 8, 2020 - You probably have used ceiling and floor mathematical functions in Python. It is just a simple math.ceil(x / y) and math.floor(x / y) respectively, when performing a division, right?
🌐
Analytics Vidhya
analyticsvidhya.com › home › understanding floor and ceiling functions in python
Floor and Ceiling Functions in Python | Applications and Behaviour
June 20, 2023 - In programming, the ceil function finds application in scenarios such as rounding up division results, handling screen pixel dimensions, or aligning elements within a grid system. Must Read: Data Analysis Project for Beginners Using Python · Gain insights into the behavior of the floor function, including its handling of positive and negative numbers, fractions, and special cases.
🌐
Enterprise DNA
blog.enterprisedna.co › python-ceiling-division
Python Ceiling Division: Quick User Guide – Master Data Skills + AI
To perform ceiling division in Python, use the math library’s ceil() function. The function rounds the number up to the smallest integer greater than or equal to it. You can also perform ceiling division by using the floor division operator and double negation to achieve your desired result.
Find elsewhere
🌐
LearnDataSci
learndatasci.com › solutions › python-double-slash-operator-floor-division
Python Double Slash (//) Operator: Floor Division – LearnDataSci
The following shows how we can ... when the input is negative. Alternatively to math.floor(), we can also use math.ceil(), which will always round up to the nearest whole number instead of down....
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
In Python 3 without the Math package, what is the equivalent of ceiling and floor? - Raspberry Pi Forums
We use optional cookies, as detailed in our cookie policy, to remember your settings and understand how you use our website. ... The // operator does floor division for integers. ... def ceil_floor(x): rest = x % 1 floor = int(x // 1) ceil = floor + 1 if rest else 0 return ceil, floor
🌐
W3Schools
w3schools.com › python › ref_math_ceil.asp
Python math.ceil() Method
# Import math library import math ... print(math.ceil(10.0)) Try it Yourself » · The math.ceil() method rounds a number UP to the nearest integer, if necessary, and returns the result....
🌐
Python.org
discuss.python.org › python help
Integer division - Python Help - Discussions on Python.org
January 25, 2023 - As far as I could tell with a quick search IEEE 754 doesn’t define “floor division” – I wonder what it would specify if it did? A fully compliant 754 implementation needs to support the 4 distinct rounding modes defined by that standard. “To minus infinity” rounding is what it calls what we’re calling “floor” here. Its “to plus infinity” rounding is what we call “ceiling”. But core Python does not support any way to get at your hardware’s rounding mode setting. If it did, and you asked for “to minus infinity” rounding, your hardware division would also return a result less than 15.0 for 6 / 0.4.
🌐
datagy
datagy.io › home › python posts › python ceiling: rounding up (and python ceiling division)
Python Ceiling: Rounding Up (and Python Ceiling Division) • datagy
April 13, 2023 - One of these operators is the Python floor division operator, //. When this operator is used, the floored integer value of the two values is returned. One interesting piece about Python is that is there no built-in way to easily compare a ceiling ...
🌐
AskPython
askpython.com › home › is there a ceiling equivalent of // operator in python?
Is There a Ceiling Equivalent of // Operator in Python? - AskPython
May 25, 2023 - If you’re a beginner in programming ... using Python, I’m pretty sure you must have encountered the ‘//’ operator or floor division operator. The // operator is used to divide an integer and then round off its value to its floor value. Have ...
🌐
Javatpoint
javatpoint.com › floor-and-ceil-functions-in-python
floor() and ceil() Functions in Python - Javatpoint
floor() and ceil() Functions in Python with python, tutorial, tkinter, button, overview, entry, checkbutton, canvas, frame, environment set-up, first python program, operators, etc.
🌐
Delft Stack
delftstack.com › home › howto › python › ceiling division python
Ceiling Division in Python | Delft Stack
March 11, 2025 - In Python, there are several ways to perform ceiling division. The most common methods include using the math.ceil() function, the // operator combined with conditional logic, and leveraging the numpy library.
🌐
Hacker News
news.ycombinator.com › item
Figuring out round, floor and ceil with integer division | Hacker News
November 26, 2022 - This is indeed clearly documented [1], I guess I never looked closely enough. I found some discussion on the dev-python list [2] which shows at least I'm not the only one surprised by this · That… is exactly the behaviour python implements, and what GP was surprised by…
🌐
Board Infinity
boardinfinity.com › blog › what-is-ceil-function-in-python
What is ceil() function in Python? | Board Infinity
June 22, 2023 - In our definition, we specify that our function can accept two arguments: a numerator and a denominator. Finally, we return the negative value obtained by dividing the denominator by the negative numerator floor.
🌐
Career Karma
careerkarma.com › blog › python › python ceil and floor: a step-by-step guide
Python Ceil and Floor: A Step-By-Step Guide | Career Karma
December 1, 2023 - The Python ceil() function rounds a number up to the nearest integer, or whole number. Python floor() rounds decimals down to the nearest whole number.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.ceil.html
numpy.ceil — NumPy v2.2 Manual
Return the ceiling of the input, element-wise · The ceil of the scalar x is the smallest integer i, such that i >= x. It is often denoted as \(\lceil x \rceil\)