Live data from Hacker News

Never Use Floats for Money (2016)

husobee.github.io

21–30 of 41 posts

Re: Never Use Floats for Money (2016)

#21
post #14

I remember hearing this back when I worked on financial software for hedge funds (2005ish), adjusting our product so it represented currencies as integer numbers of cents, taking the change to my boss (the company CEO), and then hearing "Use floats for money. All of our customers do, and if we don't you'll create needless friction for them." Ran into it more recently when doing cryptocurrency market analytics. Bitcoi…

> and then hearing "Use floats for money. All of our customers do, and if we don't you'll create needless friction for them." 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.

If 99% of your customers do things the wrong way and 1% wants to do it the "correct" way that you don't support, there's an easy business call: you do it the way the 99% want and tell the 1% "Sorry, we don't support that. Hopefully you'll find a provider who does." Massive portions of the economy are built upon that principle, from IE6 to UNIX to C to VHS to QWERTY to 120V electricity to U.S. custom measurements to MM/DD/YYYY date formats to the use of decimal (as opposed to binary) subdivisions in the first place.

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.

Re: Never Use Floats for Money (2016)

#22

This type of thing coming from programmers really bothers me. Here is a more universal rule: Never say "never." 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 sim…

Right. For example, potential insurance losses are only known approximately, so floating point is what should be used in their modeling.

Re: Never Use Floats for Money (2016)

#23

This type of thing coming from programmers really bothers me. Here is a more universal rule: Never say "never." 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 sim…

I’ve built two different payment platforms in the past, I’m curious why floating point is the correct and only choice for financial modeling.

Re: Never Use Floats for Money (2016)

#25
post #23

This type of thing coming from programmers really bothers me. Here is a more universal rule: Never say "never." 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 sim…

I’ve built two different payment platforms in the past, I’m curious why floating point is the correct and only choice for financial modeling.

Quantization error is irrelevant in modelling so far as the datatype has the appropriate dynamic range. You don't care about the rounding errors, you care about the speed of execution of your model and how able it is to find trends/behaviors. Exact values aren't useful.

Re: Never Use Floats for Money (2016)

#26

I tried a small C++ program and it does not exhibit this behavior. Why is it not showing there? program: #include using namespace std; int main(int argc, char argv) { float s = 165 * 1.40; cout output: ./floats 231

https://stackoverflow.com/a/554134/311196

Re: Never Use Floats for Money (2016)

#27
post #26

I tried a small C++ program and it does not exhibit this behavior. Why is it not showing there? program: #include using namespace std; int main(int argc, char argv) { float s = 165 * 1.40; cout output: ./floats 231

https://stackoverflow.com/a/554134/311196

thanks!

Re: Never Use Floats for Money (2016)

#28

The article is right, you should use integers for the number of cents (or whatever the smallest unit of the currency is). That is easy enough as long as you are just adding and subtracting, but what do you do about multiplication and division? How do you calculate interest or yearly yields? I think the answer is fixed point, but that is not easy.

What problem do you see with multiplication or division? You can still do those with the precision you need, followed by the legally valid rounding for the use case.

Unless you don't have a use case where those are defined/relevant, then ¯\_(ツ)_/¯

Post reply on HN