[英]C# mod fails in Unity 3D
I am performing the following, but instead of zRem
having the expected value of zero I get 2.384186E-07: 我正在执行以下操作,但不是
zRem
的期望值为零,而是得到2.384186E-07:
Vector3 t = this.transform.position - _startPosition;
float xRem = t.x % 1;
float zRem = t.z % 1;
In this instance: 在这种情况下:
this.transform.position = 2.5, 0, 3.5
_startPosition = 1.5, 0, 1.5
xRem
is correct, but zRem
have the value 2.384186E-07. xRem
是正确的,但zRem
的值为2.384186E-07。
Any suggestions on how to fix this? 对于如何解决这个问题,有任何的建议吗?
UPDATE #1 : When using the values above it should not enter this block, but because of zRem
it does. 更新#1 :使用上面的值时,不应输入此块,但是由于
zRem
原因,它应该输入。
if(!Mathf.Approximately(xRem, 0f) || !Mathf.Approximately(zRem, 0f))
{
return;
}
Well, mod is not failing... Float point arithmetics always involve some level of precision/error. 好吧,mod不会失败...浮点算法始终涉及一定程度的精度/错误。
I know, Mathf.Aproximately is meant to take ilthis into account, but Unity have its own Epsilon value for comparing floats and, guess what, it's value isn't documented. 我知道Mathf.Aproximly应该考虑到这一点,但是Unity拥有自己的Epsilon值来比较浮点数,并且猜测,它的值没有记录在案。
https://docs.unity3d.com/ScriptReference/Mathf.Epsilon.html https://docs.unity3d.com/ScriptReference/Mathf.Epsilon.html
(Maybe it is system dependent, according to the representation model of float values. Maybe it's IEEE conformant, I don't know) (根据浮点值的表示模型,它可能取决于系统。我不知道它是否符合IEEE)
Thing is: you should define what threshold use that conforms with your game logic, something like this: 问题是:您应该定义符合游戏逻辑的阈值用法,如下所示:
if(xRem <= threshold)
Or 要么
if(Mathf.abs(xRem) <= threshold)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.