🌐
JanBask Training
janbasktraining.com › community › python-python › ceil-and-floor-equivalent-in-python-3-without-math-module
Ceil and floor equivalent in Python 3 without Math module? | JanBask Training Community
August 18, 2025 - Use // 1 for floor values since it works correctly for both positive and negative numbers. For ceil, use integer truncation plus a conditional check. These methods avoid importing math, yet give you the same results.
🌐
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
def ceil_floor(x): rest = x % 1 floor = int(x // 1) ceil = floor + 1 if rest else 0 return ceil, floor · Locked · Print view · 2 posts • Page 1 of 1 · Return to “Python” · Jump to · Community · General discussion · Announcements · Other languages · Deutsch · Español · Français ...
🌐
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. ...
🌐
AskPython
askpython.com › home › round up numbers in python without math.ceil()
Round Up Numbers in Python Without math.ceil() - AskPython
June 30, 2023 - All of the functions present on your calculator have an equivalent in Python’s math module. The math module provides all operations that can be carried out using real numbers. However, this module does not support operations on complex numbers. It provides all functionalities as per the C standard. You can find the square root of numbers, remainders, use ceiling and floor functions as well using this module.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › floor-ceil-function-python
floor() and ceil() function Python - GeeksforGeeks
Apart from using the math module, we can also compute the floor and ceil of a float using basic arithmetic operations like floor division (//) and addition.
Published   January 10, 2018
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.

🌐
TechGeekBuzz
techgeekbuzz.com › blog › floor-and-ceil-function-in-python
floor() and ceil() function in Python - Techgeekbuzz
We can also use the Python int() method as an alternative for math.floor() method as shown below: >>> int(19.1) 19 >>> int(19.0) 19 >>> int(19.999) 19 · As you can see, both int and math.floor() return the same result.
🌐
Reddit
reddit.com › r/learnprogramming › [python] not able to round up despite having the math.ceil function
r/learnprogramming on Reddit: [Python] Not able to round up despite having the Math.ceil function
July 14, 2024 -

I'm building a calculator that allows users to input through a form with all the input types being number. I'm using flask to "connect" my html and python file together to allow values submitted from the form to be calculated and displayed via POST and GET methods. However, when I input a number of 154.456 it refuses to round up to 154.46 but instead gives out the follow error message:

"Please enter a valid value. The two nearest valid values are 154.45 and 154.46"

I've checked stackexchange, etc and the general advice is to change the step to "any" but I would still like to allow the input to be incremented by 0.01.

https://pastebin.com/4yJ5Fv9Y <--This is my python file and the function necessary for rounding up is

def getFormValue(form, nameOfInput, defaultValue):
    if (form.get(nameOfInput)):
        return math.ceil(float(form[nameOfInput])*100) / 100
    else:
        return defaultValue

https://pastebin.com/Hr586BMb <-- This is my HTML file

Please help me fix this issue. Feel free to ask me for any extra information.

🌐
Analytics Vidhya
analyticsvidhya.com › home › understanding floor and ceiling functions in python
Floor and Ceiling Functions in Python | Applications and Behaviour
June 20, 2023 - Confusing the floor function with other rounding functions, such as round or trunc. Discover common pitfalls encountered when working with the ceil function in Python and acquire strategies to overcome them. Neglecting to import the math module before using the ceil function.
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › find-ceil-ab-without-using-ceil-function
Find ceil of a/b without using ceil() function - GeeksforGeeks
September 20, 2022 - # Python3 program to find # math.ceil(a/b) without # using math.ceil() function import math # Driver Code # taking input 1 a = 4; b = 3; val = (a + b - 1) / b; print("The ceiling value of 4/3 is ", math.floor(val)); # example of perfect division ...
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.ceil.html
numpy.ceil — NumPy v2.2 Manual
numpy.ceil(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature]) = <ufunc 'ceil'>#