Python math.ceil() Method

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

Ceil is a function defined in Python's math module which takes a numeric input and returns the integer greater than or equal to the input number. It is equivalent to the Least integer function in mathematics.

What is Python math.ceil() Method?

Ceil in python precisely rounds up floating-point numbers to the nearest integer, which is crucial for maintaining precision in mathematical operations. This method, part of the math module, consistently ensures no loss of decimal values during rounding.

How Does the Python math.ceil() Method Work?

We use ceil in python to round off loan interest amounts to the nearest integer without losing money. This ensures that fractional amounts are rounded up, meeting the bank's requirement for seamless transactions.

In mathematical notation, if you're given an actual number xx, ceil(x) is depicted using x\left \lceil{x}\right \rceil, where the upper direction of the brackets refers to ceiling operation (as the ceiling lies above your head). Inversely, the floor(x) (which returns the most significant integer x\le x) is depicted using x\lfloor{x} \rfloor, where the downward symbol shows the flooring operation.

Ceil Function in Python

Defining in a piecewise manner, ceil(x) = :

x,ifxZx ,\hspace{20px} if \hspace{20px} x \in \mathbb{Z}
x+1,ifxZ\lfloor{x+1} \rfloor ,\hspace{20px} if \hspace{20px} x \notin \mathbb{Z}

The ceil function in Python also performs the same way. It provides the smallest integer greater than or equal to the given real number. For example,

Examples of Python math.ceil() Method

Example 1

Output:

The minimum integer greater than or equal to the provided input = 1.234 is 2, the same as the output!

Example 2

Output:

Since the input parameter 14 is an integer, the smallest integer greater than or equal to 14 is 14 itself!

Example 3

Output:

We know that 23.42<23-23.42<-23. Therefore, the smallest integer greater than or equal to the input value is -23.

Syntax of Python math.ceil() Method

or

  • where x is a real numeric input

The ceil in python can be called in two ways depending on what you have imported into your Python program.

  1. If you have imported the entire math module, not just the function itself, we must access the ceil() using the dot (.) operator. The syntax is as follows:

  2. If you have imported the ceil() function along with the math module, then the syntax for calling the function becomes simpler and is as follows:

Parameter of Python math.ceil() Method

Ceil in python accepts only real numeric inputs, such as int and float, and treats boolean values as 1 and 0, respectively. Using non-real numeric parameters will result in an error.

Return value of Python math.ceil() Method

As would be obvious, the ceil in python returns a single numeric int type value, irrespective of whether the input was int or float data type. The output value is the smallest integer greater than or equal to the input number.

Exceptions of Python math.ceil() Method

The exceptions of math.ceil() method are:

  • The ceil in python expects its argument to be numeric, precisely a floating-point number. If a different data type is passed as an argument, a TypeError will be raised.
  • In situations where the argument provided to math.ceil() is not a finite number (e.g., inf or nan), a ValueError will be raised.
  • In cases where the argument is too large to be represented as a float or the result of the rounding operation exceeds the representational limits of the system, an OverflowError may occur.
  • The ceil in python accepts only one argument. If more than one argument is provided, a TypeError will be raised.

Differences between Int & Ceiling

Aspectint()math.ceil()
Rounding BehaviorTruncates towards zero, discarding decimals.Always rounds up to the nearest integer.
Handling NegativesTruncates towards zero for negative numbers.Always rounds towards positive infinity.
Use CasesUsed for truncating or converting to integers.Ensures rounding up with decimal precision.

Conclusion

To summarize the article

  1. ceil() is a function defined in the math module of Python's Standard Library, which yields the minimum integer greater than or equal to the provided input.
  2. We can use the ceil() function in Python by calling either ceil(x) or math.ceil() depending on what we've imported into our program
  3. Only real numeric data types, which include int, float, and boolean (the last one being a sub-type of int), are allowed as input parameters for ceil in Python.
  4. ceil in python returns an output of type int for all valid inputs

See Also

  1. Round Function in Python
  2. Square Root in Python
  3. map() in Python