Python

What is ceil() function in Python?

What is ceil() function in Python?

Introduction

In this tutorial, you will learn how to round values up in Python to the nearest integer value using the math.ceil() function. Additionally, you'll discover how using the function differs from merely calling the int function. Finally, you'll discover how to compute ceiling division using Python. There are many real-world applications for Python's ability to round numbers up. This often makes working with numbers more intuitive. The ability to round numbers in the way you like will help you become a better programmer because of all the applications it has.

Rounding Concept Explained

In most cases, it is customary to round numbers to the nearest integer. This implies that you may be given a number, like 7.3. 7.3 will round down to 7 because it is only 0.3 away from 7 and 0.7 away from 8. However, you may use Python math.ceil() method to round an integer up. When you round a number up, you choose the nearest integer that is higher than the original value. As a result, if you round 7.3 up, it gives you 8.

Developing your own Python scripts can benefit greatly from understanding how to round numbers up. You may learn everything there is to know about Python ceiling function, math.ceil, in the sections that follow ().

Understanding the ceiling() function

The built-in math module's ceil() method only accepts one input, an integer. The function will return a single integer, which must be the lowest integer that is bigger than or equal to the supplied value. Let's look at how to round numbers up to the closest integer using Python's math.ceil() method. First, let's import the math library:

import math

Instead of importing the full module, we could alternatively just import the function directly:

from math import ceiling

But for the sake of this tutorial, let's import the whole library. Let's examine how we can use certain numbers—both positive and negative—and how it affects the Python ceiling function:


import math
print(math.ceil(7.2))
print(math.ceil(-7.2))

Output:
8
-7

The math. The Python method ceil() rounds numbers up to the next integer that is either bigger than or equal to the value that is being supplied to it. A positive number therefore rounds to the following number. In contrast, a negative number returns the number's truncated values.

Differences between Int & Ceiling

A single input, often a floating point number, is passed to the Python int() method, which transforms it into an integer. Because of this, it is also possible to round values using the function, but the behavior is a little different than expected. See what happens if we supply a positive floating point number to both the math and the int() functions.

import math

print(int(7.3))
print(math.ceil(7.3))
 

Output:

7

8

We can see that the int() method truncates everything after the decimal point, but the math.ceil() function rounds the number up as intended. To claim that the int() method rounds values down would be untrue. We observe that the behavior alters when we examine negative floating point values:

import math

print(int(-7.3))
print(math.ceil(-7.3)) 


Output:

-7

-7

write your code here: Coding Playground

We can see that the two functions both return the same value when dealing with negative floating point numbers. Because the two functions serve different purposes, it's critical to comprehend how they differ from one another.

Python Ceiling Division

Python includes a variety of techniques for computing division operations, and each one is thoroughly explained. The floor division operator in Python is one of them, and its symbol is /. The floored integer value derived from the two values is what is returned when this operator is used. Python's lack of an easy-to-use built-in feature to compare ceiling divisions, which results in the return of the ceiling of the quotient of the two values, is an intriguing feature.

However, we can imitate this by creating a function of our own to achieve this. Let's examine how this would appear:

def ceiling_division(numerator, denominator):
return -(-numerator // denominator)

print(ceiling_division(8, 3))

# Returns 3

write your code here: Coding Playground

Here is a breakdown of what this function entails:

  1. In our definition, we specify that our function can accept two arguments: a numerator and a denominator.
  2. Finally, we return the negative value obtained by dividing the denominator by the negative numerator floor.

Because a negative value moves to the lower integer value when it is rounded down, this results in a ceiling value (i.e., -7.3 will return -8). Then it becomes positive again when we convert it back to a negative number of itself.

Conclusion

You learned everything there is to know about the Python ceiling function in this lesson. You now understand what it means to round up and how Python 2 and Python 3 implement it differently. The distinctions between invoking the Python int function and the ceiling function were then covered. Since Python only includes a floor division function, you learned how to utilize Python to design a method of creating a ceiling division function.