While tracking down a bug, I was having a variable broadcast it’s value into the debug log. Said variable was simply LegProgress += speed * Time.deltaTime; which was called in FixedUpdate.
Now, the speed was set to just 5, and with fixed update at the default 0.02, this variable is simply adding 0.1 to itself each iteration.
But yet, when it got to 2.7, the next value was 2.799999 instead of 2.8. And then later, after 3.799999 it dropped to 3.899998.
Now, I do understand that there is a fundamental error in precision with floating point numbers, but this is manifesting itself merely at the value of 2.8f. That seems really odd to me; surely we have enough precision to add 0.1f to 2.7f and get 2.8f. I mean, I can move object positions in my scene to such a simple value.
But then it gets worse at merely 1.0 later. If 2.8 were the point where we lost precision from floating point values, wouldn’t the next drop in accuracy come in around 5.3?
And then there is one more oddity:
The very first time this log was called, it declared the first value to be 0.09999999, but then the very next log is 0.2 and 0.3 and so on. If my original calculation of 5 * 0.02 isn’t giving me a result of 0.1, then why isn’t the whole sequence off? Why do I get 26 consecutive entries that properly calculate out to 0.1 intervals, but not the first? And then they drop again ten calculations later?
This just makes no sense to me. I thought that floating point precision lost accuracy with high digit numbers, this is just two. And shouldn’t the lack of precision be at a consistent/technically-predictable rate?
(PS, I found my bug, I neglected to set a different variable when the object was spawned, in case anyone was curious.)
