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 JinJin · Apr 22, 2015 at 10:20 AM · c#bugmonodevelopscript errorif statement

Monodevelop C# script execution bug

I have a very simple if statement that is not working as expected:

   if( FreeProductStorageVolume < product.Volume * quantity )
   {
      Debug.Log( FreeProductStorageVolume );
      Debug.Log( product.Volume );
      Debug.Log( quantity );
      Debug.Log( product.Volume * quantity );
      canProduce = false;
   }

alt text

everything suggests that the breakpoint in line 824 should not be hit.

even the Immediate window evaluates the if() statement as false.

has anything like this happened to anyone else?

monodevelop.jpg (141.4 kB)
Comment
Add comment · Show 10
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 Paul-Sinnett · Apr 22, 2015 at 11:12 AM 0
Share

What are the types of these variables? float, double, int?

avatar image JinJin · Apr 22, 2015 at 11:37 AM 0
Share

they are all float

avatar image mikelortega · Apr 22, 2015 at 11:54 AM 1
Share

Floating point math is not exact, comparing two floats that are the same will give you precision problems. I mean, comparing A=1.0 and B=1.0 (A < B) can give true depending on how the numbers are represented. $$anonymous$$ore info on this.

$$anonymous$$aybe you should cast to integer, or increment the offset a little in one side of the comparison.

avatar image mikelortega · Apr 22, 2015 at 01:45 PM 1
Share

I know you are not comparing for equality, but the problem you are having is related to float precision.

You should never compare two floats the way you are doing if you want to check if they are the same or not.

avatar image Paul-Sinnett · Apr 22, 2015 at 02:42 PM 1
Share

I've found no documentation to suggest that the immediate window evaluation must be the same as the compiled code evaluation. In fact I've found very little documentation of the immediate window at all. $$anonymous$$y suggestion is that you most definitely cannot trust it.

Show more comments

3 Replies

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

Answer by Paul-Sinnett · Apr 22, 2015 at 02:13 PM

You are being misled by the Unity output window and Immediate window as they are rounding the numbers off.

The real values stored inside the machine are binary and they can't store tenths exactly. (That might seem counter intuitive, but it's similar to the way we can't store thirds exactly in base 10.) The true value of 0.2 will be something like: 0.200000002980232... But when you print it out in the output window or immediate window it will round it off to 0.2

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 JinJin · Apr 22, 2015 at 02:55 PM 0
Share

ok, that seems plausible. (even though i have turned off the debug option that uses ToString() to round numbers to 2 decimal places)

but even if the immediate window is rounding numbers, why would it print "false" when i put the "FreeProductStorageVolume < product.Volume * quantity" in the immediate window. even if 0.2 is stored as 0.200000002980232, the immediate window should use this value and print out "true".

avatar image Paul-Sinnett · Apr 22, 2015 at 03:20 PM 0
Share

Probably it's doing the calculation and rounding (truncating) in a different order in the immediate window when compared to the compiled version. Try adding an intermediate float value, e.g. volume = product.Volume * quantity. This should force the immediate and compiled versions to evaluate things the same way.

avatar image
1

Answer by _creatio_ · Apr 22, 2015 at 03:54 PM

I think other people are already described the matter of the problem, so I'll only post the solution that usually works for me:

 bool lessThenApprox(float a, float b, float precision)
 {
     //are the numbers differs enough corresponding to a given precision
     if(Mathf.Abs(Mathf.Abs(a) - Mathf.Abs(b)) > Mathf.Abs(precision)) 
         return a < b;
     else //assuming that both numbers are equal
         return false; 
 }

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
0

Answer by SUMFX · Apr 22, 2015 at 04:21 PM

I would use Visual Studio 2013 CE since its FREE and there is an official UnityVS plugin.

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

21 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

Related Questions

Why can't I edit C# script although I have monodevelop installed? PLEASE HELP 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

how to call a method from another script?,Does not contain a definition for and no accessible extension method accepting a first argument errors 1 Answer

C# Unity IF Statement not working properly and I can't figure out why. Deals with basic rotation stuff. 3 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