ceil()函数ceil()函数是cmath标头的库函数,用于查找给定数字的向上舍入值,它接受一个数字并返回不小于给定数字的最小整数值。
ceil()函数语法:
ceil(x);
参数: x –是要向上舍入的数字。
返回值: double-返回double类型值,该值是给定数字的四舍五入值。
示例
Input: float x = 2.3; Function call: ceil(x); Output: 3 Input: float x = 3.8 Function call: ceil(x); Output: 4
ceil()函数示例//示例
// ceil()功能
#include <iostream>
#include <cmath>
using namespace std;
// main()部分
int main(){
float x;
//输入号码
cout<<"Enter a float value: ";
cin>>x;
//打印舍入值
cout<<"ceil("<<x<<"): "<<ceil(x)<<endl;
return 0;
}输出结果
First run: Enter a float value: 2.3 ceil(2.3): 3 Second run: Enter a float value: 3.8 ceil(3.8): 4 Third run: Enter a float value: -2.3 ceil(-2.3): -2 Fourth run: Enter a float value: -3.8 ceil(-3.8): -3