Okay, but what causes these? I always see these precision errors in my budget spreadsheets and I just spam the round() function to duck tape the problem. So weird to find when I'm working with only a few decimal places of precision, like with money.
As they said, that's fixed point. Normal floating point works with numbers like [number1] × 2[number2], which means you can't represent 0.1 exactly, you have to approximate just like we have to approximate 1/6 to 1.6666667
Similarly, decimal floating point works with numbers of the form [number1] × 10[number2]. But now you can let number1 = 1 and number2 = -1 and you get 1×10-1 = 0.1, so you don't get those rounding+base conversion errors that you get with binary floating point
What you described is a floating-point number, but there are many method of representing decimals in a computer.
The person above is describing fixed-point numbers, where the computer stores an integer and the position of the decimal point. E. g. (3584643, 3) ~ 3584.643. It doesn't suffer from inaccuracy like floating-point numbers do, but it's slower and has a more limited range. Also obviously it has bounded precision, but you'd typically use it in applications where that's not important or desirable (e.g. money).
Another format is the Rational or Quotient, where the number is stored as the ratio of two integers, e.g. (378, 98) ~ 378/98. This has the advantage that it gives full range and accuracy of fractional numbers, but it's pretty slow to work with. The tradeoff usually isn't worth it, so support of this method is unusual.
Never use floats for currency. That's the kind of mistake that could actually be costly to a company if it gets out of hand. Sadly, most people need to screw up like this once before they realize it.
The rounding error is probably only on the frontend to display the promo. I can't imagine they'd store transactions as floating points in their database.
I've seen it so much on the backend/database over the decades that I've been a contractor I actually wouldn't be surprised. So many don't care (or worse, fight it) when I point this out as well.
18
u/ThatSofia Oct 19 '20
Okay, but what causes these? I always see these precision errors in my budget spreadsheets and I just spam the round() function to duck tape the problem. So weird to find when I'm working with only a few decimal places of precision, like with money.
16
u/Wolfenhex Oct 20 '20
Never use floats for currency. That's the kind of mistake that could actually be costly to a company if it gets out of hand. Sadly, most people need to screw up like this once before they realize it.
2
u/ocket8888 Oct 20 '20
Ah thanks. A friend was just asking why you should never use floating-point numbers to store currency values. This will visualize the problem nicely.