NumPy ceil()

The ceil() function rounds up floating point element(s) in an array to the nearest integer greater than or equal to the array element.

Example

import numpy as np

array1 = np.array([1.2, 2.7, 3.5, 4.8, 5.1])

# round up each element in array1 using ceil() result = np.ceil(array1)
print(result) # Output: [2. 3. 4. 5. 6.]

ceil() Syntax

The syntax of ceil() is:

numpy.ceil(array, out = None)

ceil() Arguments

The ceil() function takes following arguments:

  • array - the input array
  • out (optional) - the output array where the result is stored

ceil() Return Value

The ceil() function returns a new array with the rounded-up values.


Example 1: Use ceil() with 2D Array

import numpy as np

# create a 2D array
array1 = np.array([[1.2, 2.7, 3.5],
                      [4.8, 5.1, 6.3],
                      [7.2, 8.5, 9.9]])

# round up the elements in a 2D array with numpy.ceil() result = np.ceil(array1)
print("Rounded-up values:") print(result)

Output

Rounded-up values:
[[ 2.  3.  4.]
 [ 5.  6.  7.]
 [ 8.  9. 10.]]

Here, we have used the ceil() function to round up each element in array1.

The value 1.2 is rounded up to 2, the value 2.7 is rounded up to 3, and so on.

Note: The ceil() function returns an array with the same data type as the input array, and the resulting values are floating-point numbers representing the rounded up values.


Example 2: Create Different Output Array to Store Result

import numpy as np

# create an array
array1 = np.array([1.2, 2.7, 3.5, 4.9])

# create an empty array with the same shape as array1
result = np.zeros_like(array1)

# store the result of ceil() in out_array np.ceil(array1, out=result)
print(result)

Output

[2. 3. 4. 5.]

Here, the ceil() function is used with the out parameter set to result. This ensures that the result of applying ceil() function is stored in result.