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 cs94119 · Apr 25, 2014 at 06:00 AM · floatingpoint

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);
 }

}

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
0

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.

Comment
Add comment · Show 1 · 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 cs94119 · Apr 27, 2014 at 10:57 AM 0
Share

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!

avatar image
0

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

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

How do I initialize a float texture by script? 1 Answer

Why this prints (0, 0, 0): Debug.Log("" + ((new Vector3(1, 0, 0)) * 0.016f)); 1 Answer

Infinite runner world creation 2 Answers

Floating point correction 4 Answers

Float check not returning the right value. 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