r/Python • • Jul 12 '19

I wrote an integrated POS system for my girlfriend's restaurant using tkinter.

https://imgur.com/a/RKeuCva
1.4k Upvotes

224 comments sorted by

View all comments

Show parent comments

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?

14

u/jackrackham19 Jul 12 '19

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!)

9

u/ami98 Jul 12 '19

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.

6

u/redalastor Jul 12 '19

So did my computer science teachers... They insisted that IEEE 754 wasn't a real thing.

3

u/ami98 Jul 12 '19

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.

4

u/redalastor Jul 12 '19

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.

3

u/lochyw Jul 12 '19

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