functions of a real returning respectively the largest smaller and the smallest larger integer
{\displaystyle \lfloor x\rfloor =x-\{x\}}
{\displaystyle \lfloor x\rfloor =m}
{\displaystyle \lfloor x\rfloor }
{\displaystyle \lfloor x\rfloor \leq \lceil x\rceil ,}
In mathematics, the floor function is the function that takes as input a real number x, and gives as output the greatest integer less than or equal to x, denoted โŒŠxโŒ‹ or โ€ฆ Wikipedia
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Floor_and_ceiling_functions
Floor and ceiling functions - Wikipedia
February 5, 2026 - {\displaystyle \left\lfloor {\tfrac {x}{2^{n}}}\right\rfloor } . Division by a power of 2 is often written as a right-shift, not for optimization as might be assumed, but because the floor of negative results is required. Assuming such shifts are "premature optimization" and replacing them ...
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
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.
๐ŸŒ
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 problem can be solved using the ceiling function, but the ceiling function does not work when integers are passed as parameters. Hence there are the following 2 approaches below to find the ceiling value. ... a/b returns the integer division value, and ((a % b) != 0) is a checking condition which returns 1 if we have any remainder left after the division of a/b, else it returns 0.
๐ŸŒ
Scribd
scribd.com โ€บ document โ€บ 894553707 โ€บ Ceil-Division
Ceiling Division Techniques in Python | PDF
The trick involves negating the numerator and denominator to round up, or using divmod() to adjust the quotient based on the remainder. There is no dedicated ceiling division operator in Python, making these methods essential for efficient ...
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ floor-and-ceil-of-integer-division
Floor and Ceil of Integer Division - GeeksforGeeks
July 14, 2025 - Given two integers a and b (where ... / b. โ€ข The ceil of the division a / b, denoted as โŒˆa / bโŒ‰ โ€” the smallest integer greater than or equal to a / b....
๐ŸŒ
DEV Community
dev.to โ€บ kiani0x01 โ€บ python-ceiling-division-4087
Python Ceiling Division - DEV Community
July 27, 2025 - Ceiling division isnโ€™t just a trickโ€”itโ€™s a pattern. Use it when you must ensure full coverage: you need enough slots, pages, or resources.
๐ŸŒ
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 - Basically, instead of the ceiling dividing 10 by 3, we floor divided -10 by 3 and then multiplied it by a minus. Floor division of -10 by 3 will be -4 and -(-4) will be 4. This is our desired output for the ceiling division.
๐ŸŒ
Board Infinity
boardinfinity.com โ€บ blog โ€บ what-is-ceil-function-in-python
What is ceil() function in Python? | Board Infinity
June 22, 2023 - The floor division operator in Python is one of them, and its symbol is /. The floored integer value derived from the two values is what is returned when this operator is used. Python's lack of an easy-to-use built-in feature to compare ceiling divisions, which results in the return of the ceiling of the quotient of the two values, is an intriguing feature.
๐ŸŒ
Python.org
discuss.python.org โ€บ ideas
Integer ceiling divide - Ideas - Discussions on Python.org
May 8, 2025 - 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, it would be somewhat less error-prone if it were ArithmeticError for x to be anything but a nonnegative integer, or y to be anything but a positive integer.
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ api โ€บ system.math.ceiling
Math.Ceiling Method (System) | Microsoft Learn
Dim values() As Decimal = {7.03d, 7.64d, 0.12d, -0.12d, -7.1d, -7.6d} Console.WriteLine(" Value Ceiling Floor") Console.WriteLine() For Each value As Decimal In values Console.WriteLine("{0,7} {1,16} {2,14}", _ value, Math.Ceiling(value), Math.Floor(value)) Next ' The example displays the following output to the console: ' Value Ceiling Floor ' ' 7.03 8 7 ' 7.64 8 7 ' 0.12 1 0 ' -0.12 0 -1 ' -7.1 -7 -8 ' -7.6 -7 -8 ยท The behavior of this method follows IEEE Standard 754, section 4. This kind of rounding is sometimes called rounding toward positive infinity.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ writing a ceiling function in c
r/learnprogramming on Reddit: Writing a ceiling function in C
September 18, 2013 -

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?

๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ python โ€บ ceiling division python
Ceiling Division in Python | Delft Stack
March 11, 2025 - Ceiling division is the process of dividing two numbers and rounding up the result to the nearest integer. For instance, if you divide 5 by 2, the result is 2.5. With ceiling division, you would round this up to 3.
๐ŸŒ
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
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. If a and b have the same sign, then a/b is positive, otherwise it's negative. This is well expressed with a xor operator, so we will be using the sign of (a^b) (where ^ is a xor operator).