在C++中,可以使用cmath頭文件中的ceil函數(shù)來向上取整。ceil函數(shù)的原型如下:
double ceil(double x);
該函數(shù)接受一個浮點數(shù)參數(shù)x,并返回不小于x的最小整數(shù)值。
以下是ceil函數(shù)的用法示例:
#include <iostream>
#include <cmath>
int main() {
double x = 3.7;
double result = ceil(x);
std::cout << "Ceiling value of " << x << " is " << result << std::endl;
return 0;
}
輸出結(jié)果為:
Ceiling value of 3.7 is 4
在上面的示例中,我們使用ceil函數(shù)將3.7向上取整到4。