为什么很多(旧)程序都使用floor(0.5 +输入)而不是round(输入)? [英] Why do lots of (old) programs use floor(0.5 + input) instead of round(input)?

查看:136
本文介绍了为什么很多(旧)程序都使用floor(0.5 +输入)而不是round(输入)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

差异在于返回值,我认为该值提供了打破平局的输入,例如此代码

The differences reside in the returned value giving inputs around tie-breaking I believe, such as this code:

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

但是它们只是最终结果不同,一个人选择了自己喜欢的一个。我看到很多使用 floor(0.5 +输入)而不是 round(输入)的旧 C / C ++程序。

But they are just different results in the end, one chooses its preferred one. I see lots of "old" C/C++ programs using floor(0.5 + input) instead of round(input).

是否有历史原因?在CPU上最便宜?

Is there any historic reason? Cheapest on the CPU?

推荐答案

std :: round 在C ++ 11中引入。在此之前,只有 std :: floor 可用,因此程序员正在使用它。

std::round is introduced in C++11. Before that, only std::floor was available so programmers were using it.

这篇关于为什么很多(旧)程序都使用floor(0.5 +输入)而不是round(输入)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆