I have just noticed that the following code returns true:
Mathf.Approximately(0.0f, float.Epsilon); // true
I have read the Mathf.Approximately Documentation and it states that:
Approximately() compares two floats and returns true if they are within a small value (Epsilon) of each other.
And Mathf.Epsilon Documentation states that:
- anyValue + Epsilon = anyValue
- anyValue - Epsilon = anyValue
- 0 + Epsilon = Epsilon
- 0 - Epsilon = -Epsilon
As a result, I ran the following code, expecting it to be false
, but it also returns true
.
Mathf.Approximately(0.0f, 2.0f * float.Epsilon); // true
By the way:
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
Q: Based on that evidence, can I safely say that Mathf.Approximately
is not correctly implemented according to its documentation*?
(* and as a result, I should move to different solutions, such as the one in Floating point comparison functions for C#)
Mathf.Epsilon == float.Epsilon
, so it does appear that the documentation is a bit... loose with describing its algorithm (An 'approximation' only ;). But do you really need the approximation to be that precise? – EnvisageMathf.Approximately
function, that lead me to create the question here in Stack Overflow. – OmnivoreMathf.Approximately
so I can make an informed decision. – OmnivoreMathf.Epsilon == float.Epsilon
, that also states the documentation is a bit loose/the threshold is undocumented, and then asks the rhetoric question if the precision is really needed (perhaps adding that floating point operations errors are subject to error and one should always check against a error that heavily depends on the domain of values the floats can assume) is a good answer. Can you put that as an answer so we get this out of the comments and I can accept it? – Omnivore