algorithm - Mathf.RoundToInt() 的行为

标签 algorithm unity-game-engine

我有一个关于 Mathf.RoundToInt() 的问题

int val = Mathf.RoundToInt(10.5f);
val = Mathf.RoundToInt(9.5f);
val = Mathf.RoundToInt(8.5f);

上面生成的是“10,10,8”而不是“11,10,9”。

根据Unity的引用手册,当小数点后第一位为“5”时(例如8.5 -> 8.0),Mathf.RoundToInt() 返回“偶数”。

我想知道为什么 Mathf.RoundToInto() 像这样返回,而不是像正常舍入那样返回(例如 8.5 -> 9.0、9.5 -> 10.0)。

最佳答案

来自http://en.wikipedia.org/wiki/Rounding#Round_half_to_even :

For reasonable distributions of [...] values, the expected (average) value of the rounded numbers is the same as that of the original numbers.

关于algorithm - Mathf.RoundToInt() 的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19671523/

相关文章:

c# - 查找 List<int> 中最长的序列

algorithm - 如何检测添加 block 后永远不会成为最短路径一部分的网格上的正方形?

algorithm - 我如何评估节点的连通性?

unity-game-engine - 由于在unity3d iOs 游戏中的应用程序使用看门狗定时器而被Apple 拒绝应用程序?

algorithm - 谁知道石头和背包的算法?

algorithm - Prolog 递归和构建递归调用的输出

unity-game-engine - Unity 中的脚本如何工作?

javascript - crypto-js 在 Unity 中抛出编译错误

android - Android 手机上的 vuforia unity 项目显示三角形碎屏

Unity中的Android本地通知