- Home /
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
That's not code you would want to use anyway, since it's framerate-dependent, so it's a moot point here.
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
Use $$anonymous$$athf.Approximately (http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$athf.Approximately.html) to compare floating point values.
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
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.
Answer by Joshua · Mar 02, 2011 at 02:09 PM
The point of floating points is that they are not 100% accurate.
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.
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