【问题标题】:Trying to understand Mathf.Approximately [duplicate]试图理解 Mathf.Approximately [重复]
【发布时间】:2022-01-17 07:24:53
【问题描述】:

当 Mathf.Approximately 给出 true 时,有人可以举一个最简单的例子吗?

尝试以下,总是错误的:

Debug.Log(Mathf.Approximately(0.0001f, 0.0f));  // False
Debug.Log(Mathf.Approximately(0.00000001f, 0.0f));  // False
Debug.Log(Mathf.Approximately(0.00000000000001f, 0.0f));  // False

【问题讨论】:

标签: c# unity3d precision equals


【解决方案1】:

documentation 表示如果它们“在一个非常小的值 (epsilon) 内”它们是相等的。

Epsilon is documented 为“浮点数可以具有不同于零的最小值”。

那有多小? FloatMinNormalFloatMinDenormal 取决于 IsFlushToZeroEnabled,如您所见,from source code 大约为 1e-38,0.00000000000001f 小很多

对于非零值,精确计算取决于数字的数量级,you can check out the source code for the precise formula

有些数字(例如 0.1)不能准确地用浮点数表示。这就是为什么Approximately 比直接相等更有用的原因。然而,这是一个复杂的话题,需要的不仅仅是一个 SO 答案来充分发展。如果您想了解更多信息,不妨从这里开始:https://floating-point-gui.de/

【讨论】:

  • 谢谢,这让我对这个话题更清楚了。
猜你喜欢
  • 1970-01-01
  • 2018-01-13
  • 2019-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-28
  • 2018-04-17
  • 1970-01-01
相关资源
最近更新 更多