

But usually there's another way around it. You seldom should ever need to do a float comparison, but if you find yourself needing too, use Mathf.Approximately. Because even if they're reeeeaaaaalllly close, they're not exactly the same. Never check if some float x = some float y. Just know that because this float error exists you should NEVER do float comparisons. You're not performing rocket science here, it's just a video game, and that tiny difference isn't going to kill anyone.

You need to just accept that 0.59999 is close enough to 0.6 for your maths. Just like you accept that 0.33333 is close enough to 1/3. You are currently running into the binary/decimal conversion error of floats. Our result definitely won't be 15.61, but it will be even off from if we had just set a variable to 15.6 rather than summed our way there. Even though 0.01 is stored as ~0.0099998, we're going to lose some of that when we sume it to the 15.6 since so much sig value is store in that 15 of the 15.6. But there also out of sig range of one another.

6 is a repeating number, and 0.01 is a repeating number. And you always take the largest sig values resulting in 745,672,190,000.Īlso these 2 things come into play together. but we have to truncate it down to ~7 digits of sig values. Why? Because the sig value of the larger number is out of range of 1. As a result changes in a float that are smaller than the current sig value range won't register. Single floats only really store ~7 decimal digits of sig value (give or take depending the number since it's actually 24 digits of binary sig value which can vary in decimal). The other form of float error is sig value range. 1/10, 3/5, these fractions are made up of values not easily factorable by powers of 2 which binary is. And MANY other numbers have this problem. So 0.1 is stored as 0.0999 (approximately). But since there is only a finite amount of space, it gets trimmed off. And the thing is that numbers that you think are normal in decimal are actually repeating numbers in binary. Floats are stored in binary, not decimal. So with this information there is the first form of float error that most people run into. The 32-bits are broken up as sign, exponent, and mantissa (mantissa is the fractional part of the scientific notation where the significant values are). Next, floats are stored in scientific notation. Rather instead a 32-bit single floating point value fits inside 32-bits of memory. There are no "infinite repeating" values in memory as that would imply infinite amount of RAM. A 'float' in memory takes up a finite amount of space.
