Round requirements upward
Convert measured or computed quantities into conservative whole-unit requirements, such as allocating enough capacity for a fractional count.
NumPy · Function
Returns the ceiling of an input element-wise: each value becomes the smallest integer greater than or equal to it. Scalar inputs produce scalars; array inputs produce floating-point arrays.
np.ceil applies the mathematical ceiling element by element: each value becomes the smallest integer not less than that value. It accepts scalars or array-like inputs, returns a scalar for scalar input, and otherwise produces an array; results use floating-point dtype.
numpy.ceil(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True [, signature])| Name | Type | Description |
|---|---|---|
x | array_like | Input value or array whose ceiling is computed. |
out | ndarray, None, or tuple of ndarray and None | Optional destination array for the result. |
where | array_like of bool | Boolean mask selecting where computation occurs. |
casting | {'no', 'equiv', 'safe', 'same_kind', 'unsafe'} | Controls permitted casting for the operation. |
order | {'K', 'A', 'C', 'F'} | Memory layout order for the output. |
dtype | dtype, None | Output dtype. |
subok | bool | Whether subclasses of ndarray are preserved. |
signature | string, None | Optional generalized-ufunc signature. |
ndarray or scalar: the ceiling of each element in x, with float dtype; scalar input produces a scalar.
np.ceil include floor, trunc, rint, and fix.import numpy as np
values = np.array([-1.7, -0.2, 0.2, 1.5])
print(np.ceil(values))
# [-1. -0. 1. 2.]Rounds each array element upward to the smallest integer that is greater than or equal to it.
Convert measured or computed quantities into conservative whole-unit requirements, such as allocating enough capacity for a fractional count.
Apply ceiling to every element of an ndarray without writing an explicit Python loop.
Use out or where when integrating the calculation into an existing array-processing pipeline.
np.ceil compute, and how does it behave on arrays?np.ceil is a NumPy universal function that returns the ceiling element-wise. For a scalar x, the result is the smallest integer i satisfying i >= x; for an array, the operation is applied independently to every element.
Follow-up: How does it differ from np.floor? ceil rounds toward positive infinity, whereas floor rounds toward negative infinity, so negative fractional values distinguish them.
The function accepts x plus optional ufunc controls such as out, where, casting, order, dtype, and subok. The out argument can be used to place results into a supplied output array.
Follow-up: What should be checked when using where with an output buffer? Ensure positions not selected by where are initialized if their values will be read, because they are not newly computed by the operation.
For a scalar input, np.ceil returns a scalar; for array input, it returns an array containing the ceiling of each element. The documented result has floating-point dtype.
Follow-up: Why can -0.2 display as -0.? IEEE floating-point represents signed zero, and ceiling preserves that representation in this case even though it compares equal to zero.
numpy.floornumpy.truncnumpy.rintnumpy.fix