The floor() function:floor() method in Python returns the floor of x i.e., the largest integer not greater than x.
Syntax: import math math.floor(x)
Parameter: x-numeric expression.
Returns: largest integer not greater than x. Below is the Python implementation of floor() method:
Python
# Python program to demonstrate the use of floor() method
# This will import math module
import math
# prints the ceil using floor() method
print "math.floor(-23.11) : ", math.floor(-23.11)
print "math.floor(300.16) : ", math.floor(300.16)
print "math.floor(300.72) : ", math.floor(300.72)
Output:
math.floor(-23.11) : -24.0 math.floor(300.16) : 300.0 math.floor(300.72) : 300.0 The ceil() Function:The method ceil(x) in Python returns a ceiling value of x i.e., the smallest integer greater than or equal to x.
Syntax: import math math.ceil(x)
Parameter: x:This is a numeric expression.
Returns: Smallest integer not less than x. Below is the Python implementation of ceil() method:
Python
# Python program to demonstrate the use of ceil() method
# This will import math module
import math
# prints the ceil using ceil() method
print "math.ceil(-23.11) : ", math.ceil(-23.11)
print "math.ceil(300.16) : ", math.ceil(300.16)
print "math.ceil(300.72) : ", math.ceil(300.72)
Output:
math.ceil(-23.11) : -23.0 math.ceil(300.16) : 301.0 math.ceil(300.72) : 301.0 Using integer division and addition:In this approach, x // 1 is used to obtain the integer part of x, which is equivalent to math.floor(x). To obtain the ceiling of x, we add 1 to the integer part of x.
Python
x = 4.5
# Round x down to the nearest integer
rounded_down = x // 1
print(rounded_down) # Output: 4
# Round x up to the nearest integer
rounded_up = x // 1 + 1
print(rounded_up) # Output: 5
Approach: The code takes a float number x and uses floor division to round it down to the nearest integer. It then prints the result. It then uses floor division and addition to round x up to the nearest integer, and prints the result.
Time Complexity: The time complexity of the round() function is constant, which means that the time complexity of the alternative code is also constant. The time complexity of the original code is also constant, as it uses only a few simple arithmetic operations.
Space Complexity: The space complexity of both the original code and the alternative code is constant, as they both use only a few variables to store the input and the result.
floor() and ceil() function Python – FAQsWhat does floor() function do?The floor() function in Python, typically found in the math module, returns the largest integer less than or equal to a given number. It effectively rounds down a floating-point number to the nearest integer.
import math # Example of using floor() result = math.floor(3.7) print(result) # Output: 3
What is the difference between floor and Ceil?The key difference between floor and ceil functions lies in their rounding behavior:
- floor: Rounds down to the nearest integer.
- ceil: Rounds up to the nearest integer.
For example:
import math # Examples of floor() and ceil() print(math.floor(3.7)) # Output: 3 print(math.ceil(3.7)) # Output: 4
What is the floor function in NumPy?In NumPy, the floor function, part of the numpy module, operates element-wise on an array and returns the largest integer less than or equal to each element.
import numpy as np # Example of floor function in NumPy arr = np.array([3.7, 2.1, -1.5, 0.6]) result = np.floor(arr) print(result) # Output: [ 3. 2. -2. 0.]
What is the Ceil function in Python?The ceil() function in Python, also found in the math module, returns the smallest integer greater than or equal to a given number.
import math # Example of using ceil() result = math.ceil(3.7) print(result) # Output: 4
How to do ceil in Python without math?You can achieve ceiling rounding without using the math module by leveraging arithmetic operations:
# Ceiling function without math module def ceil(x): return -int(-x // 1) # Example usage: result = ceil(3.7) print(result) # Output: 4
How to find the floor of a number in Python?To find the floor of a number in Python, use the math.floor() function from the math module:
import math # Example of finding the floor of a number result = math.floor(3.7) print(result) # Output: 3
|