## Introduction to the Ceiling Function

Have you ever needed to round up to the nearest whole number? That’s exactly what the ceiling function does! The ceiling function is used to get the integer value of the fractional or decimal value.

This function is very useful in programming languages and is often used in mathematical calculations.

## Definition and Examples of Ceiling Function

The ceiling function is a mathematical function that takes a real number as input and rounds it up to the nearest integer. This function returns the smallest integer that is greater than or equal to the input value.

## Here are some examples of the ceiling function in action:

– For positive values, the ceiling of 3.5 is 4. – For negative values, the ceiling of -3.5 is -3.

– For 0, the ceiling is also 0. Ceiling function can also be applied to arrays.

When an array is passed through the ceiling function, each element of that array is rounded up to the nearest integer. Implementing Numpy.ceil() Method

Numpy is a popular Python library that is used for mathematical computations.

The ceil() function is part of this library and provides us the functionality to get the ceiling value of any number. Syntax of Numpy.ceil()

numpy.ceil(x, *args, **kwargs)

Once the function name is called, it requires one argument (in this case, x) to be given.

This argument is the value of which you wish to get the ceiling value.

## Ceiling Function for Positive Values

The ceiling function in numpy.ceil() works well for positive values. If we pass a positive value through the ceil() function, it simply returns the next integer value.

Here’s an example:

## import numpy as np

a = 4.8

print(“Output:”, np.ceil(a))

## Output:

5.0

The output confirms that ceil() returns the next integer value of the provided positive value.

## Ceiling Function for Negative Values

The ceil() function works slightly differently for negative values. We must keep in mind that the function returns the next integer value that is greater than or equal to the given value.

Therefore, if we provide a negative value as the argument, the ceil() function works as shown below:

## import numpy as np

b = -4.3

print(“Output:”, np.ceil(b))

## Output:

-4.0

Notice that even though a negative value was provided, the ceil() function returned -4, the next integer that is greater than or equal to -4.3.

## Ceiling Function for an Array

One of the coolest features of numpy is that it works well with arrays. In the following example, we pass an array through the ceil() function using numpy.

## This function is then able to round up each element in the array to the nearest integer:

## import numpy as np

arr = np.array([3.2, 4.5, 6.7, 7.2, 8.1, 9.9])

print(“Output Array:”, np.ceil(arr))

## Output:

[ 4. 5.

7. 8.

9. 10.]

## Ceiling Function for Complex Numbers

The ceil() function does not work well on complex numbers. Numpy will return an error message if we attempt to proceed with a complex number:

## import numpy as np

c = 3 + 4j

print(“Output:”, np.ceil(c))

## Output:

TypeError: can’t convert complex to float

## Conclusion

The ceiling function is a powerful and useful tool in Python programming when working with numbers. It is essential to be aware of how the function works, especially with arrays and complex numbers, to make best use of this function.

By understanding the different syntax, input parameters, and outputs that can arise, this function can easily become a valuable asset to any programmer. In the previous section, we discussed the ceiling function and how it can be implemented using the numpy library in Python.

In this section, we will explore how to plot the output of the numpy.ceil() function on a graph. Creating Empty Arrays x[] and y[]

To plot the output of the ceil() function, we first need to create two empty arrays x[] and y[].

The array x[] will store the input values for the ceil() function, and the array y[] will store the output values.

## import numpy as np

import matplotlib.pyplot as plt

x = np.empty(0)

y = np.empty(0)

## Loading Input Values Using While Loop

Next, we can use a while loop to load input values into the x[] array. The input values can be any real number.

For this example, we will use numbers ranging from -10 to 10. i = -10

while i <= 10:

x = np.append(x, i)

i += 0.1

Now that we have the input values in the x[] array, we can use the ceil() function to get the output values.

We will append these output values to the y[] array. for i in x:

y = np.append(y, np.ceil(i))

Plotting Graph Using matplotlib.pyplot Library

We can use the matplotlib.pyplot library to plot the graph of the ceil() function output.

The pyplot library is a powerful tool in Python for data visualization. plt.plot(x, y)

plt.xlabel(‘Input Values’)

plt.ylabel(‘Ceiling Values’)

plt.title(‘Graph of Ceiling Function’)

plt.show()

The first line of code uses the plot() function in pyplot to create the graph of the output values of the ceil() function.

The plot() function takes in the two arrays x[] and y[], and it will plot the y[] values against the corresponding x[] values. The following lines of code then label the x-axis as ‘Input Values’, the y-axis as ‘Ceiling Values’, and give the plot a title.

Finally, the show() function is called to display the graph on the screen. Here is the complete code example:

## import numpy as np

import matplotlib.pyplot as plt

x = np.empty(0)

y = np.empty(0)

i = -10

while i <= 10:

x = np.append(x, i)

i += 0.1

## for i in x:

y = np.append(y, np.ceil(i))

plt.plot(x, y)

plt.xlabel(‘Input Values’)

plt.ylabel(‘Ceiling Values’)

plt.title(‘Graph of Ceiling Function’)

plt.show()

When you run this code, you will see a graph of the ceil() function output that ranges from -10 to 10 on the x-axis and from -10 to 11 on the y-axis.

## Conclusion

In this section, we explored how to plot the output of the ceil() function on a graph using the numpy and matplotlib.pyplot libraries in Python. By creating empty arrays, loading input values using a while loop, and plotting the graph using the plot() function in pyplot, we were able to visualize the output of the ceil() function.

The ability to plot data is a powerful tool for data analysis and visualization and is a skill that every programmer should have in their toolbox. In this article, we discussed the ceiling function and how it can be implemented using the numpy library in Python.

We explored the syntax of the ceil() function and how it works for positive, negative, and array values. Moreover, we learned how to plot the output of the ceil() function on a graph using the matplotlib.pyplot library.

The ability to work with the ceil() function and plot graphs is an essential skill that every programmer should have in their toolbox. By using this function, we can easily round up decimal values to the nearest whole number, which can come in handy in many programming applications.