Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
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
Add comment
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
1
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
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 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
0

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

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.

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

how to use a float in a GUI.TextField? 6 Answers

Javascript int to float 1 Answer

changing floats by input C# 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