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
🌐
Python
docs.python.org › 3 › library › math.html
math — Mathematical functions
If x is not a float, delegates to x.__floor__, which should return an Integral value. ... Fused multiply-add operation. Return (x * y) + z, computed as though with infinite precision and range followed by a single round to the float format. This operation often provides better accuracy than the direct expression (x * y) + z. This function follows the specification of the fusedMultiplyAdd operation described in the IEEE 754 standard.
🌐
Codecademy
codecademy.com › docs › python › math module › math.floor()
Python | Math Module | math.floor() | Codecademy
April 23, 2025 - The math.floor() function takes in a numeric data type and rounds the value down to the nearest integer. This function is part of Python’s built-in math module, which provides access to mathematical functions defined by the C standard.
🌐
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
🌐
W3Schools
w3schools.com › python › ref_math_floor.asp
Python math.floor() Method
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... # Import math library import math # Round numbers down to the nearest integer print(math.floor(0.6)) print(math.floor(1.4)) print(math.floor(5.3)) print(math.floor(-5.3)) print(math.floor(22.6)) print(math.floor(10.0)) Try it Yourself »
🌐
W3Schools
w3schools.com › c › ref_math_floor.php
C Math floor() Function
The floor() function is defined in the <math.h> header file. Tip: To round a number UP to the nearest integer, look at the ceil() function.
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.

Find elsewhere
🌐
Sololearn
sololearn.com › en › Discuss › 2672320 › i-am-confused-between-floor-ceil-and-round-functions-in-c-programming-could-you-please-explain-it-down-here-
I am confused between floor(), ceil() and round() functions in C Programming. Could you please explain it down here? 🙏🥺 | Sololearn: Learn to code for FREE!
See there a trick floor means down so if any no. like 25.6 it will go down remaining 25. And in ceil it means top so if any no. like 25.6 is taken it'll go up that's 26. In round function that's pretty simple if value after . is greater than 5 then add 0.5 to the no. otherwise let it be as it is. 22nd Jan 2021, 2:36 AM · Aditya Mishra · Answer · Learn more efficiently, for free: Introduction to Python ·
🌐
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 with division can break software. Many programming languages (including C, C++, C#, Java, Julia, PHP, R, and Python) provide standard functions for floor and ceiling, usually called floor and ceil, or less commonly ceiling.
🌐
KooR.fr
koor.fr › Python › API › python › math › floor.wp
KooR.fr - Fonction floor - module math - Description de quelques librairies Python
Return the floor of x as an Integral. This is the largest integer <= x. La valeur de retour est de type int. La valeur de retour correspond à l'arrondi entier inférieur (l'arrondi plancher).
🌐
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 - Next, we define a Python variable called quantity. This variable stores the quantity of the bean we have in storage. We use the math.floor() function to round down the quantity variable to its nearest whole number.
🌐
Tutorialspoint
tutorialspoint.com › python › number_floor.htm
Python Number Floor Function
The floor value of x: 56 The floor value of y: -34 · However, this method will not accept any value as its argument other than numeric objects. In this example, let us try to pass a list containing numeric objects as an argument to this method and check whether it calculates the floor values for all of them or not.
🌐
Upgrad
upgrad.com › home › blog › data science › floor function in python: the underrated trick!
Floor Function in Python: Definition, Uses & Examples
October 29, 2025 - Whether you’re working with prices, timestamps, or dataset values, the floor function ensures consistent and predictable outputs. In this guide, you'll read more about what the floor function in · Python is, its syntax and working, real-world examples, differences between floor and ceiling functions, use in NumPy, edge cases, and practical mini projects that demonstrate its importance in everyday programming.
🌐
Analytics Vidhya
analyticsvidhya.com › home › understanding floor and ceiling functions in python
Floor and Ceiling Functions in Python | Applications and Behaviour
June 20, 2023 - Visualizing Patterns and Trends in DataBasics of MatplotlibBasics of SeabornData Visualization with SeabornExploring Data using Python ... The floor and ceiling functions are mathematically used to round numbers to the nearest integer. This comprehensive guide will explore the floor and ceiling functions in Python, understand their formulas, and discover their various use cases and applications.
🌐
Medium
medium.com › @pies052022 › what-is-floor-function-in-python-complete-guide-with-examples-80b956e66c4f
What is Floor Function in Python? (Complete Guide with Examples) | by JOKEN VILLANUEVA | Medium
November 20, 2025 - floor() is a built-in math library function in Python that takes two numbers and returns the lowest of them. The math.floor() function takes a number as an argument and returns the largest integer that is not greater than the number given.
🌐
W3Schools
w3schools.com › python › ref_math_ceil.asp
Python math.ceil() Method
Python If Python Elif Python Else Shorthand If Logical Operators Nested If Pass Statement Code Challenge Python Match ... Python Functions Python Arguments Python *args / **kwargs Python Scope Python Decorators Python Lambda Python Recursion Python Generators Code Challenge Python Range
🌐
TutorialsPoint
tutorialspoint.com › floor-and-ceil-function-python
floor() and ceil() function Python
The floor value of x: 56 The floor value of y: -34 · However, this method will not accept any value as its argument other than numeric objects. In this example, let us try to pass a list containing numeric objects as an argument to this method and check whether it calculates the floor values for all of them or not.