Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 ina · Jan 13, 2011 at 08:15 PM · mathfdecimalrounding

Unity Mathf Float Minus Integer Rounding Error

Can someone explain why subtracting this integer from a 2-decimal significant figures float results in the rounding error shown - and is there a way to "fix" or anticipate this error:

alt text

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
1

Answer by Jesse Anders · Jan 13, 2011 at 08:47 PM

The behavior you're seeing is inherent to floating-point representations (generally speaking), and is to be expected. You can't really prevent it per se, but there are ways to accommodate it (how to accommodate it depends largely on the context).

[Edit: I haven't provided an explanation of why this happens, as that information is widely available already. Here though is a link to get you started if you want to investigate it further; the paper is pretty dense, but it's more or less a standard reference on this subject. If you search online for 'floating-point error' though, you can probably find some easier-to-follow explanations.]

Edit: In response to the comment below...

It's inherent because floating-point representations use a finite number of bits to represent an infinite number of real numbers. Therefore, only a finite subset of the real numbers can actually be represented. One consequence of this (among many) is that many numbers cannot be exactly represented; instead, the nearest 'machine number' must be selected. That's one reason that floating-point values don't always come out exactly as you'd expect them to.

(The full story behind the floating-point representations most commonly used today is pretty involved; again, check the paper I linked for more info.)

As for accommodating this inherent imprecision, there are various solutions, but a common one is to introduce an epsilon or tolerance value. That is, rather than asking if x = y, you ask whether x equals y within some specified tolerance.

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 ina · Jan 13, 2011 at 08:48 PM 0
Share

can you give some examples on how to accommodate it? Incidentally, why is it inherent?

avatar image Jesse Anders · Jan 13, 2011 at 08:50 PM 0
Share

I'll edit my post and add a bit more info...

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

No one has followed this question yet.

Related Questions

I dont want Decimals :P 1 Answer

how to round a float to 2 DP 7 Answers

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

Rounding to multiples of 0.5 1 Answer

Rounding to the nearest int as a Result of a Function? 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