For comparing floats they recommend using Mathf.Approximately
At the same time, they don't use it, for example, for comparing types such as vectors.
From Vector3.cs:
public bool Equals(Vector3 other)
{
return x == other.x && y == other.y && z == other.z;
}
If I understand it correctly it wouldn't cause any problem when using ==
operator to compare floats that are formed the same way (or copied from the same source). For example:
float x = 10f / 10f;
float a = x; // or `a = 10f / 10f`;
float b = x;
bool equal = (a == b); // always `true`
but it can cause a problem if floats are formed differently even if their results are supposed to be the same. For example:
float a = 10f / 10f;
float b = 11f - 10f;
bool equal = (a == b); // result is unpredictable
Is my understanding correct?
float.Epsilon
also shares the name epsilon, it's not a suitable choice for equality comparisons - it's absurdly tiny (about a sixtillionth of a septillionth). For typical vector calculations in games, your rounding error will be quite a bit larger than this, so you'll want a larger epsilon. A common metric is to think about the size of your unit in last place (or "ULP") - what's the value of the last binary digit in the numbers you're using? You'll want your epsilon to be greater than this to have any effect. – DMGregory Jun 13 '20 at 16:53