• 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
Question by davidrochin · Jul 24, 2017 at 12:41 AM · floatwrongconditional

if(12f > 12f) = true?

Hi! I am making a Character controller. I am almost finished, I just needed to fix a bug using the following if statement:

 if(GetColliderLimit(Vector2.up) > yLimitTop) {
                 Debug.Log(GetColliderLimit(Vector2.up) + " > " + yLimitTop + " = " + (GetColliderLimit(Vector2.up) > yLimitTop));
                 //DO THINGS TO FIX THE BUG HERE
             }

However, there is a case when it enters the conditional when it shouldn't. That is when both numbers are 12.

alt text

As far as I know, 12 > 12 is not True. Please help! What am I doing wrong? If this is a floating point error, how can I print the full float number (not rounded)?

I made A LOT of Math like this in my script, and this is the only time I got an error like this.

Thank you in advance.

capture.png (14.1 kB)
Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by CraigGraff · Jul 24, 2017 at 01:09 AM

It is almost certainly a floating point error. 12.000001 will appear as 12, but still register as greater than 12.

To fix this, you could rearrange your if condition to use Mathf.Approximately. Something like this:

 if(GetColliderLimit(Vector2.up) > yLimitTop && !Mathf.Approximately(GetColliderLimit(Vector2.up), yLimitTop)){

@DavidRochin Edited to working code.

Comment
davidrochin

People who like this

1 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 davidrochin · Jul 24, 2017 at 02:18 AM 0
Share

I didnt realize you edited the code and I made a comment mess haha. It worked! Thank you very much!

avatar image

Answer by toddisarockstar · Jul 24, 2017 at 12:58 AM

hmmm. i know "Print" works for accurate councel output with floats. Floats print with the decimals. Vector2 and 3 do NOT. hope this helps

         Vector2 v22 = new Vector2 (5.45433f, 6.985495f);
              print (v22.x+"/"+v22.y);
Comment

People who like this

0 Show 0 · 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

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

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

Related Questions

subtracting numbers gives a very wrong answer 0 Answers

Get the two highest floats in a genericList without using .Sort()? 1 Answer

I want to move a rigid object with following command 1 Answer

What is the max float? -1 Answers

Converting a float to a byte[]? Not working 2 Answers


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