C++ math ceil syntax
std::ceil(x)
In C++, std::ceil rounds toward positive infinity. The ceiling of 2.3 is 3, the ceiling of −2.3 is −2, and 2 stays 2.
std::ceil
#include <iostream>
#include <cmath>
int main() {
std::cout << (std::ceil(2.3)) << '\n';
}
3
std::ceil(x)
| Input | An arithmetic value. Include <cmath>. |
|---|---|
| Result | A floating-point value. Integral input is treated as double. |
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 <iostream>
#include <math.h>
using namespace std;
int main(int argc, const char * argv[])
{
// Positive floating point value
cout << "The ceiling value of 1.0 is " << ceil(1.0) << endl;
cout << "The ceiling value of 1.1 is " << ceil(1.1) << endl;
cout << "The ceiling value of 1.2 is " << ceil(1.2) << endl;
cout << "The ceiling value of 1.3 is " << ceil(1.3) << endl;
cout << "The ceiling value of 1.4 is " << ceil(1.4) << endl;
cout << "The ceiling value of 1.5 is " << ceil(1.5) << endl;
cout << "The ceiling value of 1.6 is " << ceil(1.6) << endl;
cout << "The ceiling value of 1.7 is " << ceil(1.7) << endl;
cout << "The ceiling value of 1.8 is " << ceil(1.8) << endl;
cout << "The ceiling value of 1.9 is " << ceil(1.9) << endl;
cout << "The ceiling value of 2.0 is " << ceil(2.0) << endl;
return 0;
}
The ceiling value of 1.0 is 1 The ceiling value of 1.1 is 2 The ceiling value of 1.2 is 2 The ceiling value of 1.3 is 2 The ceiling value of 1.4 is 2 The ceiling value of 1.5 is 2 The ceiling value of 1.6 is 2 The ceiling value of 1.7 is 2 The ceiling value of 1.8 is 2 The ceiling value of 1.9 is 2 The ceiling value of 2.0 is 2
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 <iostream>
#include <math.h>
using namespace std;
int main(int argc, const char * argv[])
{
// Positive floating point value
cout << "The ceiling value of 1.5698 is " << ceil(1.5698) << endl;
cout << "The ceiling value of 1.3093 is " << ceil(1.3093) << endl;
cout << "The ceiling value of 1.0087 is " << ceil(1.0087) << endl;
cout << "The ceiling value of 1.1038 is " << ceil(1.1038) << endl;
cout << "The ceiling value of 1.9573 is " << ceil(1.9573) << endl;
cout << "The ceiling value of 1.7462 is " << ceil(1.7462) << endl;
cout << "The ceiling value of 1.6427 is " << ceil(1.6427) << endl;
cout << "The ceiling value of 1.4128 is " << ceil(1.4128) << endl;
cout << "The ceiling value of 1.8099 is " << ceil(1.8099) << endl;
cout << "The ceiling value of 1.2743 is " << ceil(1.2743) << endl;
cout << "The ceiling value of 2.0001 is " << ceil(2.0001) << endl;
return 0;
}
The ceiling value of 1.5698 is 2 The ceiling value of 1.3093 is 2 The ceiling value of 1.0087 is 2 The ceiling value of 1.1038 is 2 The ceiling value of 1.9573 is 2 The ceiling value of 1.7462 is 2 The ceiling value of 1.6427 is 2 The ceiling value of 1.4128 is 2 The ceiling value of 1.8099 is 2 The ceiling value of 1.2743 is 2 The ceiling value of 2.0001 is 3
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 <iostream>
#include <math.h>
using namespace std;
int main(int argc, const char * argv[])
{
// Positive floating point value
cout << "The ceiling value of -1.5698 is " << ceil(-1.5698) << endl;
cout << "The ceiling value of -1.3093 is " << ceil(-1.3093) << endl;
cout << "The ceiling value of -1.0087 is " << ceil(-1.0087) << endl;
cout << "The ceiling value of -1.1038 is " << ceil(-1.1038) << endl;
cout << "The ceiling value of -1.9573 is " << ceil(-1.9573) << endl;
cout << "The ceiling value of -1.7462 is " << ceil(-1.7462) << endl;
cout << "The ceiling value of -1.6427 is " << ceil(-1.6427) << endl;
cout << "The ceiling value of -1.4128 is " << ceil(-1.4128) << endl;
cout << "The ceiling value of -1.8099 is " << ceil(-1.8099) << endl;
cout << "The ceiling value of -1.2743 is " << ceil(-1.2743) << endl;
cout << "The ceiling value of -2.0001 is " << ceil(-2.0001) << endl;
return 0;
}
The ceiling value of -1.5698 is -1 The ceiling value of -1.3093 is -1 The ceiling value of -1.0087 is -1 The ceiling value of -1.1038 is -1 The ceiling value of -1.9573 is -1 The ceiling value of -1.7462 is -1 The ceiling value of -1.6427 is -1 The ceiling value of -1.4128 is -1 The ceiling value of -1.8099 is -1 The ceiling value of -1.2743 is -1 The ceiling value of -2.0001 is -2
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 <iostream>
#include <math.h>
#include <limits>
using namespace std;
int main(int argc, const char * argv[])
{
// Positive ∞ infinite value
cout << "The ceiling value of inf is " << ceil(std::numeric_limits<float>::infinity()) << endl;
// Negative ∞ infinite value
cout << "The ceiling value of -inf is " << ceil(-std::numeric_limits<float>::infinity()) << endl;
return 0;
}
The ceiling value of inf is inf The ceiling value of -inf 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 <cmath>
#include <iostream>
int main() {
int passengers = 73, seats = 24;
auto buses = std::ceil(static_cast<double>(passengers) / seats);
std::cout << buses << " minibuses";
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