C math ceil syntax
double ceil(double x);
In C, ceil rounds toward positive infinity. The ceiling of 2.3 is 3, the ceiling of −2.3 is −2, and 2 stays 2.
ceil
#include <stdio.h>
#include <math.h>
int main(void) {
printf("%.15g\n", (double) (ceil(2.3)));
}
3
double ceil(double x);
| Input | A double. Include <math.h>. |
|---|---|
| Result | A double with an integral value for finite input. |
A test bench increases a reading by one tenth of a unit at a time. Watch where the rounded display changes as the sample passes from one integer toward the next.
#include <stdio.h>
#include <math.h>
int main(int argc, const char * argv[])
{
// Positive floating point value
printf("The ceiling value of 1.0 is %f \n", ceil(1.0));
printf("The ceiling value of 1.1 is %f \n", ceil(1.1));
printf("The ceiling value of 1.2 is %f \n", ceil(1.2));
printf("The ceiling value of 1.3 is %f \n", ceil(1.3));
printf("The ceiling value of 1.4 is %f \n", ceil(1.4));
printf("The ceiling value of 1.5 is %f \n", ceil(1.5));
printf("The ceiling value of 1.6 is %f \n", ceil(1.6));
printf("The ceiling value of 1.7 is %f \n", ceil(1.7));
printf("The ceiling value of 1.8 is %f \n", ceil(1.8));
printf("The ceiling value of 1.9 is %f \n", ceil(1.9));
printf("The ceiling value of 2.0 is %f \n", ceil(2.0));
return 0;
}
The ceiling value of 1.0 is 1.000000 The ceiling value of 1.1 is 2.000000 The ceiling value of 1.2 is 2.000000 The ceiling value of 1.3 is 2.000000 The ceiling value of 1.4 is 2.000000 The ceiling value of 1.5 is 2.000000 The ceiling value of 1.6 is 2.000000 The ceiling value of 1.7 is 2.000000 The ceiling value of 1.8 is 2.000000 The ceiling value of 1.9 is 2.000000 The ceiling value of 2.0 is 2.000000
A scheduling display groups positive measurements into whole-unit intervals. Compare readings just on either side of an integer to see which interval the display would select.
#include <stdio.h>
#include <math.h>
int main(int argc, const char * argv[])
{
// Positive floating point value
printf("The ceiling value of 1.5698 is %f \n", ceil(1.5698));
printf("The ceiling value of 1.3093 is %f \n", ceil(1.3093));
printf("The ceiling value of 1.0087 is %f \n", ceil(1.0087));
printf("The ceiling value of 1.1038 is %f \n", ceil(1.1038));
printf("The ceiling value of 1.9573 is %f \n", ceil(1.9573));
printf("The ceiling value of 1.7462 is %f \n", ceil(1.7462));
printf("The ceiling value of 1.6427 is %f \n", ceil(1.6427));
printf("The ceiling value of 1.4128 is %f \n", ceil(1.4128));
printf("The ceiling value of 1.8099 is %f \n", ceil(1.8099));
printf("The ceiling value of 1.2743 is %f \n", ceil(1.2743));
printf("The ceiling value of 2.0001 is %f \n", ceil(2.0001));
return 0;
}
The ceiling value of 1.5698 is 2.000000 The ceiling value of 1.3093 is 2.000000 The ceiling value of 1.0087 is 2.000000 The ceiling value of 1.1038 is 2.000000 The ceiling value of 1.9573 is 2.000000 The ceiling value of 1.7462 is 2.000000 The ceiling value of 1.6427 is 2.000000 The ceiling value of 1.4128 is 2.000000 The ceiling value of 1.8099 is 2.000000 The ceiling value of 1.2743 is 2.000000 The ceiling value of 2.0001 is 3.000000
A temperature logger records values just below zero or another integer boundary. Follow the rounding direction on the negative side before using the result as the label of a chart interval.
#include <stdio.h>
#include <math.h>
int main(int argc, const char * argv[])
{
// Negative floating point value
printf("The ceiling value of -1.5698 is %f \n", ceil(-1.5698));
printf("The ceiling value of -1.3093 is %f \n", ceil(-1.3093));
printf("The ceiling value of -1.0087 is %f \n", ceil(-1.0087));
printf("The ceiling value of -1.1038 is %f \n", ceil(-1.1038));
printf("The ceiling value of -1.9573 is %f \n", ceil(-1.9573));
printf("The ceiling value of -1.7462 is %f \n", ceil(-1.7462));
printf("The ceiling value of -1.6427 is %f \n", ceil(-1.6427));
printf("The ceiling value of -1.4128 is %f \n", ceil(-1.4128));
printf("The ceiling value of -1.8099 is %f \n", ceil(-1.8099));
printf("The ceiling value of -1.2743 is %f \n", ceil(-1.2743));
printf("The ceiling value of -2.0001 is %f \n", ceil(-2.0001));
return 0;
}
The ceiling value of -1.5698 is -1.000000 The ceiling value of -1.3093 is -1.000000 The ceiling value of -1.0087 is -1.000000 The ceiling value of -1.1038 is -1.000000 The ceiling value of -1.9573 is -1.000000 The ceiling value of -1.7462 is -1.000000 The ceiling value of -1.6427 is -1.000000 The ceiling value of -1.4128 is -1.000000 The ceiling value of -1.8099 is -1.000000 The ceiling value of -1.2743 is -1.000000 The ceiling value of -2.0001 is -2.000000
A sensor can use infinity to mark a reading outside its measuring range. Pass both signs through this operation so the display can handle the result or the reported error deliberately.
#include <stdio.h>
#include <math.h>
int main(int argc, const char * argv[])
{
// Positive ∞ infinite value
printf("The ceiling value of INFINITY is %f \n", ceil(INFINITY));
// Negative ∞ infinite value
printf("The ceiling value of -INFINITY is %f \n", ceil(-INFINITY));
}
The ceiling value of INFINITY is inf The ceiling value of -INFINITY is -inf
A teacher has 73 passengers on the museum trip, including the adults. Each minibus seats 24. She needs enough vehicles for the last passenger as well as the first 72.
#include <stdio.h>
#include <math.h>
int main(void) {
int passengers = 73, seats = 24;
double buses = ceil((double) passengers / seats);
printf("%.0f minibuses", buses);
return 0;
}
4 minibuses
Three minibuses seat 72 people. Dividing before rounding up reserves the fourth vehicle for the remaining passenger.
Reference: C numeric operations