- Home /
floating point error or script engine error?
Hi, i am making some simulation game and my team found strange bug. The simulation result of unity editor and window published exe is different. And we found some floating point bug and we make some test code like below. The result is different also. I think script engine of unity3d editor have some serious bug about return value processing.
using UnityEngine; using System.Collections;
public class FloatTest : MonoBehaviour {
string str = "";
float Func()
{
return 0.1f * 0.1f;
}
float Func1(float index)
{
return index * 0.1f;
}
// Use this for initialization
void Start () {
float test = Func();
float value = Func1(0.1f);
if( Func() == Func1(0.1f) )
{
str += "test1 same:";
}
else
{
str += "test1 differ:";
}
if( (float)Func() == (float)Func1(0.1f) )
{
str += "test2 same:";
}
else
{
str += "test2 differ:";
}
double r1 = Func ();
double r2 = Func1 (0.1f);
if( r1 == r2 )
{
str += "test3 same:";
}
else
{
str += "test3 differ:";
}
if(Func().Equals(Func1(0.1f)))
{
str += "Equals is true: ";
}
else
{
str += "Equals is false: ";
}
}
void OnGUI()
{
GUI.Label(new Rect(0, 0, 1000, 100), str);
}
}
Answer by tanoshimi · Apr 25, 2014 at 06:04 AM
You don't say what results you got, or what you were expecting, but all I see in your code is a lot of exact floating point comparisons - Func() == Func1(0.1f), r1 ==r2 etc.
Don't ever make comparisons for equality of two floating point numbers - it's not a bug in Unity - it's just an inherent imprecision in how floating point maths works: http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
Unity provides a Mathf.Approximately() function for this very reason.
Thanks for your answers! No, I don't think this is floating point imprecision problem. The pont is results of float point is different in Unity editor between published exe file!
InEditor if(Func() == Func1(0.1f)) => differ if((float)Func() == (float)Func1(0.1f)) => same
double r1 = Func (); double r2 = Func1 (0.1f);
if( r1 == r2 ) => differ
In exe file or webplayer all result is same!
I had tested also in visual studio native c# program. all result is same!
ps1) And about $$anonymous$$athf.Approximately() function. I think this is not the Approximately case. Even if error or imprecision occurs, the result must be same! Approximately is for point(1.0f/3.0f, 0, 0) is on in some plane test fuction. yes 1.0f/3.0f have the floating point problem and you can't get 0 from plane equation. The point is on the plane in math world, but maybe the point is not on the plane in floating point world. That's the need of $$anonymous$$athf.Approximately().
ps2) Statememt [if(Func() == Func1(0.1f))] and [if((float)Func() == (float)Func1(0.1f))] must be same code! But the result is differnt in editor!
Answer by YoungDeveloper · Apr 25, 2014 at 06:08 AM
Hi, dont use == sign for floats, as there might be so called magic numbers 1.000001 for example. Instead use this https://docs.unity3d.com/Documentation/ScriptReference/Mathf.Approximately.html