- Home /
What is floating point precision limitations and how to deal with it?
I did the roll-a-ball tutorial and I succeeded. But one day it said due to floating point precision limitations, it makes the world coordinates of my object to be smaller making the sphere glitch out and disappear. I don't know why and please help.
-TQ
That is more of a Computer Science question rather than Unity related, so you have better chances looking for it on more appropriate site. That been said I ll give you a
simple
explanation.
All computers use Binary Base system meaning they represent internaly everything with 0 and 1(off-on).
Now when you feed a computer with a number (represented with base of 10) the computer has to convert it to binary base when that conversion happen there are chances that the original number (base 10) has 5 digit but the equal binary might have infinit digit thus it can not be represented correctly, so the computer will go ahead and cut off (or round up, not sure) the remaining digits, that is a representation error, and will accumulate over many calculations leading to wrong results.
$$anonymous$$ore specifically when you use a float which is 32-bit floating point number it will change the number to ,for example from 1892,3 to 1,8923(mantissa) 10^3(exponent?!) (or 0,18923 10^4 don't remember which one is it) and will use 23bits for the mantissa 8-bit for the exponent(just the power not the base, which of course will be 2) and 1-bit for the sign (+/-) but if the mantissa can not be represented in full (has more than 23 bits) will be cut out.
On the other hand if the exponent is larger than 8-bit it will throught an error Overflow or Underflow.
So to conclude the precision Limitation is how close is the actual number from that it is used from the system. You may use double(64-bit) or decimal (128-bit) for more precision (more bits more precision), but you shouldn't bother with all that unless you use heavy math functions and the results need to be precise, also Unity uses floats for pretty much anything so you don't have much of a choice on the matter.
Cheers.
Over and underflow do not really apply to floating point numbers and floating point math. The result is always rounded to the closest representable number based on well defined rules. At very large numbers if a change is too small to be represented it will not cause any change at all so you are stuck at that value. If the change is large enough and you're close to the max value the result might just jump to "infinity". However this will almost never happen as the change would need to be astronomically huge.
You may want to have a look at the table i posted over here.
Here's a neat IEEE single precision float converter which might help to understant how a number is represented.
Answer by ZeN12 · Nov 21, 2017 at 09:41 AM
The problem can be with physics, scale of gameobject with collider should be 1,1,1. This is important in order the physics work correctly, if you want to scale a mesh you should use Scale factor (https://docs.unity3d.com/Manual/FBXImporter-Model.html).
The floating point limitation for c# are describe here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/float
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Roll a Ball Tutorial: Ball floats in the air when I enter play mode. 2 Answers
Making a Trading card Game 1 Answer