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 falconfetus8 · Jun 20, 2013 at 04:05 PM · floatdoubleaccuracydecimalrounding

On the accuracy of floats and doubles: Where/when do rounding errors usually occur, and how can I avoid them?

It's become apparent to me that floats and doubles aren't always accurate. It's only natural, of course, that a computer would have to round after a certain number of digits due to memory/performance limitations. However, I have read here that floating point numbers have an accuracy somewhere 7.2 and 7.5 digits. My understanding is that, since you can't have "part" of a digit, the computer will actually start to make weird mistakes remembering what the true value of a number is once too many digits come into play. But at what point do these mistakes start happening How many digits can I use before 1.250000 turns into something weird like 1.250001413?

The reason I ask this is because I plan on having a health system where the player has the potential to have a fraction of a hit-point(something like 2.5 health left), and I'm worried about small errors adding up to unexpectedly (and unfairly) killing the player when he should have barely survived. I'm fairly certain that using just two decimal places isn't going to cause any problems, but I'm curious as to what causes floats to lose their accuracy.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by whydoidoit · Jun 20, 2013 at 04:12 PM

You will find inaccuracies all the time at seemingly unlikely values, of course without applying them to a multiplication or a power etc those are so small as to not make a difference in any real sense. If you are performing complex math on them then you should use the highest precision possible and consider the methods outlined here http://en.wikipedia.org/wiki/Floating_point#Minimizing_the_effect_of_accuracy_problems - though it probably won't have much of an effect in the kind of system that you are proposing.

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 falconfetus8 · Jun 20, 2013 at 05:21 PM 0
Share

Does there seem to be any method to the madness, though? Are the inaccuracies essentially random, or do they only happen under a predictable set of circumstances?

avatar image whydoidoit · Jun 21, 2013 at 07:28 AM 0
Share

Well it's representing small decimal fractions with binary digits so it's generally hard to spot at exactly which point the errors start to occur, so there is certainly method, and it could be understood, I've just never really bothered apart from fiddling with the inspector when I want things to have a slightly different coordinate so that Z fighting is avoided :)

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

15 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

Related Questions

Convert double to float for Vector3 1 Answer

I dont want Decimals :P 1 Answer

C# CS0266 error for multiplying by a decimal 1 Answer

Rotation Values Garbled When Set By Code 2 Answers

Accuracy issues with Float (Vector3) 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