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.| Parameter | Description |
|---|---|
x | Input array or numeric data-type |
out | Optional, ndarray to store the result; must match expected output shape |
where | Optional, boolean array mask; elements where True are evaluated, False left untouched |
dtype | Optional, data-type override for output |
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.]
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. |
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.]
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. ]
| 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 |
np.ceil() returns floating-point arrays even if inputs represent integers, preserving floating representation.Test ceil() with arrays, preallocated buffers, boolean masks, and run verification test suites in our interactive Google Colab companion notebook:
Launch the notebook directly in Google Colab to run live code cells or inspect the source repository on GitHub:
View on GitHubContinue exploring related math functions: floor(), round(), rad2deg(), and Math & ufuncs.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.