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
1
Question by user-759 (yahoo) · Mar 02, 2011 at 02:03 PM · vector3pointfloatingaccuracy

Floating point accuracy weirdness

1) Create New Unity Project 2) Add Cube 3) Add below script to Cube. 4) Rounding errors occur ??

What gives, where does the extra .000001 appear from, then disappears again. ?.

function Update () {

transform.position.x += 0.001;

}

X value/frame 0.001 0.002 0.003 0.004 0.005 0.006000001 0.007000001 0.008 0.009000001 0.01 0.011 .... ..... 0.037999999

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 Eric5h5 · Mar 02, 2011 at 02:47 PM 0
Share

That's not code you would want to use anyway, since it's framerate-dependent, so it's a moot point here.

4 Replies

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

Answer by Ben 14 · Mar 02, 2011 at 02:15 PM

Welcome to the world of floating-point precision issues :) This is not related to Unity specifically but to any usage of floats on computers. The way they are encoded means that not all numbers may be perfectly represented, and that will get you small, incremental errors, just as you've noticed.

I advise taking the time to read through http://en.wikipedia.org/wiki/Floating_point for a detailled explanation, but the takeaway is this:

  • don't rely on the precision of arithmetic operation on floats
  • don't compare floats by equality ( sqrt(0.1*0.1) may not be equal to 0.1 ), be tolerant to small differences
  • the further you are from 0.0, the bigger the errors will be. IIRC, around 10K you may get as much as a 0.1 error.

Cheers, Ben

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 yoyo · Mar 02, 2011 at 05:55 PM 3
Share

Use $$anonymous$$athf.Approximately (http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$athf.Approximately.html) to compare floating point values.

avatar image
1

Answer by emdzeyek · Jan 09, 2014 at 06:40 PM

Is this posible to change floating point by using some camera script in realtime?

For egsample. if camera is close the object Clipping plane near let it be 0.01 and if the camera is far away from object near property will be 1. Please for respond

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 Cambesa · Oct 10, 2016 at 11:52 AM 0
Share

I am also looking for such a solution. In my case I have a full earth map that is streamed to unity in which you can navigate to certain countries. Only near the world space (0,0,0) coordinates there's no distortion while navigating but at distances from about 10.000, there's a slightly notable distortion of 0.1 units, going to 20.000 it changes to a distortion of 1 unit. At distances of 375.000 from the world center that distortion went up to a 100 units which is very notable. I'm trying to make the movement smooth but can not think of a way to bypass the floating point errors.

I can not change everything to integers because the coordinate system in unity works with floats.

avatar image
0

Answer by Joshua · Mar 02, 2011 at 02:09 PM

The point of floating points is that they are not 100% accurate.

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 user-759 (yahoo) · Mar 08, 2011 at 01:52 PM

I would have hoped for something slightly more accurate that 0.001 before it starts being inaccurate, I shall just have to be aware of this issue.

Thanks for the answers people.

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 Statement · Mar 08, 2011 at 02:00 PM 0
Share

There isn't much you can do about it. What you can do is make your own fixed point struct, and work on that while doing calculations. Then before any rendering you could just go back to the "lossy floating point" version. You can find information via google. Here's a stackoverflow question I found: http://stackoverflow.com/questions/605124/fixed-point-math-in-c

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

2 People are following this question.

avatar image avatar image

Related Questions

how to find direction between two points 2 Answers

Why am I experiencing vector addition inaccuracies? 1 Answer

Kind of Patrol AI with Point A and B Help 1 Answer

How can I loop through every 3D point on a plane's surface in C#? 1 Answer

Find Most Distant Geometric Points in the Scene 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