• 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 t$$anonymous$$s is a floating point error, how can I print the full float number (not rounded)?

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

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 t$$anonymous$$s, you could rearrange your if condition to use Mathf.Approximately. Somet$$anonymous$$ng like t$$anonymous$$s:

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

@DavidRoc$$anonymous$$n 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 t$$anonymous$$s 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.

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

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

Having trouble with Isometric movement 1 Answer

Float sets itself to zero 1 Answer

Why are floats not accessing my inputted values? 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