- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# How to find the smallest number greater than x in Python?

In this article, we will show you how to find the smallest number greater than x in Python.

## Introduction to Ceil Function in Python

Ceil is a function in Python's math module, which is included in the Python Standard Library. In mathematics, it is the same as the Least Integer Function or the Ceil Function.

If you are given a real integer x, **ceil(x)** is represented in mathematical notation as ⌈x⌉, where the upper direction of the brackets corresponds to **ceiling operation** (as the ceiling lies above your head).

In contrast, the **floor(x)** (which returns the greatest integer ≤x) is represented by using ⌊x⌋, where the downward sign represents the **flooring operation**.

Using a piecewise definition, ceil(x) =

x, ifx∈Z ⌊x+1⌋, ifx∉Z

### ceil() Function

In Python, the method ceil(x) returns the smallest integer greater than or equal to x. It is called the ceiling value of x.

### Syntax

import math math.ceil(x)

### Parameters

**x**− any real number

**Return value **− Returns the smallest integer not less than x.

## Calling ceil() Function in 2 Ways

Depending on what you've imported into your Python application, you can invoke the **ceil()** method in two ways.

If you imported the whole

**math module**rather than just the function, we must use the**dot (.)**operator to access the**ceil()**function (since it is defined in math module). The format is as follows if y is our output variable and x is our numeric input variable −

y= math.ceil(x)

If you imported the

**ceil()**function with the math module, the syntax for calling the function becomes easier, as seen below −

y= ceil(x)

If you additionally imported another module that has its own

**ceil() defined**(which may or may not be for the same purpose), you must use the first approach to avoid ambiguity.

## Example

### Algorithm (Steps)

Following are the *Algorithm/steps* to be followed to perform the desired task −

Use the import keyword to import the

**math**module.Use the

**math.ceil()**function to get the ceiling value of a number i.e, the smallest integer greater than or equal to the number by passing the number as an argument to it.

### Example

The following program returns the ceiling value i.e, the smallest integer greater than or equal to x using **math.ceil()** function −

# importing math module import math # getting the ceiling value of numbers using math.ceil() print("The Smallest Integer Greater than -12.11 is:", math.ceil(-12.11)) print("The Smallest Integer Greater than 50.26 is:", math.ceil(50.26)) print("The Smallest Integer Greater than 30.5 is:", math.ceil(30.5)) print("The Smallest Integer Greater than 1.1 is:", math.ceil(1.1))

### Output

On executing, the above program will generate the following output −

The Smallest Integer Greater than -12.11 is: -12 The Smallest Integer Greater than 50.26 is: 51 The Smallest Integer Greater than 30.5 is: 31 The Smallest Integer Greater than 1.1 is: 2

## Smallest Integer Function using int() function

### Algorithm (Steps)

Following are the *Algorithm/steps* to be followed to perform the desired task −

Check if the number passed is greater than 0 and if it is true then Return the integer format of a number passed using the

**int()**function(returns an integer from a given object) +1. This returns the smallest number greater than the given argument.Else return the integer format of the number.

Call the

**smallestInteger()**function by passing a number as an argument and print the resultant smallest integer function value of a number returned by it.Similarly, find for the other numbers and observe the changes.

### Example

The following program returns the ceiling value i.e, the smallest integer greater than or equal to x using the **int()** function −

# creating a function that returns by taking the given number # as an argument def smallestInteger(n): # Checking if the number is greater than 0 if(n>=0): # Return the integer format of the given number+1 return int(n)+1 # Else if it is a negative number else: # Return the integer format return int(n) # calling the smallestInteger() function by passing given number as an argument print('The Smallest Integer Greater than 5.2 is:',smallestInteger(5.2)) print('The Smallest Integer Greater than 0.2 is:',smallestInteger(0.2)) # Negative Number print('The Smallest Integer Greater than -5.2 is:',smallestInteger(-5.2))

### Output

On executing, the above program will generate the following output −

The Smallest Integer Greater than 5.2 is: 6 The Smallest Integer Greater than 0.2 is: 1 The Smallest Integer Greater than -5.2 is: -5

## Cases of Exceptions When invalid Parameters Passed

As we saw in the preceding section, **ceil()** in Python accepts a single numeric argument. In Python, numerical types include int, float, and complex, however only the real types, int and float, are allowed input parameters for ceil ().

Because boolean is a subtype of int, True and False values will be accepted as input as well. In fact, Python **ceil()** will treat them as 1 and 0 respectively.

### Passing string as an input

The following program returns a TypeError as the ceil() function doesn’t accepts string as an input −

# importing math module import math # passing the input as string # returns a TypeError print(math.ceil("2.3"))

### Output

On executing, the above program will generate the following output −

Traceback (most recent call last): File "main.py", line 6, inprint(math.ceil("2.3")) TypeError: must be real number, not str

### Passing list as an input

The following program returns a TypeError as the ceil() function doesn’t accepts list as an input −

# importing math module import math # input list list = [1,2,3] # passing the input as list # returns a TypeError print(math.ceil(list))

### Output

On executing, the above program will generate the following output −

Traceback (most recent call last): File "main.py", line 9, inprint(math.ceil(list)) TypeError: must be real number, not list

## Conclusion

In this article, we studied two methods for finding the smallest number greater than x in Python. With examples, we also studied the case of exceptions for the ceil() (smallest Integer Function).