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 /
avatar image
1
Question by PorkMuncher · Jan 22, 2015 at 11:42 AM · floatcomparezero0

Is it a good idea to check if a float isn't zero

As I understand floats can't be checked for equality - but is it different with the 0?

Is it a good idea to use

 float a=0;
 
 if(a!=0) DoSomething();


Or should I use

 float a=0;
 
 if(a>0 || a<0) DoSomething();

instead. Or is there no difference?

Can I check a float for equality with zero? Can it somehow go wrong, so that it reads 0.000001 instead of 0 and the if becomes true?

This is because my game character has stats as floats, but I want them written in UI only if they are not 0. Or should I simply compare it with an arbitrary, really small value?

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

3 Replies

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

Answer by DaDonik · Jan 22, 2015 at 11:46 AM

This is how you should do it, or better said how you can do it safely.

 public bool FloatIsZero(float _fValue, float _fEpsilon)
 {
     bool bRet = false;
 
     if ((_fValue < _fEpsilon) && (_fValue > -_fEpsilon))
     {
         bRet = true;
     }
 
     return bRet;
 }

Comment
Add comment · Show 3 · 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 PorkMuncher · Jan 22, 2015 at 11:51 AM 0
Share

Thanks! The $$anonymous$$athf class thingy turns out have the epsilon - this means I don't need to give it as a parameter right?

avatar image DaDonik · Jan 22, 2015 at 11:53 AM 0
Share

Yup, either remove the epsilon parameter and use $$anonymous$$athf.Epsilon inside the function, or just pass $$anonymous$$athf.Epsilon to the function. I do it the second way, because i can change it, depending on how exactly i want to check for any given purpose.

avatar image Dailyalex · May 23, 2020 at 05:52 AM 0
Share

Better to use $$anonymous$$ath.Abs(amount) > EPSILON

avatar image
3

Answer by Owen-Reynolds · Jan 22, 2015 at 08:10 PM

It depends.

If you are doing something which you know will eventually set it to zero, than checking ==0 is fine. For example, in x=Mathf.Clamp(x,0,100); if x is -14, it will be set to exactly 0. It will never, ever turn -14 into 0.0000001.

And in x=Mathf.MoveTowards(x,0,Time.deltaTime); you will end at exactly 0. If you know x starts positive, then checking x<=0 is a nice reminder. But if you aren't sure whether x starts positive or negative, while(x!=0) works fine.

In other words, anything which set x=0 will not add "rounding error," and if(x==0) is safe.

If you have an equation, which should hit 0, that's when rounding happens. Starting from 10 and subtracting 0.1 over and over may hit 0.000001 instead of 0. Or a rigidbody at -1 moving at a speed of 0.1 may skip 0 and hit 0.000001 instead.

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

Answer by Studio-JD · Jan 22, 2015 at 12:03 PM

Or you can use it :

if(!Mathf.Approximately(a, 0f)) DoSomething();

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 DaDonik · Jan 22, 2015 at 12:05 PM 0
Share

Didn't even know that function exists...

Still i like having a variable epsilon parameter, in case i have to 'finetune' it.

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

28 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

Related Questions

Unity 2*10^-16 == 0 is true!! 1 Answer

zero is not zero 2 Answers

Input.GetAxis returns a value, whilst CrosPlatformInputManager.GetAxis dosn't. 1 Answer

Assignment of float is incorrectly set to zero. 2 Answers

Why comparing float values is such difficult? 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