Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
This question was closed Jul 07, 2014 at 12:53 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
-2
Question by Efril · Mar 30, 2014 at 09:38 PM · floatvectorcompareapproximately

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.

Comment
Add comment · Show 6
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 Scribe · Mar 30, 2014 at 10:25 PM 0
Share

"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?

avatar image Efril · Mar 31, 2014 at 09:07 AM 0
Share

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.

avatar image whydoidoit · Mar 31, 2014 at 09:26 AM 0
Share

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;
  }
avatar image Scribe · Mar 31, 2014 at 10:08 AM 0
Share

"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.

avatar image Efril · Mar 31, 2014 at 10:52 AM 0
Share

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.

Show more comments

1 Reply

  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Why aren't we able to multiply or "mask" vectors ? 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