NumPy ceil(): Ceiling Function for Array Elements

import numpy as np

ar = np.array([1.57, 2.09, 2.79, -3.41, -3.62])
result = np.ceil(ar)
print(result)  # [ 2.  3.  3. -3. -3.]
numpy.ceil(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) is a universal function (ufunc) that returns an element-wise ceiling of the input, defined as the smallest integer greater than or equal to each element.
ParameterDescription
xInput array or numeric data-type
outOptional, ndarray to store the result; must match expected output shape
whereOptional, boolean array mask; elements where True are evaluated, False left untouched
dtypeOptional, data-type override for output

Basic ceil() Examples 🔝

The ceiling function rounds numbers up toward positive infinity. Notice how negative numbers move closer to zero (e.g., -3.41 rounds up to -3.0):

import numpy as np

ar = np.array([1.57, 2.09, 2.79, -3.41, -3.62])
res = np.ceil(ar)
print(res)

assert np.array_equal(res, np.array([2.0, 3.0, 3.0, -3.0, -3.0]))

Expected output

[ 2.  3.  3. -3. -3.]

Legacy vs Modernized Code Comparison 🔝

The table below highlights how legacy scripts compare against the modernized, structured implementation:

Aspect Legacy Implementation Modernized Implementation
Structure & Markup Basic echo output with unformatted pre tags and missing metadata. Structured HTML5 layout, Schema TechArticle markup, and responsive tables.
Above-the-Fold Answer Requires scrolling past introductory text to find code examples. Core code snippet placed right at the top for instant visitor access.
Verification Unchecked print statements with manual result inspection comments. Automated unit test assertions (assert) verifying array outputs.
Ecosystem Integration Isolated code blocks with sparse navigation links. Comprehensive cross-linking, quick-jump navigation, and runnable Colab notebooks.

Using out Buffer 🔝

Alternative output arrays can be preallocated using empty_like() to eliminate runtime memory allocation overhead in production loops:

ar = np.array([1.57, 2.09, 2.79, -3.41, -3.62])
ar_out = np.empty_like(ar)
np.ceil(ar, out=ar_out)
print("Preallocated out buffer:", ar_out)

assert np.array_equal(ar_out, np.array([2.0, 3.0, 3.0, -3.0, -3.0]))

Expected output

Preallocated out buffer: [ 2.  3.  3. -3. -3.]

Using where Mask 🔝

The where parameter allows selective execution of the ceiling function across boolean array masks:

ar = np.array([1.57, 2.09, 2.79, -3.41, -3.62])
ar_where = np.array([True, False, True, False, True])

masked_res = np.ceil(ar, where=ar_where)
print("Masked ceil result:", masked_res)

Expected output (Indices 1 and 3 are left un-evaluated)

Masked ceil result: [ 2.    2.09  3.   -3.41 -3.  ]

Comparison with floor() and trunc() 🔝

Function Behavior Example input 2.7 Example input -2.7
np.ceil() Rounds up toward positive infinity 3.0 -2.0
np.floor() Rounds down toward negative infinity 2.0 -3.0
np.trunc() Truncates fractional digits toward zero 2.0 -2.0

Common Pitfalls 🔝

  • Floating-Point Dtype Retention: np.ceil() returns floating-point arrays even if inputs represent integers, preserving floating representation.
  • Negative Number Direction: Remember that ceiling rounds towards zero for negative numbers (e.g. ceil(-1.2) is -1.0).

Practice in Google Colab 🔝

Test ceil() with arrays, preallocated buffers, boolean masks, and run verification test suites in our interactive Google Colab companion notebook:

Interactive Practice: Colab & GitHub

Launch the notebook directly in Google Colab to run live code cells or inspect the source repository on GitHub:

View on GitHub Open In Colab

Continue exploring related math functions: floor(), round(), rad2deg(), and Math & ufuncs.




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer