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
🌐
GitHub
gist.github.com › gyli › 917faad02f9ee8b75d082f7a24640f5a
Division with Ceil and floor in Python without math module · GitHub
Division with Ceil and floor in Python without math module - ceil_and_floor_without_math.py
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.

Discussions

python - How to perform ceiling-division in integer arithmetic? - Stack Overflow
It's basically returning the boxes_needed. 1 box can contain 10 items. So if the items typed by the user is 102 then the code should return 11 boxes. Is there a way to divide that rounds upwards if More on stackoverflow.com
🌐 stackoverflow.com
Integer ceiling divide - Ideas - Discussions on Python.org
I would like python to have integer-ceiling-divide. The usual expression is -(x // -y), with similar semantics to // except rounding up instead of down. It is generally used to answer the question “I have x objects and containers of size y. How many containers do I need?” Because of this, ... More on discuss.python.org
🌐 discuss.python.org
1
May 8, 2025
Ceiling of integer division (Python 2 compatibility)
In the _compute_nccf function, there are two computations which take math.ceil of the result of an integer division. It seems that either the math.ceil function is unnecessary, or the division should be performed on floating point arguments (which in Python 2 is not necessarily the case with ... More on github.com
🌐 github.com
3
October 19, 2019
Why is 11//2 = 5?
I understand // rounds the resulting number to the closest integer. This is untrue. // rounds down in Python. Here's an example. >>> 14 // 5 2 If the numbers are both positive, you can think of it just returning the integer part of the dividend quotient. That is, 14 / 5 is actually 2.8. So once you throw away the decimal part you're left with 2. (It's a little more complicated with negative numbers, though. Not much, but slightly.) More on reddit.com
🌐 r/learnpython
23
7
January 23, 2023
🌐
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
November 16, 2022 - 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
🌐
Python
bugs.python.org › issue43255
Issue 43255: Ceil division with /// operator - Python tracker
February 18, 2021 - This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/87421
🌐
Python.org
discuss.python.org › ideas
Integer ceiling divide - Ideas - Discussions on Python.org
May 8, 2025 - The usual expression is -(x // -y), with similar semantics to // except rounding up instead of down. It is generally used to answer the question “I have x objects and containers of size y. How many containers do I need?” Because of this, ...
Find elsewhere
🌐
AskPython
askpython.com › home › round up numbers in python without math.ceil()
Round Up Numbers in Python Without math.ceil() - AskPython
June 30, 2023 - The math.ceil() function is normally used to perform the ceiling operation on floating type, but we can substitute it using the round() function or manually write a program with math.modf() to do so.
🌐
Shelly Lighting
shellysavonlea.net › home › python ceiling without math
Python Ceiling Without Math | Shelly Lighting
July 23, 2025 - Python Ceiling Rounding Up And Division Datagy · Python 1 Introduction To Class 13 Exercise · Python Rounding Banker S Round Math Ceil Floor Trunc Tutorial You · Python Ceiling · Mathematics Free Full Text Series Of Floor And Ceiling Functions Mdash Part Ii Infinite · Python Libraries Math Scipy Numpy Matplotlib · Make Prediction The Signature For This Function Chegg Com · Math Ceil And Large Numbers Issue 99590 Python Cpython Github ·
🌐
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 - Double slashes, and that is all for expressing math.floor(x / y) simply. What about ceiling? You may start to google but you might not be able to find anything on this immediately from Python’s documentation, since ceiling function does not comes with it’s own operator like floor function.
🌐
GitHub
github.com › pytorch › audio › issues › 381
Ceiling of integer division (Python 2 compatibility) · Issue #381 · pytorch/audio
October 19, 2019 - It seems that either the math.ceil function is unnecessary, or the division should be performed on floating point arguments (which in Python 2 is not necessarily the case with the single-slash (/) division operator.
Author   kostmo
🌐
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 - In this article, we’re going to study different ways to perform ceiling division and also create a custom function for ceiling division. The article is beginner-friendly so no need to worry and let’s jump right into it. Operators are symbols that denote a mathematical operation.
🌐
JanBask Training
janbasktraining.com › community › python-python › is-there-a-ceiling-equivalent-of-operator-in-python
Is there a ceiling equivalent of // operator in Python? | JanBask Training Community
September 26, 2025 - This function rounds a number up ... and apply ceil() on regular division. ... You can combine -(-a // b) trick to simulate ceiling division without importing math....
🌐
Python
bugs.python.org › issue46639
Issue 46639: Ceil division with math.ceildiv - Python tracker
February 4, 2022 - This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/90797
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › find-ceil-ab-without-using-ceil-function
Find ceil of a/b without using ceil() function - GeeksforGeeks
September 20, 2022 - The integer division value is added with the checking value to get the ceiling value.
🌐
Homedutech
homedutech.com › faq › python › ceil-and-floor-equivalent-in-python-3-without-math-module.html
Ceil and floor equivalent in Python 3 without Math module?
In Python 3, you can achieve the ... module: Ceil (Round Up): To round a number up to the nearest integer (ceiling), you can use integer division and add 1 if there's a remainder....
🌐
Replit
replit.com › home › discover › how to round up in python
How to round up in Python
August 8, 2019 - The final negation flips the sign back, effectively rounding the original division up to the next whole number. It’s a concise way to get a ceiling result without importing the math module.
🌐
CodeRivers
coderivers.org › blog › python-ceiling-division
Python Ceiling Division: An In - Depth Exploration - CodeRivers
March 2, 2025 - In this example, first, the division ... and it returns the smallest integer greater than or equal to 2.333, which is 3. You can also implement ceiling division manually without using the math module....