Ceiling function (source code)

= Ceiling function
{title2=$\lceil x\rceil$}

The ceiling function gives the least <integer> large enough to meet a real lower bound. For a <real number> $x$,
$$
\lceil x\rceil=m\quad\Longleftrightarrow\quad m-1<x\leq m,\qquad m\in\mathbb Z.
$$
Thus $\lceil2.7\rceil=3$, $\lceil-2.7\rceil=-2$, and $\lceil2\rceil=2$. The operation rounds upward in the <total order> on the <real numbers>, which can move a negative input towards zero. Its difference from the input obeys $0\leq\lceil x\rceil-x<1$. Python's `math.ceil` implements this least-integer definition. https://docs.python.org/3.14/library/math.html#math.ceil[Python 3.14 math.ceil documentation: the least integer at least x]{ref}.

The <ceiling count for fixed-size batches> is a direct application. Suppose each box holds at most $k$ items, with positive <integer> $k$, and there are $N\geq0$ items. The smallest number of boxes with enough capacity is $\lceil N/k\rceil$: a candidate count $b$ must satisfy $bk\geq N$. For $N=101$, $k=20$, \b[six boxes are needed], leaving $120-101=19$ unused places. Exactly 100 items need only five boxes, and zero items need zero boxes. Adding one to the <floor function> result unconditionally would therefore overcount at exact multiples of $k$.

\Image[/ceiling-function-thresholds-and-capacity.png]
{title=The <ceiling function> retains the endpoint on the left and counts enough whole containers}
{description=Left: open and filled endpoints distinguish the half-open <intervals> $(m-1,m]$ where $\lceil x\rceil=m$, including negative inputs. Right: the original capacity example uses 20 items per box; 101 items need six boxes, while an exact multiple of 20 needs no extra box. The right-hand inputs are integer item counts.}

For nonnegative <integer> $N$ and positive <integer> $k$, <Euclidean division> gives the exact identity
$$
\boxed{\left\lceil\frac Nk\right\rceil=\left\lfloor\frac{N+k-1}{k}\right\rfloor.}
$$
Write $N=qk+r$, $0\leq r<k$. The count is $q$ when the <remainder> is zero and $q+1$ otherwise, and the right-hand side encodes these two cases. This argument specifies the domain of the identity; arbitrary negative denominators require their own sign conventions.

The <ceiling function> is a <nondecreasing function>, constant on each of the half-open <intervals> $(m-1,m]$, and has a <discontinuity of a function> at every <integer>. It is tied to the <floor function> by $\lceil x\rceil=-\lfloor-x\rfloor$. Adding an <integer> $j$ shifts its output by the same amount: $\lceil x+j\rceil=\lceil x\rceil+j$. At an <integer> the floor and ceiling agree; at every other input they differ by one. These statements refer to exact <real numbers>; an input already perturbed by <floating-point arithmetic> can land on the other side of a threshold.