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
🌐
Purple Engineer
purpletutor.com › home › understanding code › python floor division guide for precise integer results
Floor division python guide with examples for integer division 🚀🔍
December 24, 2025 - The math.ceil() function provides the opposite behavior of floor operations, rounding up to the nearest integer. While not directly related to floor division, it's often used in complementary scenarios where you need ceiling rather than floor ...
People also ask

When should I use floor division instead of regular division in Python?
Use floor division when you need an integer result that rounds down, such as in loop iterations or when dealing with discrete quantities like array indices. It's preferable over regular division to avoid floating-point precision issues in integer math. For example, in scenarios like calculating grid positions, floor division ensures accurate, whole-number outcomes.
🌐
purpletutor.com
purpletutor.com › home › understanding code › python floor division guide for precise integer results
Floor division python guide with examples for integer division ...
What is the floor division of 5 and 2?
The floor division of 5 and 2 using the // operator in Python returns 2, as it divides 5 by 2 (resulting in 2.5) and floors it to the nearest lower integer. This operation discards the fractional part entirely. It's a quick way to get the integer quotient without needing additional functions.
🌐
purpletutor.com
purpletutor.com › home › understanding code › python floor division guide for precise integer results
Floor division python guide with examples for integer division ...
How does floor division work with negative numbers?
Floor division with negative numbers in Python rounds the result down toward negative infinity. For instance, -5 // 2 returns -3 because -2.5 is floored to -3, not -2. This behavior ensures consistency in mathematical operations involving negatives.
🌐
purpletutor.com
purpletutor.com › home › understanding code › python floor division guide for precise integer results
Floor division python guide with examples for integer division ...
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.

🌐
Mathspp
mathspp.com › blog › til › 001
TIL #001 – ceiling division in Python | mathspp
By using -a in the division, it's as if you flip a upside down, so “its ceiling is now on the floor”, so you can use -a // b. Then, you just need to put everything back in place, using a final negation: -(-a // b). At first, I thought this ...
🌐
The Teclado Blog
blog.teclado.com › rounding-in-python
Rounding in Python
October 26, 2022 - The important takeaway here, is that floor rounds towards zero for positive numbers, and away from zero for negative numbers. The ceil function is the opposite of floor.
🌐
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
Python is following the round toward even choice rule. This is not what we are implementing here (Edit: a partial implementation is provided at the end though). There are many ways of rounding, so make sure you've clarified what method your language picked. The integer division is symmetrical around 0 but ceil and floor aren't, so we need a way get the sign in order to branch in one direction or another.
🌐
Noble Desktop
nobledesktop.com › division in python
Division in Python - Free Video Tutorial and Guide
September 15, 2023 - The opposite of floor division is modulus. Modulus (or percentage sign) will give you the remainder. For example, if you feed three into five, you will get a remainder of two. Modulus is useful when you need to get the remainder. Python also has the function divmod which returns two values ...
Find elsewhere
🌐
Python.org
discuss.python.org › python help
Integer division - Python Help - Discussions on Python.org
January 25, 2023 - But Python’s floor division doesn’t care about that - it’s rounding down to the nearest integer in the direction of minus infinity. Hardware isn’t doing it - CPython is working hard to give you the true floor.
🌐
Python
peps.python.org › pep-0238
PEP 238 – Changing the Division Operator | peps.python.org
For all other operators, one can ... arguments happen to have an integral type, it implements floor division rather than true division....
🌐
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 will get -4, since it applies the floor function to a negative number, essentially rounding it down more negatively. In Python, the floor function rounds the value towards negative infinity.
🌐
The Teclado Blog
blog.teclado.com › pythons-modulo-operator-and-floor-division
Python's modulo operator and floor division
October 26, 2022 - The amount left over after the division is called the remainder, which in this case is 1. To give you another example, 11 / 3 gives us a quotient of 3 and a remainder of 2. In Python, the modulo operator simply yields the remainder: ... Floor ...
🌐
W3Schools
w3schools.com › python › python_operators_arithmetic.asp
Python Arithmetic Operators
Python has two division operators: / - Division (returns a float) // - Floor division (returns an integer) Division always returns a float: x = 12 y = 5 print(x / y) Try it Yourself » · Floor division always returns an integer. It rounds DOWN to the nearest integer: x = 12 y = 5 print(x // y) Try it Yourself » ·
🌐
Wikibooks
en.wikibooks.org › wiki › Python_Programming › Operators
Python Programming/Operators - Wikibooks, open books for an open world
May 4, 2004 - Thus, 5 / 2 == 2 and -3 / 2 == -2. Using "/" to do division this way is deprecated; if you want floor division, use "//" (available in Python 2.2 and later). Dividing by or into a floating point number will cause Python to use true division. Thus, to ensure true division in Python 2.x: x=3; y=2; float(x)/y == 1.5. ... The modulus (remainder of the division of the two operands, rather than the quotient) can be found using the % operator, or by the divmod builtin function.
🌐
Quora
quora.com › What-is-the-difference-between-modulus-and-floor-division-in-Python
What is the difference between modulus and floor division in Python? - Quora
Answer (1 of 3): Modulus division returns the remainder of whatever two numbers are being divided. For example, [code]>>> 18 % 4 2 [/code]4 goes into 18 four times with a remainder of 2 Floor division returns the “floor” of the result, meaning ...
🌐
CodeGym
codegym.cc › java blog › learning python › floor division in python
Floor Division in Python
November 11, 2024 - While // is the easiest way to perform floor division, Python offers some alternatives that can achieve similar results.
🌐
GeeksforGeeks
geeksforgeeks.org › python › floor-ceil-function-python
floor() and ceil() function Python - GeeksforGeeks
Python’s math module provides many useful mathematical functions, including floor() and ceil(), which are commonly used for rounding numbers. floor(): Rounds a number down to the nearest integer. Example: floor() of 3.3 will be 3.
Published   January 10, 2018