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
0
Question by Gregu · Apr 02, 2015 at 12:40 PM · floatmathfprecision

Comparing float value to zero

This code works as expected:

 float myValue = 6.0f;
 
 void Update()
 {
   myValue -= Time.deltaTime;
 
   if(myValue < 0.0f)
     myValue = 0.0f;
 }
 
 public bool IsValueZero()
 {
   return myValue == 0.0f;
 }

But I'm curious if it's safe to use a construction like that. I'm aware of float comparsion, but also wonder - can we do a safe float to value comparsion if we are sure that our float was set to exactly that value. It don't have to be a zero, can also be something like:

 float myValue = 6.43f;
 
 if(myValue == 6.43f)
   Debug.Log("It's working!");

Will this construction always work as expected?

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

1 Reply

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

Answer by Owen-Reynolds · Apr 02, 2015 at 02:26 PM

Yes. Setting a float directly to a value will not produce "rounding" errors. Checking if(x==4.0f), when you know it was directly set, is safe.

Setting a float to an int is always exactly that int (unless something like 345664345654.0, with too many significant digits.) Setting to a decimal will "round" the same both ways. So 6.43 may round to 6.42999, but it will always round the same way, so a direct compare is safe (but not commonly done.)

The only time "random" rounding occurs is when you do math. 0.1f*10 may be 1.00001. But 0.05f*20 may be 0.999999. The trick is, we know 1/3 is repeating 0.33333, so has to round. For computers, 0.1 is repeating, since it stores fractions as 1/2, 1/4, 1/8 ... . So, 0.5 won't round, and 0.5f*20 should be exactly 10 (but no one does stuff like that.)

Lerp, clamp, moveTowards, ... will give an exact number. x=lerp(2.0f, 9.0f, percent); will give exactly 2.0 and 9.0 when percent is less than 0 or more than 1. Because, internally, they say "if percent 1 or more, value equals 2nd number."

Comment
Add comment · Show 2 · 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 Gregu · Apr 02, 2015 at 02:31 PM 0
Share

Thank you, that was the answer I was looking for :) Do you have any sources on the topic btw.? I assume it's a general rule in C# (and other languages probably).

avatar image Owen-Reynolds · Apr 02, 2015 at 02:38 PM 0
Share

The key is the 3rd para. Look up how floats are stored. 0.75 in binary is 0.11 (1/2 plus 1/4th.) Once you understand that most decimal numbers are repeating negative powers of 2 (10th is 1/16th, plus 1/64th(?) plus ... ) it should all be obvious.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

float precision, again 0 Answers

Show 1 decimal only when printing a float 1 Answer

Rounding to the nearest int as a Result of a Function? 1 Answer

float precision, wrong computations 1 Answer

Problem with precision of moving object many times.,Moving object many times with precision 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