That makes sense, thank you! So even a float represented to the hundredth place (say in this restaurant POS system) is enough to introduce rounding error?
Great question. Basically the answer is that if you know exactly how many decimal places you need, you are actually just using integers scaled by a multiple of ten. If you're not sure how many decimal places are necessary, go ahead and use an IEEE 754 floating point number. No matter what, it'll use either 32 or 64 bits to get as close as it can to your number. I'd recommend researching it a little more, as I think IEEE 754 numbers are one of the more brilliant things in computer science. The most frustrating bit is that they had to special case zero (but don't take my word for it, go look up why!)
I will definitely read up on IEEE 754 numbers, thank you so much for the help. I really took floats for granted, but I see now that there's quite a bit more to them.
I really appreciate the help, by the way. I was shocked when I booted up the terminal and checked 0.1 + 0.2 for myself. I feel like it's something I probably should have known by this point! I had just assumed rounding errors only popped up with more precise numbers, not because of using binary.
Keeping the numbers in binary means your CPU can compute them in a single operation. It's super fast, that's why it's the default. And the loss of precision is really tiny so for lots of use case, it doesn't matter at all.
However, floats are dangerous because they clash with human intuition.
well yes. If they were all .9 or .5 whatever the rounding is to, then they all round up one I would think. I thought decimal was always good enough, but I guess I need to learn more about handling money. :P
8
u/ami98 Jul 12 '19
That makes sense, thank you! So even a float represented to the hundredth place (say in this restaurant POS system) is enough to introduce rounding error?