Would you mind explaining this to me? I understand the difference between floats and integers, but when handling money wouldn't you need to represent decimals? Thanks
$ python
Python 3.7.0 (default, Dec 2 2018, 20:10:15)
[GCC 8.2.1 20180831] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 0.1 + 0.2
0.30000000000000004
>>>
Wow, everybody knowns that 0.1 + 0.2 equals 0.3, right? Is Python broken? Turns out that no, every single programming language on the planet will get you the same results. Some will trick you into thinking they don't, for instance Python 2 would use a printing trick when it noticed a bunch of zeroes like that and just print 0.3 but under the cover it stored 0.30000000000000004 like everybody else.
Now that we saw the weird behaviour in action, let's check out why.
Let say I asked you to write 1 / 3 in decimal. You would write 0.33333333. At some point you have to stop writing 3s and what you wrote is not exact. It close to the answer but it's not exact.
Computers don't store their data in decimal, they store it in binary. In decimal after the dot you have 1/10th, 1/100th, 1/1000th, etc. In binary afer the dot you have 1/2th, 1/4th, 1/8th, etc. 0.5 in decimal is 0.1 in binary and 0.75 is 0.11.
Now, how do you write 0.1 decimal in binary? Turns out you can't, 1/10th doesn't work at all in powers of two. So as soon as you gave that to python you lost precision. Does it matter? It depends on your use case. For instance if you are using it on a pi program that mesures the temperature in your aquarium it won't matter because the loss of precision is so small it's negligible, certainly smaller than your sensor's precision.
However, it's a big deal in two cases:
The numbers must be precise. In the case of money for instance if two different computations lead to $1.99 and one is very slightly under and the other is very slightly over then they will be unequal while they shouldn't be. And it gives bugs that are incredibly hard to debug because most of the time computations will give you the same imprecision, except when they don't.
Numbers accumulate over time so the error compounds. There was this bug in a missile interceptor used in the first Gulf War. The cummulative error made it miss an Iraqi missile by 3 seconds and about 30 soldiers died from it.
Now, what can you do about it? Well, if you are counting money, you can count in cents. Then you would nave no decimal at all. You just put them back at display time.
Or you do what you learn in school and keep the numerator and denominator around so again, you aren't asking the computer to keep floating point numbers around. Of course, it's slower than floating point because each computation is now actually several computations under the hood. Python already has this implemented in a module : https://docs.python.org/3.7/library/fractions.html
As an addendum to that, most languages/environments have an arbitrary precision number library available. The Python standard library has one called decimal.
That being said, storing the number of cents (or some fraction of a cent) as integers and doing all operations on that is probably faster, and is a well understood way to handle money in programs.
Thank you very much for the thorough answer, it makes complete sense now. And thank you for the Gulf War fact, it really illustrates the problem with compounding rounding errors.
In my previous work, we store money amount as 1/100 of cents. This is mainly to support interest calculation. Rounding to the nearest cent everyday introduces too much error than some of us liked, so we round to the nearest 1/100 of a cent.
Floating point numbers are close to a particular number, not an exact one. A lot of times, that's fine. But if things like rounding error could cause doubt, as when dealing with money, it's better to use discreet absolute amounts. Instead of doing your operations on a partial number of dollars, do all of your math on an exact amount of pennies.
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.
Using integers makes most sense when you don't want precision smaller than a cent which usually is what you in a POS. When counting taxes and whatnot, you are supposed to discard anything smaller than one cent.
109
u/redalastor Jul 12 '19
Did you avoid the common pitfall of using floating point numbers to manage money?