🌐
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.
Published   January 10, 2018
🌐
TutorialsPoint
tutorialspoint.com › floor-and-ceil-function-python
floor() and ceil() function Python
The difference occurs when the floor value of 2.3 is 2, but the floor value of 2.9 will also be 2; unlike the estimation technique which rounds off 2.9 to 3 instead of 2. Note − This function is not accessible directly, so we need to import ...
🌐
Analytics Vidhya
analyticsvidhya.com › home › understanding floor and ceiling functions in python
Floor and Ceiling Functions in Python | Applications and Behaviour
June 20, 2023 - The floor function is handy when values need to be rounded down, like when determining the number of whole units. On the other hand, the ceil function is handy when rounding up is required, like when allocating resources or determining the minimum ...
🌐
Note.nkmk.me
note.nkmk.me › home › python
Round Up/Down Decimals in Python: math.floor, math.ceil | note.nkmk.me
January 15, 2024 - In Python, math.floor() and math.ceil() are used to round down and up floating point numbers (float). Contents · Round down (= take the floor): math.floor() Round up (= take the ceiling): math.ceil() Difference between math.floor() and int() ...
Find elsewhere
🌐
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 - In Python, the floor function rounds the value towards negative infinity. By playing on this principle, you will realize that by taking the negative of negative floor, can do something like this: ... In the above code, you will be getting 4. ...
🌐
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.
🌐
TechGeekBuzz
techgeekbuzz.com › blog › floor-and-ceil-function-in-python
floor() and ceil() function in Python - Techgeekbuzz
We can also use the Python int() ... math.floor() return the same result. The math.ceil() method is also used to round a number, and it works the opposite of math.floor() method....
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.

🌐
Python
docs.python.org › 3 › library › math.html
math — Mathematical functions
Return x with the fractional part removed, leaving the integer part. This rounds toward 0: trunc() is equivalent to floor() for positive x, and equivalent to ceil() for negative x.
🌐
Linux Hint
linuxhint.com › python-floor-ceil-functions
Python floor() and ceil() functions
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
Medium
medium.com › @heyamit10 › understanding-numpy-round-up-ceil-vs-round-vs-floor-f155922395b8
Understanding NumPy Round Up (ceil vs round vs floor) | by Hey Amit | Medium
April 12, 2025 - Rounding up, rounding down, and ... expect. Let’s break it down with three key NumPy functions: np.ceil(): Always rounds up to the nearest integer. np.floor(): Always rounds down to the nearest integer....
🌐
Upgrad
upgrad.com › home › blog › data science › floor function in python: the underrated trick!
Floor Function in Python: Definition, Uses & Examples
October 29, 2025 - Together, the ceiling and floor function in Python help you handle both upward and downward rounding logic precisely, an essential skill for data processing, financial analysis, and mathematical computations. ... The floor function in Python behaves differently from other rounding methods like round(), ceil(), and trunc().
🌐
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
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
🌐
Initial Commit
initialcommit.com › blog › python-ceiling
Python Ceiling
But specifically, the next whole number on the right side of the number line. While flooring outputs the next whole number on the left side of the number line. In this article, we learned how the math.ceil(...) function works in Python.
🌐
Scaler
scaler.com › home › topics › floor() function in python
floor() Function in Python - Scaler Topics
August 3, 2023 - In the following example given ... for mathematical expressions. Mathematical expressions, such as the ceil method in Python, can be utilized as the floor() function's parameter....