Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Ceil Function in Python

Last Updated on May 16, 2023 by Prepbytes

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 Ceil Function in Python?

The Python math module, which is a part of the Python Standard Library, offers the ceil function. It is equivalent to the mathematical Least Integer Function or Ceil Function.

If you’re building a program to calculate the interest on a loan and the bank mandates that no such monetary quantities contain fractional amounts of rupees (like paisa) in order to facilitate transactions, then what happens? You choose to round the resultant number off to the next nearest integer keeping in mind that the bank shouldn’t lose money.
So, what method would you use? utilizing Python’s ceil() function

In mathematical notation, if you’re given a real number x, ceil(x) is depicted using ⌈x⌉, 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 greatest integer ≤ x) is depicted using ⌊x⌋, where the downward symbol depicts the flooring operation.

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

x, if x ∈ Z 

⌊x+1⌋, if x ∉ Z

What this piecewise formula says is that if the input for the ceil function is an integer, then the greatest integer greater or equal to that number is that number itself. Otherwise, just add one to that number and then floor.

Ceil function in Python also performs the same way. It returns the smallest integer greater than or equal to the input real number. For example,

ceil(2.3) = 3
ceil(-1.3) = -1
ceil(3) = 3

Syntax of Ceil Function in Python:

Here we have code for ceil function in python

Import math
math.ceil(x)

Here, x is the number or expression for which the lowest integer larger than or equal to it must be calculated. math is the Python math module. Ceil() produces an integer as a result.

Parameter Values of Ceil Function in Python

The ceil function only accepts real numbers (float) or integers as input. If the input parameter is an integer, the ceil function returns the same integer. If the input parameter is a float, the ceil function rounds up float values to the next integer greater than or equal to the input value.

Some important things to note about the ceil function are:

  • The value of the ceil function is always an integer.
  • If the supplied value is already an integer, the ceil function just returns the same number.
  • If the input parameter is NaN (not a number), the ceil function returns the same value. Regardless of whether the input value is positive or negative infinity, the ceil function produces the same output.

Example1 of Ceil Function in Python

In this example we have taken a positive float value

# your code goes here

import math

num = 17.8
ceil_num = math.ceil(num)
print(ceil_num) # Output: 18

Output

18

Explanation: In the above python program we have taken positive float value 17.8 , so the nearest greatest integer of 17.8 is 18 so if we observe on output screen ceil function gives 18 as a output .

Example 2 of Ceil Function in Python

In this example we have taken a positive integer value

# your code goes here



import math

num = 3
ceil_num = math.ceil(num)
print(ceil_num) # Output: 3

Output

3

Explanation: In the above python program we have taken positive integer value 3, so the nearest greatest integer of 3 is itself 3 so if we observe on output screen ceil function gives 3 as a output .

Example 3 of Ceil Function in Python

In this example we have taken a negative integer value

# your code goes here


import math

num = -12.5
ceil_num = math.ceil(num)
print(ceil_num) # Output: 4

Output

-12

Explanation:
In the above python program we have taken negative float value -12.5, so the nearest greatest integer of -12, not -13, because -12.5 is greater than -12, so if we observe on output screen ceil function gives 3 as an output.

Conclusion
For rounding up values to the next integer in Python, use the ceil() function. It may be applied to a broad range of tasks, from basic math to more complex computations. The ceil() function is useful in a variety of situations, including rounding up numbers by determining the lowest integer that is greater than or equal to a given floating-point number. For instance, if a program has to figure out how many pages are required to print a document, it can use the ceil() method to determine how many pages are required to fit any extra text after dividing the total amount of words by the number of words per page.

Frequently Asked Questions (FAQ)

Here are some FAQs on ceil function in Python:

Q1. What is the difference between the ceil and floor functions in Python?
Ans. The ceil function in Python returns the smallest integer greater than or equal to the given number, while the floor function returns the largest integer less than or equal to the given number.

Q2. What is the return type of the ceil function in Python?
Ans. The ceil function in Python returns an integer value.

Q3. What happens if we pass a non-numeric value to the ceil function in Python?
Ans. If we pass a non-numeric value to the ceil function in Python, it will result in a TypeError.

Q4. Can we use the ceil function for floating-point numbers in Python?
Ans. Yes, the ceil function can be used for both integer and floating-point numbers in Python.

Q5. Can we use the ceil function for negative numbers in Python?
Ans. Yes, the ceil function can be used for negative numbers in Python. If the given number is negative, the ceil function returns the smallest integer that is less than or equal to the given number.

Leave a Reply

Your email address will not be published. Required fields are marked *