不同之处在于返回的值提供了我相信的平局输入,例如 以下代码:
int main()
{
std::cout.precision(100);
double input = std::nextafter(0.05, 0.0) / 0.1;
double x1 = floor(0.5 + input);
double x2 = round(input);
std::cout << x1 << std::endl;
std::cout << x2 << std::endl;
}
输出:
1
0
但它们最终只是不同的结果,一个选择它喜欢的一个。我看到很多“旧”C/C++ 程序使用 floor(0.5 + input)
而不是 round(input)
。
有什么历史原因吗? CPU最便宜?
原文由 markzzz 发布,翻译遵循 CC BY-SA 4.0 许可协议
std::round
在C++11中引入。在此之前,只有std::floor
可用,因此程序员正在使用它。