- Home /
The question is answered, right answer was accepted
X,Y,Z coords of vector greater or equal or lesser or equal comparison
Hi all. I faced with a little problem when performing vector calculations. I know that Unity approximately checks for equality Vector2 and Vector3 structures and I can use Mathf.Approximately to check separate floats. But what if I should check Y coords of two vectors if they greater or equals or lesser or equals to each other, for example? The followng expression unexpectedly gives false for me:
//Vertex1.y is 123.9999985 and Vertex2.y is 124. value is false
bool value = Vertex1.y >= Vertex2.y;
What should I do in this case? Does Unity have something built-in for this? Or making my own comparison which will use Mathf.Aproximately as well as > and < is the only way? Thanks in advance.
"Is 123.999... greater than or equal to 124" -> obviously not, why are you expecting an answer other than false? What do you want the answer to be. Or why not use mathf.approximately and give is the 2 y values as floats if thats what you are asking?
Of course it is true when working with simple floats. But in my case they are Y coords of Vector2 structures where 123.9999985 should be equal to 124 and as the result 123.9999985 >= 124 should be true. The sense of this expression in terms of vector calculations is different from exact comparison of floats and I was thinking maybee Unity have something built-in for this similary to $$anonymous$$athf.Approximately.
That makes no sense at all - there is no circumstance where 123.9999985 >= 124 should return true - vector calculations are never different to that. I'm not sure what value of epsilon $$anonymous$$athf.Approximately is using but you should write your own comparer if you want it to be 0.0000016
const float epsilon = 0.0002f;
bool ApproximatelyGE(float a, float b) {
if($$anonymous$$athf.Abs(a - b) < epsilon) return true;
return b > a;
}
"Of course it is true when working with simple floats" - this is incorrect, try doing : Debug.Log(123.9999985 >= 124.0);
It will most definitely return False. I'm not sure what machine epsilon is for floating points normally, I have a feeling you can go to 12 decimals before they will become the 'same' value due to floating point errors.
$$anonymous$$aybe you want to do something like:
Debug.Log($$anonymous$$athf.Approximately(Vertex1.y, Vertex2.y) || Vertex1.y>= Vertex2.y);
that should do basically what @whydoidoit said but with whatever the internal epsilon is.
Thank you all. As I understand there is no built-in comparision method for such cases so I will do it myself. As to the epsilon Unity already have $$anonymous$$athf.Epsilon value which should be fine for this. Thanks again.
Answer by CHPedersen · Mar 31, 2014 at 11:26 AM
Mathf.Epsilon is equal to the smallest possible amount two floats can differ. This is a very small number. Mathf.Approximately checks for equality with Mathf.Epsilon as the tolerance. It is sometimes the case that your specific situation will require equality with a greater tolerance than Mathf.Epsilon. In those cases, you have to implement it yourself. Just define float tolerance = some number, and then do if(A < B + tolerance || A < B - tolerance).
This idea is semantically equivalent to whydoidoit's comment.
Follow this Question
Related Questions
Understanding Arithmetic & Vector Data 1 Answer
Convert localScale to float? 1 Answer
Link a value to another value in the Inspector 1 Answer
Vector3 constructor precision 1 Answer