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 Kyle · Nov 21, 2010 at 11:28 PM · floatcomparison

Float Comparison is not working

I'm having trouble with comparison of 2 float values in a C# script after having exhausted what I have seen as possible answers. Here is the code that assigns the value to the first float every FixedUpdate():

currentTime += Time.deltaTime;
currentTime = Mathf.Round(currentTime*100f)/100f;

The other float is assigned a float value from another method. Its value never changes. I've tried the "==" comparison as well as Mathf.Approxmately(). I've also tried changing each of the hard-coded values from simple numbers (100) to decimals (100.0) and appending "f" to designate them as floats. I've tried many combinations of the above 3 in conjunction with the different comparison options but the comparison is NEVER evaluated as true.

Am I missing something terribly obvious?

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
9
Best Answer

Answer by Mike 3 · Nov 22, 2010 at 02:18 AM

You really shouldn't ever compare floats with ==, as soon as you do any maths with them they'll be different internally, even if they look similar

What you can do is make a simple function to check for equality within a tolerance, e.g.

 function IsApproximately(a : float, b : float) 
 //lets you skip the tolerance to use a sane default value
 {
     return IsApproximately(a, b, 0.02);
 }

 function IsApproximately(a : float, b : float, tolerance : float)
 {
     return Mathf.Abs(a - b) < tolerance;
 }

ALternatively, when you create your conditional statements, use the greater/less than operators, e.g:

 if (myVal > -0.05 || myVal < 0.05) { /* do something */ }

The reason Mathf.Approximately isn't always too useful is that it's basically the function above, with Mathf.epsilon as the tolerance, which is the smallest possible float value, so it's not likely to be within it after a few sums

Comment
Add comment · Show 5 · 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 Kyle · Nov 22, 2010 at 02:22 AM 0
Share

Thank you. I hadn't seen that epsilon was the tolerance.

avatar image zoon · Nov 22, 2010 at 09:11 AM 0
Share

@$$anonymous$$ike You are wrong about $$anonymous$$athf.Approximately. You can look at code in Reflector - tolerance is a maximum of: 0.000001 $$anonymous$$ax(a,b) -- most cases , or 8 epsilon. It'a a good approximation for most game development tasks.

avatar image zoon · Nov 22, 2010 at 09:14 AM 0
Share

@$$anonymous$$ike: No, epsilon is not the smallest possible float value. It's the smallest positive float value that is greater than zero.

avatar image Mike 3 · Nov 22, 2010 at 11:19 AM 0
Share

Ah, I see what happened - in 2.6 they used float.Epsilon for approximately, which was ridiculous, but changed it to a much saner calculation for 3

avatar image rad1c · Nov 14, 2016 at 03:39 PM 0
Share

Great answer. As of today (Unity v5.4.2f2) I also just had the same issue with an "epsilon size" float variable, testing against "<". if ([variable of 4.482269E-05 value] < [0.05f const]) { / does not ever happen... / } A little counter-intuitive as I supposed 0.05f is a number big enough to be stored (kinda) precisely (at least againts a 10^-5 epsilon), but this was a good lesson :)

avatar image
0

Answer by zoon · Nov 22, 2010 at 09:01 AM

First of all, you can get current time much easier:

currentTime = Mathf.Round(Time.time);

Second: you should never compare time and constant with ==, always use <= or =>

Example:

const float kickHim = 5f;
...
if (Time.time >= kickHim)
{
   DoKickHim()
}

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

1 Person is following this question.

avatar image

Related Questions

summing up items price in runtime 1 Answer

how to make a float value,debug as int.(or show in text UI as Int) 1 Answer

Public Variable not updating 1 Answer

Change value according to Y position of an object? 2 Answers

How to change a point light intensity with an updating float 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