在C++中,可以使用ceil()
函數(shù)來實(shí)現(xiàn)向上取整。ceil()
函數(shù)定義在<cmath>
頭文件中。
以下是使用ceil()
函數(shù)的示例:
#include <iostream>
#include <cmath>
int main() {
double num1 = 3.14;
double num2 = 5.6;
double ceilNum1 = ceil(num1);
double ceilNum2 = ceil(num2);
std::cout << "Ceiling of " << num1 << " is " << ceilNum1 << std::endl;
std::cout << "Ceiling of " << num2 << " is " << ceilNum2 << std::endl;
return 0;
}
輸出結(jié)果為:
Ceiling of 3.14 is 4
Ceiling of 5.6 is 6
在示例中,ceil()
函數(shù)用于向上取整num1
和num2
的值。ceil()
函數(shù)接受一個浮點(diǎn)數(shù)作為參數(shù),并返回大于或等于該浮點(diǎn)數(shù)的最小整數(shù)。