The ceil()
function in C++ returns the smallest possible integer value which is greater than or equal to the given argument.
It is defined in the cmath header file.
Example
#include <iostream>
#include <cmath>
using namespace std;
int main() {
// find the smallest possible integer value >= 15.08
cout << ceil(15.08);
return 0;
}
// Output: 16
ceil() Syntax
The syntax of the ceil()
function is:
ceil(double num);
ceil() Parameters
The ceil()
function takes the following parameter:
- num - floating-point number whose ceiling value is to be computed
ceil() Return Value
The ceil()
function returns:
- the smallest possible integer value which is greater than or equal to num
ceil() Prototypes
The prototypes for ceil()
as defined in the cmath header file are:
double ceil(double num);
float ceil(float num);
long double ceil(long double num);
// for integral types
double ceil(T num);
Example 1: C++ ceil()
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double num = 10.25;
double result = ceil(num);
cout << "Ceil of " << num << " = " << result;
return 0;
}
Output
Ceil of 10.25 = 11
Example 2: C++ ceil() for Integral Types
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int num = 15;
double result = ceil(num);
cout << "Ceil of " << num<< " = " << result;
return 0;
}
Output
Ceil of 15 = 15
We will always get the same result for integral types. So this function is not used with integral types in practice.
Also Read: