- Home /
Floating point determinism..
Hello folks, quick question about floating points. I know floating point numbers can be calculated differently on varying machines and operating systems though does this only apply to multiplication, division, etc? If a player clicks in a Vector3 location on the world, or I assign a unit to move to a Vector3 position in the world, are the XYZ float values potentially going to vary between machines as well? Or does it only apply to arithmetic?
edit: It seems like majority of the time Vector3 values are applied rounded to the nearest 10ths (20.3 etc), if my code remains that way should I even worry about determinism? Physics and collisions I'm sure will have some issues
Vector3 values are NOT rounded to a single decimal place. Vector.ToString() rounds to a single decimal place by default when formatting the string. Try `myVector.ToString("0.000000").
The precision of floating point arithmetic is predo$$anonymous$$antly dependent on the difference in magnitude between the two floating point numbers. The exponent part of the float indicates it's magnitude, and for a given exponent a floating point number will have a certain resolution, or the $$anonymous$$imum amount the float can change. As an example if you add 0.0001f to 1000000f the result is still 1000000f, because 0.0001 is less than the precision that can be represented by a float (32-bit IEEE) of that scale.
The $$anonymous$$imum precise significant digits that are guaranteed by a float is 6. Therefore if you know you are dealing with a score out of 20, for example, you are guaranteed precise arithmetic with up to 4 decimal places (2 significant digits left of the decimal, 4 after).
Good comment by Huacanacha, but I'd like to add that if you compare two vectors in unity, it will declare them equal even if they are only "really close" to being equal. http://docs.unity3d.com/ScriptReference/Vector3-operator_eq.html
You can do the same where checking float numbers for equality by using the mathf.Approximately(float, float) method. (http://docs.unity3d.com/ScriptReference/$$anonymous$$athf.Approximately.html)
Probably. In fact, I've seen values vary slightly from one session to the next on the SA$$anonymous$$E machine: Zero the first run, and some really small number like 6.745E-9 the next run.are the XYZ float values potentially going to vary between machines as well
Your answer
Follow this Question
Related Questions
Hexadecimal String to Float 1 Answer
VR flight sim floating point fun 1 Answer
Floating Point Inaccuracies in Editor 0 Answers
Atmospheric Scattering Shader Glitch on Floating Origin Late Update 0 Answers
My character vibrates. 1 Answer