NumPy ceil()

The numpy.ceil() function returns the ceiling value of each element in an input array. The ceiling of a number is the smallest integer greater than or equal to that number.

Syntax

</>
Copy
numpy.ceil(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)

Parameters

ParameterTypeDescription
xarray_likeInput array containing numerical values.
outndarray, None, or tuple of ndarray and None, optionalOptional output array where the result is stored. If None, a new array is created.
wherearray_like, optionalBoolean mask specifying which elements to compute. Elements where where=False retain their original value.
castingstr, optionalDefines the casting behavior when computing the ceiling function.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalDefines the data type of the output array.
subokbool, optionalDetermines if subclasses of ndarray are preserved in the output.

Return Value

Returns an array with the ceiling values of the input elements. If the input is a scalar, a scalar is returned.


Examples

1. Computing the Ceiling of a Single Value

Here, we compute the ceiling of a single floating-point number.

</>
Copy
import numpy as np

# Define a floating-point number
num = 3.7

# Compute the ceiling of the number
result = np.ceil(num)

# Print the result
print("Ceiling of 3.7:", result)

Output:

Ceiling of 3.7: 4.0

2. Computing the Ceiling for an Array

We compute the ceiling values for multiple floating-point numbers in an array.

</>
Copy
import numpy as np

# Define an array of floating-point numbers
arr = np.array([1.2, 2.8, -3.5, 4.0, -0.9])

# Compute the ceiling of each element
ceil_values = np.ceil(arr)

# Print the results
print("Input array:", arr)
print("Ceiling values:", ceil_values)

Output:

Input array: [ 1.2  2.8 -3.5  4.  -0.9]
Ceiling values: [ 2.  3. -3.  4.  0.]

3. Using the out Parameter

Using an output array to store results instead of creating a new array.

</>
Copy
import numpy as np

# Define an array of floating-point numbers
arr = np.array([2.1, -4.7, 5.5, 0.3])

# Create an output array with the same shape
output_array = np.empty_like(arr)

# Compute ceiling values and store in output_array
np.ceil(arr, out=output_array)

# Print the results
print("Ceiling values stored in output array:", output_array)

Output:

Ceiling values stored in output array: [ 3. -4.  6.  1.]

4. Using the where Parameter

Using a condition to compute the ceiling only for selected elements.

</>
Copy
import numpy as np

# Define an array of floating-point numbers
arr = np.array([1.4, -2.9, 3.6, -1.2])

# Define a mask (compute ceiling only where mask is True)
mask = np.array([True, False, True, False])

# Compute ceiling values where mask is True
result = np.ceil(arr, where=mask)

# Print the results
print("Ceiling values with mask applied:", result)

Output:

Ceiling values with mask applied: [2. 0. 4. 0.]

The ceiling values are computed only for elements where mask=True. The other values remain unchanged.