Is Vector3 == operator broken?

I know this sounds like a stupid question, as it seems pretty unlikely that something as fundamental as Vector3 could have such a huge bug in it that no one has noticed. I’m hoping someone can point out an obvious mistake I’m making.

I’m trying to compare two Vector3 for equality. In this specific example, I’m expecting both to be (0, 180, 0).

public Vector3 RightRotation = new Vector3(0, 180, 0);

private void Move() 
{
   Debug.Log("Rotation: (" + transform.rotation.eulerAngles.x + ", " + transform.rotation.eulerAngles.y + ", " + transform.rotation.eulerAngles.z + ")" + ", Rotation: (" + RightRotation.x + ", " + RightRotation.y + ", " + RightRotation.z + ")" + "; Equal: " + (transform.rotation.eulerAngles == RightRotation));
   if (transform.rotation.eulerAngles == RightRotation)
   {
      Debug.Log("They are equal.");
   }
}

When run this outputs the following to the console:

Rotation: (0, 180, 0), Rotation: (0, 180, 0); Equal: False

I am printing the individual floats here so there is no rounding taking place. The y values of both vectors are exactly 180. Yet, == returns false.

The documentation for Vector3.== says:

Returns true if the vectors are equal.
This will also return true for vectors that are really close to being equal.

It seems to me that something is not working right here.

This looks strange to me, too. O_O This is a shot in the dark, but maybe they differ by more than Mathf.Epsilon? I know that Mathf.Epsilon is defined to be the smallest possible number two floats can differ, and Mathf.Approximately uses this number to test for equality. Maybe Vector3's equality operator does the same thing on a per-component-basis, and it fails because one of the components is more different than epsilon, for some reason.

I did end up using Vector3.Distance and comparing to a small value, and that works fine. I just don't understand how those Vector3s could not be equal.

2 Answers

2

The operator is implemented like this:

public static bool operator ==(Vector3 lhs, Vector3 rhs)
{
    return Vector3.SqrMagnitude(lhs - rhs) < 9.99999944E-11f;
}

and SqrMagnitude looks like this:

public static float SqrMagnitude(Vector3 a)
{
    return a.x * a.x + a.y * a.y + a.z * a.z;
}

So if something is broken than it would be plain floating-point-math. I guess that ToString will round the value. The eulerAngles property is not a good source for exact comparisons. Also watch out, it works differently in edit mode. In edit mode Unity uses a cached value internally. At runtime it’s purely calculated from the quaternion which will never be 100% exact.

I know that eulerAngles isn't the best way to compare rotations, and I have no problem working around that. That wasn't really my concern here. What was really baffling me was that I couldn't see any difference between the values inside the Vector3s. I can understand Vector3.ToString() doing some rounding, but float.ToString() seems like it should print exactly what's in the float at all times. At least, that's what I'd expect.

The error is there, even when ToString doesn't display it: Debug.Log("V1: " + (V1.y4).ToString("0.000000000000")); Debug.Log("V2: " + (V2.y4).ToString("0.000000000000")); --> V1: 720.000100000000 V2: 720.000000000000 Keep in mind ToString shows you a decimal representation of the number, but it's acutally binary.

That makes sense. Annoying, but makes sense. Thanks!

You can’t expect that kind of precision out of Transform.eulerAngles. Rotations aren’t actually stored as Vector3s. Try comparing the Quaternions, or make your own Equals extension method that takes your own tolerance as a parameter.

Great advice about comparing quaternions, but I know that eulerAngles isn't an exact way of working with rotations. My real concern was with the fact that I can't see any difference between those Vector3s, but they don't evaluate as equal.