English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ Usage and example of ceil() function

C++ Library Function <cmath>

C ++The ceil(x) function returns the smallest integer greater than or equal to x.

ceil() prototype [from C ++ 11[Standard Start]

double ceil(double x);
float ceil(float x);
long double ceil(long double x);
double ceil(T x); //is for integer

C ++The minimum possible integer value returned by ceil() function in<cmath>Defined in the header file.

ceil() parameter

The maximum value of the parameter used by ceil() function is calculated.

ceil() return value

The minimum possible integer value returned by ceil() function is greater than or equal to the given parameter.

Example1: Used for ceil() function of double, float, and long double types

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    double x = 10.25, result;
    result = ceil(x);
    cout << "Ceil of " << x << " = " << result << endl;
    return 0;
}

When the program is run, the output is:

Ceil of 10.25 = 11

Example2: Integer type ceil() function

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    int x = 15;
    double result;
    result = ceil(x);
    cout << "Ceil of " << x << " = " << result << endl;
    return 0;
}

When the program is run, the output is:

Ceil of 15 = 15

For integer types, you will always get the same result, so this function is actually not used for integer types.

C++ Library Function <cmath>