C++11 引入了 std::nearbyint 和 std::round 函数。两者都返回“最近”的整数值。
什么时候应该使用其中的一种?
我已经使用值为 0.5
进行了测试:
案例1 : nearbyint演示
#include <iostream>
#include <cmath>
int main()
{
std::cout<<"nearbyint(0.5) = "<<std::nearbyint(0.5);
}
输出:0
情况2:round的演示
#include <iostream>
#include <cmath>
int main()
{
std::cout<<"round(0.5) = "<<std::round(0.5);
}
输出:1
为什么这两个输出不同呢?