As pointed out by other answers, in python they return floats probably because of historical reasons to prevent overflow problems. However, they return integers in python 3.

>>> import math
>>> type(math.floor(3.1))
<class 'int'>
>>> type(math.ceil(3.1))
<class 'int'>

You can find more information in PEP 3141.

Answer from jcollado on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › floor-ceil-function-python
floor() and ceil() function Python - GeeksforGeeks
Example: ceil() of 3.3 will be 4. Note: Both functions require importing the math module: import math ... Both return an integer. Let’s take a list of floating-point numbers and apply both floor() and ceil() to each value.
Published   January 16, 2026
Discussions

what is the difference between using math.ceil and round for rounding the decimal ?
ceil always rounds up. More on reddit.com
🌐 r/learnpython
3
2
October 10, 2022
Ceil and floor equivalent in Python 3 without Math module? - Stack Overflow
I need to ceil and floor 3/2 result (1.5) without using import math. ... OK, here is the problem: to sum all numbers 15 + 45 + 15 + 45 + 15 ... with N items. ... Believe me, I tried. BTW I used PHP before, now I'm learning Python :) More on stackoverflow.com
🌐 stackoverflow.com
Floor and ceil function in python - Stack Overflow
I want to round up a float value to an integer in python to multiple of five. say 3.6 to 10 or 21.3 to 25 More on stackoverflow.com
🌐 stackoverflow.com
Is there a ceiling equivalent of // operator in Python? - Stack Overflow
I found out about the // operator in Python which in Python 3 does division with floor. Is there an operator which divides with ceil instead? (I know about the / operator which in Python 3 does fl... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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). Round down (= take the floor): math.floor() Round up (= take the ceiling): math.ceil() Difference ...
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › floor-and-ceil-function-python
floor() and ceil() function Python
August 8, 2019 - 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 ...
🌐
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.
🌐
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. ...
🌐
Javatpoint
javatpoint.com › floor-and-ceil-functions-in-python
floor() and ceil() Functions in Python - Javatpoint
February 17, 2022 - floor() and ceil() Functions in Python with python, tutorial, tkinter, button, overview, entry, checkbutton, canvas, frame, environment set-up, first python program, operators, etc.
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.

🌐
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 - Python · Locked · Print view · 2 posts • Page 1 of 1 · jahboater · Posts: 9179 · Joined: Wed Feb 04, 2015 6:38 pm · Wed Nov 16, 2022 11:40 am · The // operator does floor division for integers. DeaD_EyE · Posts: 261 · Joined: Sat May 17, 2014 9:49 pm · Thu Nov 17, 2022 11:21 am ...
🌐
W3Schools
w3schools.com › python › ref_math_ceil.asp
Python math.ceil() Method
# Import math library import math ... integer, if necessary, and returns the result. Tip: To round a number DOWN to the nearest integer, look at the math.floor() method....
🌐
Linux Hint
linuxhint.com › python-floor-ceil-functions
Python floor() and ceil() functions
July 19, 2023 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
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....
🌐
Initial Commit
initialcommit.com › blog › python-ceiling
Python Ceiling
October 10, 2021 - The ceiling function rounds up to the next whole number, while the floor function rounds down to the previous whole number. Their names represent this exactly. One is called floor (below), and the other is ceil, referring to the ceiling (above).
🌐
Rip Tutorial
riptutorial.com › rounding: round, floor, ceil, trunc
Python Language Tutorial => Rounding: round, floor, ceil, trunc
floor, ceil, and trunc always return an Integral value, while round returns an Integral value if called with one argument. ... round breaks ties towards the nearest even number. This corrects the bias towards larger numbers when performing a large number of calculations. ... As with any floating-point representation, some fractions cannot be represented exactly. This can lead to some unexpected rounding behavior. ... Python (and C++ and Java) round away from zero for negative numbers.