Tag: 地板

为什么很多(旧)程序使用floor(0.5 + input)而不是round(input)?

这个差异就在于我所相信的返回值给出的input是相信的,比如这个代码 : 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 但是最后他们只是不同的结果,一个select了自己喜欢的结果。 我看到许多使用floor(0.5 + input)而不是round(input)的“旧”C / C ++程序。 有什么历史原因吗? 在CPU上最便宜?