CKTIS / C
ceil — C23 mathematical function
`ceil(x)` from `<math.h>` returns the smallest integer value not less than `double x`, still represented as `double`; `ceilf` and `ceill` preserve `float` and `long double`. This is rounding toward positive infinity.
Version: C23
Syntax
double ceil(double x);ceil(x) from <math.h> returns the smallest integer value not less than double x, still represented as double; ceilf and ceill preserve float and long double. This is rounding toward positive infinity.
Use it for an upper count of units such as pages or blocks, after performing floating-point division. The example shows the asymmetry: ceil(2.1) produces 3, while ceil(-2.1) produces -2.
The result remains floating-point; a later conversion to an integer type can overflow and needs a separate range check. Annex F/IEC 60559 guarantees an exact result independent of rounding mode, preserves the sign of ±0, and specifies ceil(+∞) = +∞.
Examples
#include <math.h>
#include <stdio.h>
int main(void) {
printf("%.0f %.0f\n", ceil(2.1), ceil(-2.1));
return 0;
}
Result
3 -2
Use it for an upper count of units such as pages or blocks, after performing floating-point division. The example shows the asymmetry: `ceil(2.1)` produces `3`, while `ceil(-2.1)` produces `-2`.