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 sarcasteak · Jul 13, 2014 at 11:28 PM · c#floatrounding-errors

Unity/C# Division problem

Given this code:

 print (dP.transform.localPosition.x + " " + TileManager.tileSize);
 print (1.3f/1.3f);
 print (dP.transform.localPosition.x/TileManager.tileSize);

I get this as output:

 1.3 1.3
 1
 0.9999998

The kicker is that all of the positions are exactly set from the editor as 1.3. Datatypes are all float. Viewing values in inspector and debugger shows no difference during runtime, the value of dP.transform.localPosition.x is 1.3!

I know there are floating point issues when doing division but I am doing the exact same operation both ways. Does it have to do with using a constant value vs a stored value?

Comment
Add comment · Show 2
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 sarcasteak · Jul 13, 2014 at 09:58 PM 0
Share

Yea I am just wondering why 1.3f/1.3f = 1 and referencing a variable storing 1.3f and dividing it by another variable storing 1.3f gives a different answer.

avatar image Bunny83 · Jul 14, 2014 at 12:10 AM 0
Share

The expression "1.3f/1.3f" is resolved as a constant at compile time and will be replaced with "1f". Anyways the string conversion might also round the value

2 Replies

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

Answer by CasualT · Jul 13, 2014 at 11:36 PM

Likely what's happening here is that the compiler sees you're doing math with two constants and so calculates the result at compile-time instead of waiting till run-time. This compile-time math "engine" is apparently able to take a shortcut when the two operands are the same, but the run-time version of division is not.

The runtime calculation is producing a result that is not unusual for floating point numbers. You could use Math.Round to get the 1.0 that you want.

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 Bunny83 · Jul 14, 2014 at 12:36 AM

This will give you the exact same result:

 float f1 = 1.2999998f;
 float f2 = 1.3000001f;
 
 Debug.Log (f1 +" "+ f2);
 Debug.Log (1.3f / 1.3f);
 Debug.Log (f1 / f2);

The problem is that the string conversion might round the last digit away. So your values actually aren't equal. That's absolutely normal for floating point values. Also .NET / Mono is even allowed to cast floats to doubles, executing an operation and cast back to float. This only happens in rare cases but might result in a slightly different result.

If you add this line:

 Debug.Log (f1.ToString("G20") +" / "+ f2.ToString("G20"));

you will see that f1 and f2 aren't 1.3 nor 1.2999998f nor 1.3000001f as they can't be represented as float. A float is stored as binary number, not as decimal.

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 Bunny83 · Jul 14, 2014 at 01:18 AM 0
Share

ps: Even this line:

 Debug.Log("Test: " + 1.3f.ToString("G20"));

will print this:

 "Test: 1.29999995"

See Single precision floating point format for more information.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Slowly decreasing a float 2 Answers

How do i make a float match to another float on other script 1 Answer

Return 2 FLOAT from 1 VOID 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