Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by freedt · Jan 20, 2017 at 08:47 AM · math

Why does float division not work?

If i do something like this

double xd = 1.5f / .3f;

it shows xd as not equaling 5, and instead equaling something like 4.999999

Why is this?

Comment
Add comment · Show 1
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 Hellium · Jan 20, 2017 at 08:46 AM 0
Share

http://stackoverflow.com/questions/753948/why-is-floating-point-arithmetic-in-c-sharp-imprecise

3 Replies

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

Answer by JJMGsoftworks · Jan 20, 2017 at 04:33 PM

Doubles and floats are not always stored as expected in memory. They are also not always precise for equations. Take a look at this answer on a stack overflow Post Here Hellium Has Commented this link above already. I believe Decimal is a more accurate type to go for but it depends on what you need. Check this out if you need More Help. If you have trouble in the future MSDN or a google search will always have the information on how data types work.

Comment
Add comment · Show 3 · 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 tanoshimi · Jan 20, 2017 at 04:53 PM 0
Share

Decimal gives more precision if you're calculating, say, interest rates in a business application. But it's very uncommon in games program$$anonymous$$g - ints and floats should be sufficient for pretty much everything.

avatar image JJMGsoftworks tanoshimi · Jan 20, 2017 at 07:00 PM 0
Share

Agreed I have never used Decimal in a game. I just provided an alternative if completely necessary. The main thing is the the $$anonymous$$SDN page because it covers the data types in more detail.

avatar image freedt · Jan 21, 2017 at 12:06 AM 0
Share

The realization that certain decimals that are finite in decimal but infinite in binary sealed the deal.

avatar image
0

Answer by Amon · Jan 20, 2017 at 10:15 AM

I think it is due to "floating point precision error" in the number of digits after the decimal point. When your code is executed on different types of hardware it could yield a different result to the one desired. Some hardware can only process 6 digits after the decimal and others 4 or 7 etc.

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 NoseKills · Jan 20, 2017 at 04:24 PM 0
Share

Partly true. I recommend reading for example the link @hellium posted above, or even skim$$anonymous$$g through the wikipedia article on floats. It's not just the number of decimals, it's also how numbers of different magnitude are stored and that some numbers simply can't be exactly presented in the format floating point numbers use... Or decimal format in general...

10 / 3 = 3.33333...

No matter how many "3"s you put after the decimal point, multiplying by 3 will never give you 10 as a result.

avatar image freedt · Jan 20, 2017 at 11:54 PM 0
Share

But at no point is the code snippet I presented dealing with anything larger than one decimal point. And the answer is an integer. I should be no where near the limits of the float data structure.

avatar image
0

Answer by elenzil · Jan 20, 2017 at 08:59 PM

lots of good discussion about the "Why" of what's going on here.

here's some additional practical advice:

unless you fully understand the article Hellium linked to (and very few programmers do), you should never use == to compare two floats for equality. they will almost always be unequal.

instead, you should test for approximate equality. you can do this by either writing your own utility function, or using Mathf.Approximately().

Here's an example.

   void floatTest() {
     float a = 1.5f;
     float b = 0.3f;
     float c = 5.0f;
     float d = 5.00001f;
     Debug.Log("(1.5f / 0.3f) == " + c + ": " + ((a / b) == c));
     Debug.Log("Mathf.Approximately(1.5f / 0.3f, 5.0f):    " + Mathf.Approximately(a / b, c));
     Debug.Log("Mathf.Approximately(1.5f / 0.3f, 5.0001f): " + Mathf.Approximately(a / b, d));
 
     /*
     ouput:
     (1.5f / 0.3f) == 5: False
     Mathf.Approximately(1.5f / 0.3f, 5.0f):    True
     Mathf.Approximately(1.5f / 0.3f, 5.0001f): 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

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to calculate the angle between two vectors? 6 Answers

Adjusting viewing angle without distortion. 0 Answers

Issues wrapping my brain around this math 2 Answers

I am showing double on screen and it disappear 1 Answer

How to divide big float 2 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