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 /
avatar image
0
Question by Xavier78 · Jul 14, 2016 at 10:14 PM · floatrounding

why is my float slightly off, and how do I see it?

So I am trying to get rid of a 0.0000001 incorrectness on my float from the expression

 cldsDown[i] = Mathf.Round(HitDown.distance * 1000) * 0.001f;            // ray dist rounded

it isn't always off, but annoying when it is. I tried to check to see if it is less then or equal to itself times a thousand rounded,

 if (cldsDown[i] * 1000 > Mathf.Round(cldsDown[i] * 1000)
 {
     cldsDown[i] -= 0.0000001
 }
 

But this is true even if my print I do of the two numbers are exactly the same! does print() not show precise Precision?

Summery of question

  1. How to make float precise

  2. how to see what my floats are exactly.

ps. I tried to google this, but have no clue what to call it.

Comment
Add comment · Show 3
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 · Jul 14, 2016 at 10:19 PM 1
Share

"How to make float precise". You can't. By their very nature, floating point numbers are inherently imprecise. But it doesn't matter - you never need to, and never should try to, compare equality of two floating point numbers.

avatar image Xavier78 tanoshimi · Jul 15, 2016 at 12:28 AM 0
Share

But if I am using a raycast to find a distance to a wall, and then move my character that distance, but then it says there is a 0.000000001 gap, but then won't move my character that distance to close the gap to say that it is actually colliding against a wall. So there ends up being a gap that never gets closed, so my character never collides with it!

avatar image Eric5h5 · Jul 15, 2016 at 07:08 AM 0
Share

There's no reason for this. A value being off by .00000001 will make no difference whatsoever in any sane situation. If your design is somehow dependent on infinitesimal differences like this, you really need to change your design.

2 Replies

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

Answer by Jessespike · Jul 14, 2016 at 11:45 PM

That's just the way floats work, as explained by tanoshimi. If you need more precision, you can use a double, it still won't be exact though. Use Mathf.Approximately if you want to compare floating points.

Comment
Add comment · Show 4 · 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 Xavier78 · Jul 15, 2016 at 12:23 AM 0
Share

But these are binary values, that represent a specific decimal value, I understand division and a few other mathematical functions will always be off because we are working on a base 2, just like you can't divide 1 perfectly bye 3, but it still can be saved as 0.33333332 or 0.33333334 which then can be viewed and adjusted by hand. What I am saying is even though the math might not turn out correct one still can view it perfectly and edit it perfectly right? So if floats only go down to like 7 decimal places why can't I see all 7 decimal places, and then edit the very last one, and compare two together??

Edit Take this for an example. I just multyplied my float buy 100k, and then cast it to int. As a float printed it said it was 121.00000001 as and int it was 12,100,000,000,003, so clearly the unity is out putting it to print rounded up.

avatar image Jessespike Xavier78 · Jul 15, 2016 at 05:29 AM 0
Share

I think the print or ToString function only shows up to 7 decimal places by default, even though floats can contain more than 7 decimal places, you usually don't need to know more than that. The string format can be overridden to show more digits if you want.

Floats aren't simple binary value as you may think, quoting wiki: "floating point is the formulaic representation that approximates a real number so as to support a trade-off between range and precision. A number is, in general, represented approximately to a fixed number of significant digits (the significand) and scaled using an exponent in some fixed base".

avatar image tanoshimi Xavier78 · Jul 15, 2016 at 06:10 AM 0
Share

No, you can't edit it precisely because that's not how floats are stored. They can store a very large range of values, but at the cost of precision. Why does it matter that your character is 0.000000001 units away from the wall?

avatar image Xavier78 Xavier78 · Jul 15, 2016 at 11:07 PM 0
Share

Well how ever this might be annoying, I still think you for your answer :).

avatar image
1

Answer by rmassanet · Jul 15, 2016 at 09:43 AM

As stated by other answers:

1) You can't make floats more precise.

However: 2) You can print floats with more digits using the string.Format function, which lets you control the number of decimal places, 0 padding, and things like that.

Regarding your problem of the 0.000000001 gap causing your character not colliding with a wall, why is that important? Your character is 0.000000001 away from the wall, so what? Whatever logic you are doing when the character collides with the wall, you could trigger it when you are forcing the character to move to the wall, right?

I hope this helps.

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

48 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there a setting to change integer rounding to 'Swedish Rounding'? 2 Answers

Rounding Off A Float? 2 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 the nearest int as a Result of a Function? 1 Answer

Double Round Problems 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