C++ ceil() Function Explained | Round Up Numbers

The ceil function is a mathematical function in C++ that is used to return the smallest integer greater than or equal to a given floating-point number. The complete declaration of the ceil function is as follows:

double ceil(double x);

Parameter x is a floating point number, and the function returns a double value representing the smallest integer greater than or equal to x.

Here are some example uses of the ceil function:

#include <iostream>
#include <cmath>

int main() {
    double num1 = 3.4;
    double num2 = -2.8;
    
    std::cout << "ceil(3.4) = " << ceil(num1) << std::endl;
    std::cout << "ceil(-2.8) = " << ceil(num2) << std::endl;
    
    return 0;
}

The output result:

ceil(3.4) = 4
ceil(-2.8) = -2

In the examples above, ceil(3.4) returns 4 because 4 is the smallest integer greater than or equal to 3.4. ceil(-2.8) returns -2 because -2 is the smallest integer greater than or equal to -2.8.

bannerAds