Python supports different modules and packages to do various programming tasks. The module named βmathβ is used in Python to perform various calculations on data using multiple functions. Some of the functions of the math module are factorial(), floor(), ceil(), exp(), log10(x) etc.
In this article, one of the functions of the βmathβ module named βmath.ceil()β is demonstrated in depth using various examples. The following topics are part of our Python Guide:
- What is Python βmath.ceil()β Function?
- Example 1: Ceiling of a Number Using math.ceil()
- Example 2: Errors in math.ceil() function
So let’s get started!
What is Python βmath.ceil()β Function?
The βmath.ceil()β function takes numeric data type values like int and floats and rounds up the given number to the nearest integer. The syntax of the βmath.ceil()β function is shown below:
math.ceil(numeric expression)
The numeric expression value is required in the βmath.ceil()β function to return its ceiling value in an integer.
Note: Boolean value (True and False) can also be passed inside the βmath.ceil()β function. They will be utilized as β1 and 0β.
Let’s understand the working of this function with numerous examples given below:
Example 1: Ceiling of a Number Using math.ceil()
In the code given below, the βmath.ceil()β function is used to find the ceiling number of different positive and negative float numbers
Code:
import math
print(math.ceil(222.7))
print(math.ceil(992.882))
print(math.ceil(-93.7))
print(math.ceil(-4.8))
print(math.ceil(49))
In the following code:
- The βmathβ module is imported in the program using the prefix βimportβ.
- The βmath.ceil()β function is used with β5β different values in the above code, i.e., two βPositive floatβ, two βNegative floatβ and one βAbsolute integerβ value.
- The positive value will round up to the nearest integer, and the negative float value will round toward zero.
Output:
The output shows the ceiling value of numeric data.
Example 2: Errors in math.ceil() Function
When the positive or negative infinite value is passed in βmath.ceil()β function. The function returns an βoverflowβ error as an output. Let’s understand via the following code:
Code:
import math
number = math.inf
print(math.ceil(number))
In the following code, the βmath.ceil()β function throws an error when the infinite number is passed to it. The βmath.infβ expression retrieves a floating point infinity.
Output:
The output shows the βoverflow errorβ of math.ceil() function.
Thatβs all from this guide!
Conclusion
In Python, the βmath.ceil()β function takes positive or negative numeric values as arguments and rounds off the given numbers to the nearest integer. The βmath.ceil()β function does not change the absolute integer value. This function gives an error when an infinite or undefined value is passed to it. This article provided all the details regarding the βPython math.ceil()β function with numerous examples.