Python’s math module provides many useful mathematical functions, including floor() and ceil(), which are commonly used for rounding numbers.
- floor(): Rounds a number down to the nearest integer. Example: floor() of 3.3 will be 3.
- ceil(): Rounds a number up to the nearest integer. Example: ceil() of 3.3 will be 4.
Note: Both functions require importing the math module: import math
import math
x = 3.7
print(math.floor(x))
print(math.ceil(x))
Output
3 4
Explanation:
- floor(3.7) returns 3 because 3 is the greatest integer less than or equal to 3.7.
- ceil(3.7) returns 4 because 4 is the smallest integer greater than or equal to 3.7.
Syntax:
math.floor(number)
math.ceil(number)
Parameters:
- number: A float or integer value.
Return Type:
- Both return an integer.
1. Round a List of Floats
Let’s take a list of floating-point numbers and apply both floor() and ceil() to each value.
import math
a = [1.1, 2.5, 3.9, 4.0, 5.8]
fl = list(map(math.floor, a))
cl = list(map(math.ceil, a))
print("Floor:", fl)
print("Ceil :", cl)
Output
Floor: [1, 2, 3, 4, 5]
Ceil : [2, 3, 4, 4, 6]
Explanation:
- math.floor(1.1) returns 1, and math.ceil(1.1) returns 2, and so on.
- map() function applies floor and ceil to each element of the list.
2. Compare floor() and ceil() with Negative Numbers
import math
a = -2.3
b = -5.9
print("floor(-2.3):", math.floor(a))
print("ceil(-2.3) :", math.ceil(a))
print("floor(-5.9):", math.floor(b))
print("ceil(-5.9) :", math.ceil(b))
Output
floor(-2.3): -3
ceil(-2.3) : -2
floor(-5.9): -6
ceil(-5.9) : -5
Explanation:
- floor() always rounds towards negative infinity.
- ceil() always rounds towards positive infinity.
3. Round User Input to Nearest Integer.
import math
a = 7.3
print("Rounded down using floor():", math.floor(a))
print("Rounded up using ceil():", math.ceil(a))
Output
Rounded down using floor(): 7 Rounded up using ceil(): 8
Computing Floor and Ceil Without Importing math
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.
Concept:
- x // 1 returns the largest integer less than or equal to x - similar to math.floor(x).
- To get the ceiling, just add 1 to the floor value (i.e., x // 1 + 1).
Note: This method works well for positive numbers. For negative numbers, it may not give accurate ceiling values.
Floor and Ceil using Integer Division
x = 4.5
y = -4.5
fx = x // 1 if x >= 0 else (x // 1 if x == x // 1 else x // 1 - 1)
cx = x // 1 if x == x // 1 else (x // 1 + 1) if x > 0 else x // 1
fy = y // 1 if y >= 0 else (y // 1 if y == y // 1 else y // 1 - 1)
cy = y // 1 if y == y // 1 else (y // 1 + 1) if y > 0 else y // 1
print("Floor of 4.5:", fx)
print("Ceil of 4.5 :", cx)
print("Floor of -4.5:", fy)
print("Ceil of -4.5 :", cy)
Output
Floor of 4.5: 4.0 Ceil of 4.5 : 5.0 Floor of -4.5: -6.0 Ceil of -4.5 : -5.0
Explanation:
1. Floor
- Positive: x // 1
- Negative: subtract 1 if x is not already integer (x // 1 - 1)
2. Ceil
- Positive: x // 1 + 1 if not integer
- Negative: just x // 1