floor() and ceil() function in python

In this tutorial, we will cover floor and ceil functions, as well as their functionality, using a simple piece of code.

The math module, which can be found by importing the “math” library, is a collection of number-related tools such as functions for dealing with square roots, cosine, and sine.

The two methods – floor() and ceil() that you are referring to here are a part of this set. In order to understand how they work, let’s first have a quick look at what they do individually.

The math.floor() function gets the nearest integer value underneath the passed float or decimal argument while math.ceil() gets the nearest integer value above it instead. So regardless of your number being greater than 1 or smaller than 1 in magnitude i.e: either above infinity or below zero in values, both will return an integer value – anything from 0 to positive infinity for the floor() and positive infinity down to 0 for ceil().

Why do we use floor and ceil functions in python?

When programming in Python, you might come across a situation where you have to round a number to the nearest whole integer. To fulfill that situation, you will want to use math.ceil() and math.floor() functions. These functions both round up or down to the nearest integer but one (ceil) rounds up while the other (floor) rounds down.

Ceil method

To use the method ceil(), we must first import the math module into our program. This module has several methods and functionalities that make a programmer’s job easier.

It makes no difference whether we use ceil() on an integer or not. For floating-point numbers, the ceil() function is used. The ceiling of a number is also known as the upper bound of a number. A ceiling only applies to floating-point numbers. So, before tacking floats ceiling, we need to convert it into one: the function ceil() only accepts one argument, math.ceil(arg), where arg is the number we are looking for its ceiling.

Example:

import math
print(math.ceil(45.86))
print(math.ceil(45.98))
print(math.ceil(-78.68))
print(math.ceil(-94.99))

Output:

img1

Floor Method

Floating-point numbers have a lower bound. So floor() is only applicable to floating-point numbers. The Integer Function (int()) converts a number into a long integer, while the Decimal Function of Decimal(), on the other hand, converts an integer or float into a decimal, but to avoid ambiguity when converting an integer, you need to multiply that number by 10 and then divide it by 10. This is equivalent to dividing the integer by 1 or 10 respectively and discarding any remainder.

Example:

import math
print(math.floor(48.25))
print(math.floor(45.98))
print(math.floor(-38.68))
print(math.floor(-24.99))

Output:

img2

Conclusion of floor and ceil methods

In conclusion, we discussed the ceil and floor functions of Python. The ceil function (short for ceiling) and the floor function are both mathematical functions that are present in virtually every programming language.

The roles of these two surfaces aren’t the same. For example, the ceiling function, in many respects, returns the smallest integer value that is greater than or equal to the supplied number.

The floor function always results in the greatest whole number less than or equal to the number provided. The input is always an integer with a .0 number in the trailing position.

That’s all for this article if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn

Suggested Articles:

  1. How to Connect Python with Database
  2. How to create a virtual environment in Python?
  3. What is the difference between pip and pip3?

Leave a Comment