Vector3 comparison

Could anyone make some comments on what the docummentation say about Vector3 comparison?

"Vector3.operator == static operator
== (lhs : Vector3, rhs : Vector3) : bool Description

Returns true if the vectors are equal.

This will also return true for vectors
that are really close to being equal."

I would like co compare the rotation vectors and they that are supposed take values from the a very limited set, and I want my comparison to return true even if the vectors differ say 1%.

Would it be the right solution to compare the Vector3.ToString() instead of vectors?
I could set the threshold then like on putting ToString(“F2”) - I get 2 digits threshold

When it says it will return true for Vectors very close to each other, the mean due to floating point inaccuracies and such, they test for approximate equality.

I would lean towards dot products to check the angle. The dot product of two normalized vectors is the cosine of the angle between them. So given Vector a and Vector b.

``````function CompareVectors(a : Vector3 , b : Vector3, angleError : float) : boolean {
//if they aren't the same length, don't bother checking the rest.
if(!Mathf.Approximately(a.magnitude, b.magnitude))
return false;
//A value between -1 and 1 corresponding to the angle.

var cosAngle = Vector3.Dot(a.normalized, b.normalized);
//The dot product of normalized Vectors is equal to the cosine of the angle between them.
//So the closer they are, the closer the value will be to 1.  Opposite Vectors will be -1
//and orthogonal Vectors will be 0.

if(cosAngle >= cosAngleError) {
//If angle is greater, that means that the angle between the two vectors is less than the error allowed.
return true;
}
else
return false;
}
``````

I would simply say

``````var vectorOne : Vector3;
var vectorTwo : Vector3;
var percentageDifferenceAllowed : float = 0.01 // is 1%
if( (vectorOne - vectorTwo).sqrMagnitude =< (vectorOne * percentageDifferenceAllowed).sqrtMagnitude ) {
Debug.Log( "They are less then 1% different" );
}
``````