我刚刚注意到以下代码返回true:
Mathf.Approximately(0.0f, float.Epsilon); // true
我已经阅读了Mathf.Approximately Documentation,它指出:
Mathf.Epsilon Documentation指出:
结果,我运行了以下代码,期望它是
false
,但它也返回true
。Mathf.Approximately(0.0f, 2.0f * float.Epsilon); // true
顺便一提:
Mathf.Approximately(0.0f, 2.0f * float.Epsilon); // true
Mathf.Approximately(0.0f, 3.0f * float.Epsilon); // true
Mathf.Approximately(0.0f, 4.0f * float.Epsilon); // true
Mathf.Approximately(0.0f, 5.0f * float.Epsilon); // true
Mathf.Approximately(0.0f, 6.0f * float.Epsilon); // true
Mathf.Approximately(0.0f, 7.0f * float.Epsilon); // true
Mathf.Approximately(0.0f, 8.0f * float.Epsilon); // false
Mathf.Approximately(0.0f, 9.0f * float.Epsilon); // false
问:基于这些证据,我可以确定地说
Mathf.Approximately
根据其文档没有正确实现*吗? (*因此,我应该转到其他解决方案,例如Floating point comparison functions for C#中的解决方案)
最佳答案
这是Unity的public static bool Mathf.Approximately(float a, float b);
的反编译代码
您可以在^^的末尾看到* 8.0f
,因此确实记录得很差。
/// <summary>
/// <para>Compares two floating point values if they are similar.</para>
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
public static bool Approximately(float a, float b)
{
return (double) Mathf.Abs(b - a) < (double) Mathf.Max(1E-06f * Mathf.Max(Mathf.Abs(a),
Mathf.Abs(b)), Mathf.Epsilon * 8.0f);
}
关于c# - Mathf.Aboutly(0.0f,float.Epsilon)== true是其正确行为吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58599418/