In this tutorial, we are going to be discussing how to return the ceiling of an input today. This is going to be done by using python and google collab. Recall that you have probably tried the greatest integer function and the smallest integer function when you were younger. The ceil function is the smallest integer function and the floor function is the greatest integer function. We will be implementing all of these in our code snippet today. Let’s get started.

*Also read: Numpy floor – Return the floor of the input, element-wise*

## What is ceiling function?

The ceiling function for an input *x* is the nearest smallest integer I, such that I should be greater than or equal to x. It is denoted as . Let us observe the examples below.

- The ceiling function for 0.366 would be 1. because 1 is the nearest smallest integer that is greater than 0.366.
- The ceiling function for -0.366 would be 0. because 0 is the nearest smallest integer that is greater than -0.366.
- The ceiling function for -3 would be -3. because -3 is the nearest smallest integer that is greater than or equal to -3.

The NumPy.ceil() function is used to round a number up to the nearest integer. This function is imported from the Numpy library, which is a library for the Python programming language that enables mathematical operations on arrays and matrices. This function can be applied to single-valued inputs as well as arrays.

## Implementing the Numpy.ceil() method

Today we will see how to implement this function for a positive value, a negative value, an integer, and an array. Let us see the syntax for using this function below.

```
numpy.ceil(x, out=None, where=True, casting='same_kind', order='K', subok : [bool, datatype])
```

The parameters used in the above snippet function as follows:

**x:**It can be a variable containing a value in Radian or it may be an array containing some value**out:**A location in which the result is to be stored. If provided, it must have a shape similar to the input X. If not provided or None, a fresh value or result is returned. It’s optional.**where = true:**Where the condition is True, We must get our result or output for “where = False” We will not get any result. It’s optional. y default its value is True.**casting=’same_kind’:**It means only float64 to float32 value or results are allowed. The function shout cast values in this range or datatype.**order = ‘K’:**It always results in an output array in the form of k ordered. (Note: There are 4 types of orders as**{‘K’, ‘C’, ‘F’, ‘A’}**). It’s optional.**subok**: [bool, datatype]**to make a subclass of the result or not**. if True then provide a name for the subclass. It returns an array with the same shape and type as a given array. It’s also optional.

Now we will implement the same in our code snippet below. We will have four code snippets for implementing this function by passing different inputs in each code snippet. let us get it.

## Numpy.ceil() function for positive numbers

```
import numpy as np
a = np.ceil(0.366)
a
#output
1.0
```

In the above code snippet, we implemented our `Numpy.ceil()`

function after importing the Numpy library as well. We passed a positive fractional value as its argument and got the same as in the example above.

## Ceiling function for negative numbers

Similarly, let us try passing a negative fractional value and see the result below.

```
import numpy as np
b = np.ceil(-0.366)
b
#output
-0.0
```

## Ceiling function for an array

This time, we will pass an array comprising some values as below. Let us see how it results.

```
import numpy as np
import math
input = np.array([-2.35,-1, -0.36, 0, 0.36, 1, 5.69, 5%2, 5/2, math.pi ])
c = np.ceil(input)
c
#output
array([-2., -1., -0., 0., 1., 1., 6., 1., 3., 4.])
```

You can see that it gives us an array for ceiling functions of all the elements inside our input array. Let us focus on the last two elements, We had passed 5%2, 5/2, and math.pi. Do you analyze how does it react to this type of input? It computed respective values as 5%2 = 1, 5/2 = 2.5, and math.pi = 22/7 =3.142857142857… then it implements the ceiling function for 1, 2.5, and 3.142857142857… respectively.

One more thing, We all need to notice that, The output array comprises all the values of float datatype. So, Be sure, The datatype in our syntax is by default float, It always gives the output in float or decimal.

## Ceiling function for Complex numbers

```
import numpy as np
import cmath
x=5
y=6
z=complex(x,y)
print(z)
d = np.ceil(z)
print(d)
```

As We can see, We imported the** ” cmath ” **module created a complex no. using

**method and finally passed the same onto our**

`complex()`

**function. Let us see how it behaves to our input. Let us print the value for d.**

`numpy.ceil()`

```
print(z)
print(d)
(5+6j) #value of z
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-12-b0ea321d89b0> in <module>
5 z=complex(x,y)
6 print(z)
----> 7 d = np.ceil(z)
8 print(b)
TypeError: ufunc 'ceil' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
```

You can see that It throws an error. you got what the error is? **The ceil function does not support this type of input.**

## Plotting numpy.ceil() on a graph

In the code snippets below, We have created empty arrays x[] and y[]. Using a while loop, We have loaded input values starting from -3.99, sequentially increasing by 0.01 since 4.00 into the x[] array. After, Implementation of the `ceil`

function We loaded the respective results into the y[] array. Using both x[] and y[] arrays we have plotted our graph using `matplotlib.pyplot `

library and its supportive methods.

```
import matplotlib.pyplot as plt
import numpy
x=[]
y=[]
i=-3.99
while (i<4.00):
x.append(i)
y.append(numpy.ceil(i))
i=i+0.01
plt.xlabel("x axis - input")
plt.ylabel("y axis - output = (numpy.ceil(input))")
plt.grid(linestyle='-', linewidth=0.5,color='red')
plt.plot(x,y, linewidth=3, color = 'black')
```

## Conclusion

After trying our ceiling function today, we found that it was easier to understand and learn. We tried different types of inputs and got different outputs. We understand better now. We will be back with some more exciting topics. Thank you.