- Home /
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;
}
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?
What are the types of these variables? float, double, int?
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.
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.
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.
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
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".
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.
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;
}
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.