- Home /
float Issue
Hi, I have a float which controls the rotation speed of a cube. When you level up I'm trying to add 0.1f to the rotation speed float. It all works fine the first few times. It goes from 0.4f to 0.5, 0.6, 0.7 but then it goes a bit haywire as the number that should be 0.8f is now 0.80000001. Is there a reason that this is happening? Is it an issue I should be concerned with? I have googled and not found anything that I can see is relevant, but that's probably down to me not knowing how to word the query properly
Answer by tanoshimi · Oct 09, 2016 at 06:20 PM
Floating point arithmetic is inherently imprecise - that's just how it works. 0.7f + 0.1f is not exactly 0.8f. No, it's nothing that you need to worry about, other than you should know never to compare two floating-point numbers for exact equality.
Thanks for the straightforward and precise answer Have a nice day :)
Little hint to this one: if you need to trigger something when two floats are the same, you should use "if ($$anonymous$$athf.Approximately(float, float))". Without that, two floats will hardly trigger "if (float == float)".
Your answer

Follow this Question
Related Questions
Shown decimal places 2 Answers
Playerpref.SetInt enterd with a float. 2 Answers
Convert double to float for Vector3 1 Answer
I dont want Decimals :P 1 Answer
Javascript rounding off a decimal 0 Answers