I am performing the following, but instead of zRem
having the expected value of zero I get 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.
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.
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.
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.
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)
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.