无涯教程-nearbyint(x)函数

27 阅读1分钟

该函数使用当前的舍入方法将给定值舍入为附近的整数值。 fegetround描述了当前的舍入方法。

nearbyint - 语法

假设数字是“ x”。语法为:

return_type nearbyint(data_type x);

Note: return_type可以是float,double或long double。

nearbyint - 参数

x :该值可以是float或double。

nearbyint - 返回值

它使用舍入方法将x的舍入值返回到附近的整数值。

nearbyint - 例子1

让我们看一个简单的例子。

#include <iostream>  
#include<math.h>  
#include <cfenv>  
using namespace std;  
int main()  
{  
     float x=2.3;  
    std::cout << "Value of x is:" <<x<< std::endl;  
    cout<<"Rounding to nearby,value is :"<<nearbyint(x);  
    return 0;  
}  

输出:

Value of x is:2.3
Rounding to nearby,value is :2   

参考链接

www.learnfk.com/c++/cpp-mat…