Never Use Floats for Money (2016)
husobee.github.io
husobee.github.io
Whether or not floating point numbers can be used depends on the context. If you are talking about an accounting system, core banking system, ERP, or similar transactional systems, then sure you probably should use integers not floats.
However, if you're working on any financial model, like something as simple as discounted cash-flow model, then floating point is the correct choice and only choice.
I've had a programmer try to convince me that I had to use integers in financial modeling software that I was developing, and he used this same folksy argument with me. It was a genuine struggle to convince him that this was not a universal law and made no sense for the application at hand.
Modeling (including financial modeling) requires transcedental functions and real numbers.
I think if you're doing financial modeling, or anything where the real uncertainty is bigger than your unit of money, then it's like any other calculation -- use floats with all of the cautions that you learned in numerical analysis class.
My dad played the stock market and used a slide rule for his calculations.
Another reminiscence: Since he owned some stocks, my dad got annual reports. I remember leafing through the report from General Motors, and seeing that all of the numbers were given at full precision, down to the penny. I remember thinking to myself: If one worker accidentally carries a pencil home, those numbers are all wrong!
Ran into it more recently when doing cryptocurrency market analytics. Bitcoin represents currency as integer numbers of satoshi, where 100M satoshi = 1 BTC. Ethereum represents it as integer numbers of wei, where 10^18 wei = 1 ETH. I thought I'd be smart and use these fundamental units internally, only to find out that basically every exchange quotes prices in floats. By the time you get the data, it's already lost whatever precision it had, and you're just introducing needless friction by trying to get smart.
Sometimes being compatible is more important than being correct.
Until a competent customer doesn't, then you've got to explain to them why your calculations are intentionally wrong, never a good look. Regulatory bodies also tends to want the correct decimal numbers.
Context matters too: both of these examples are financial modeling as opposed to transactions, where (as riskneutral's sibling comment points out) floating point error is swamped by the errors in your models in the first place, and some concepts can't be modeled at all with integers. Also regulators explicitly don't care about this area: if your models give incorrect results and you trade on them that's your problem.
It's a simple conversion for the sake of precision. If you are dealing with money transactions, you should strive for precise values wherever and whenever possible.
Obviously only convert to real numbers if you are doing operations on numbers in your system, otherwise keep in client format like you said.
https://en.wikipedia.org/wiki/Numeric_precision_in_Microsoft...
A real life example: in the R language, this statement resolves to FALSE
> 74.20+153.20==227.40
[1] FALSEUnless you don't have a use case where those are defined/relevant, then ¯\_(ツ)_/¯
I used to evangelize integer cents but then I worked on a few systems with floats and the world didn't fall over.
on the backend I have python's exquisite Decimal() which covers all bases and is base-10
how would one guarantee that precision when you have to serialize it into json and that's gonna become a normal double in JS?
[0]https://frontstuff.io/how-to-handle-monetary-values-in-javas...
program: #include <iostream>
using namespace std;
int main(int argc, char argv) { float s = 165 * 1.40; cout << s << endl; }
output: ./floats 231
That all gets thrown out the window when somebody really wants an exchange rate of pi for god knows why, but satisfying every real number is impossible, and satisfying every computable number exposes you to things like the halting problem, so having a system that just supports rationals seems like a good compromise.