Python math.ceil() – Ceiling of a Number

Python math.ceil()

math.ceil(x) function returns the smallest integer greater than or equal to x.

Syntax

The syntax to call ceil() function is

math.ceil(x)

where

ParameterRequiredDescription
xYesA numeric value.

If x is infinity, then ceil(x) raises OverflowError.

If x is nan, then ceil(x) raises ValueError.

Examples

Ceiling of floating point number.

Python Program

import math

x = 5.25
result = math.ceil(x)
print('ceil(x) :', result)

Output

ceil(x) : 6

Ceiling value of negative floating point number

Python Program

import math

x = -5.25
result = math.ceil(x)
print('ceil(x) :', result)

Output

ceil(x) : -5

Ceiling value of infinity.

Python Program

import math

x = math.inf
result = math.ceil(x)
print('ceil(x) :', result)

Output

OverflowError: cannot convert float infinity to integer

Ceiling value of an integer.

Python Program

import math

x = 5
result = math.ceil(x)
print('ceil(x) :', result)

Output

ceil(x) : 5

Ceiling value of nan.

Python Program

import math

x = math.nan
result = math.ceil(x)
print('ceil(x) :', result)

Output

ValueError: cannot convert float NaN to integer

Summary

In this Python Examples tutorial, we learned the syntax of, and examples for math.ceil() function.

Code copied to clipboard successfully 👍