Open In App

floor() and ceil() function Python

Last Updated : 06 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Python3




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


Output

4.0
5.0

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.



Next Article

Similar Reads

Pandas Series dt.ceil | Ceil DateTime To Specified Frequency
The dt.ceil() method performs ceil operation on the data to the specified frequency. Example C/C++ Code import pandas as pd sr = pd.Series(['2012-12-31 08:45', '2019-1-1 12:30', '2008-02-2 10:30', '2010-1-1 09:25', '2019-12-31 00:00']) idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5'] sr.index = idx sr = pd.to_datetime(sr) result = sr.dt.ceil(fre
2 min read
Ceil and floor of the dataframe in Pandas Python – Round up and Truncate
In this article, we will discuss getting the ceil and floor values of the Pandas Dataframe. First, Let's create a dataframe. Example: C/C++ Code # importing pandas and numpy import pandas as pd import numpy as np # Creating a DataFrame df = pd.DataFrame({'Student Name': ['Anuj', 'Ajay', 'Vivek', 'suraj', 'tanishq', 'vishal'], 'Marks': [55.3, 82.76,
2 min read
Python | math.ceil() function
In Python, math module contains a number of mathematical operations, which can be performed with ease using the module. math.ceil() function returns the smallest integral value greater than the number. If number is already integer, same number is returned. Syntax: math.ceil(x) Parameter: x: This is a numeric expression. Returns: Smallest integer no
1 min read
Python | Pandas DatetimeIndex.ceil()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas DatetimeIndex.ceil() function ceil the data to the specified frequency. The function takes the target frequency as input. It retu
2 min read
Python | Pandas TimedeltaIndex.ceil
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas TimedeltaIndex.ceil() function ceil the index of the TimedeltaIndex object to the specified freq. The function takes the frequenc
2 min read
Python | Pandas Timestamp.ceil
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.ceil() function return a new Timestamp ceiled to this resolution. The function takes the desired time series frequency
2 min read
Python | Pandas Timedelta.ceil()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Timedelta is a subclass of datetime.timedelta, and behaves in a similar manner. It is the pandas equivalent of python’s datetime.timedel
1 min read
Python - PyTorch ceil() method
PyTorch torch.ceil() method returns a new tensor having the ceil value of the elements of input, Which is the smallest integer larger than or equal to each element. Syntax: torch.ceil(inp, out=None) Arguments inp: This is input tensor. out: The output tensor. Return: It returns a Tensor. Let's see this concept with the help of few examples: Example
1 min read
numpy.ceil() in Python
The numpy.ceil() is a mathematical function that returns the ceil of the elements of array. The ceil of the scalar x is the smallest integer i, such that i >= x Syntax : numpy.ceil(x[, out]) = ufunc ‘ceil’) Parameters : a : [array_like] Input array Return : The ceil of each element with float data-type. Code #1 : Working # Python program explain
2 min read
Ceil in a Binary Search Tree Python
In a Binary Search Tree (BST), the ceiling of a given value is the smallest element in the tree that is greater than or equal to the given value. Finding the ceiling in a BST can be a useful operation when working with ordered data. In this article, we will explore how to find the ceiling in a Binary Search Tree using Python. Binary Search Tree Ove
3 min read
Practice Tags :
three90RightbarBannerImg