• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by rmarra07 · Apr 24, 2013 at 02:24 PM · bugvector3

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Apr 24, 2013 at 02:38 PM 3
Share

You are working with floating point numbers, so even though the Vector3s print out the same, they may not be close enough. You might print out the Vector3.Distance() between the two. You could even use Vector3.Distance() being less than your own defined threshold ins$$anonymous$$d of the '=='.

But I think you are headed for heartache if you approach solving your problem this way. For any given "physical" rotation, there are multiple euler angle rotations, and Unity will make changes in the euler representation. For example if you set a rotation of (180,0,0) and immediately read it back, you might get (0,180,180) ins$$anonymous$$d. If you give us information about the problem you are trying to solve, perhaps we can give you a better approach.

avatar image CHPedersen · Apr 24, 2013 at 02:42 PM 0
Share

This looks strange to me, too. O_O

This is a shot in the dark, but maybe they differ by more than $$anonymous$$athf.Epsilon? I know that $$anonymous$$athf.Epsilon is defined to be the smallest possible number two floats can differ, and $$anonymous$$athf.Approximately uses this number to test for equality. $$anonymous$$aybe 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.

avatar image rmarra07 · Apr 24, 2013 at 03:03 PM 0
Share

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 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · Apr 24, 2013 at 02:49 PM

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rmarra07 · Apr 24, 2013 at 03:01 PM 0
Share

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.

avatar image Bunny83 · Apr 24, 2013 at 03:37 PM 0
Share

You can simply never trust float values ;)

Here's an example (based on yours):

 var V1 = transform.eulerAngles;
 var V2 = RightRotation;
 Debug.Log("V1: " + (V1.y).ToString("0.000000000000"));
 Debug.Log("V2: " + (V2.y).ToString("0.000000000000"));
 
 Debug.Log("V1: " + (V1.y*0.5f).ToString("0.000000000000"));
 Debug.Log("V2: " + (V2.y*0.5f).ToString("0.000000000000"));

This will print:

 V1: 180.000000000000
 V2: 180.000000000000
 
 V1: 90.000010000000
 V2: 90.000000000000

Because 180 has more significant numbers before the floating point there are less behind it. ToString does kind of round away the error because it's too small. When you scale it down (by dividing, subtracting) the significant digits behind the floating point increase as well as the relative error.

avatar image Bunny83 · Apr 24, 2013 at 03:42 PM 0
Share

The error is there, even when ToString doesn't display it:

 Debug.Log("V1: " + (V1.y*4).ToString("0.000000000000"));
 Debug.Log("V2: " + (V2.y*4).ToString("0.000000000000"));

-->

 V1: 720.000100000000
 V2: 720.000000000000

$$anonymous$$eep in $$anonymous$$d ToString shows you a decimal representation of the number, but it's acutally binary.

avatar image rmarra07 · Apr 24, 2013 at 03:43 PM 0
Share

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

avatar image
0

Answer by Jessy · Apr 24, 2013 at 02:48 PM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rmarra07 · Apr 24, 2013 at 03:03 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Accels Instead of Jumping? 1 Answer

transform.forward breaks when changing scenes 1 Answer

Problem with normal snapping and rotations. 1 Answer

Vector3.Smoothdamp weird negative number glitch?! 2 Answers

transform.position = new Vector 3 NOT moving to correct position? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges