Live data from Hacker News

Never Use Floats for Money (2016)

husobee.github.io

31–40 of 41 posts

Re: Never Use Floats for Money (2016)

#31

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…

It can't be worth any microscopic performance benefit to not just be safe/precise and substitute a decimal datatype in 99% of cases. Something crazy like HFT might be an exception, or if it's just a quick excel sheet. Otherwise it's still a perfectly good rule of thumb to keep money in decimal types if you're writing code.

Re: Never Use Floats for Money (2016)

#32

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…

It can't be worth any microscopic performance benefit to not just be safe/precise and substitute a decimal datatype in 99% of cases. Something crazy like HFT might be an exception, or if it's just a quick excel sheet. Otherwise it's still a perfectly good rule of thumb to keep money in decimal types if you're writing code.

It's not about performance.

Modeling (including financial modeling) requires transcedental functions and real numbers.

Re: Never Use Floats for Money (2016)

#34

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…

> Sometimes being compatible is more important than being correct.

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.

Re: Never Use Floats for Money (2016)

#35
post #2

Fun Fact: Microsoft Excel uses double precision floating point numbers. Much of the world's numbers are run through Excel. I feel like some people have probably just ended up using the same floats themselves, just so that their boss's spreadsheet agrees with their numbers... https://en.wikipedia.org/wiki/Numeric_precision_in_Microsoft...

As does Javascript of course. On the other hand, IEEE754 guarantees correct rounding, so floating point operations on integral values will come out alright (additions and multiplications will be exact within a large range, and inexact divisions will be rounded correctly, so you shouldn't be any worse than using integer operations).

Re: Never Use Floats for Money (2016)

#36

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…

> Sometimes being compatible is more important than being correct. 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.

If your customers are already using floats, precision has already been lost. They're sending you data that already has floating-point round-off. If you then treat that as integer numbers of cents, you're adding additional round-off error by converting their 23 bits of binary precision to 2 digits of decimal precision. It's better to use the same format they do so that all floating-point error occurs within their systems, where it's known, has presumably been judged to be low-risk, and can be compensated for, rather than silently and unpredictably add new sources of error that your customers don't know about.

Re: Never Use Floats for Money (2016)

#37

Use a rational type :)

I rather like this approach. It easily handles sub-cent transactions with any desired degree of precision (e.g. internal computations using the 9/10 cent bs at gas pumps). Internally everything is a rational quantity, and you just need to maintain correct (de) serialization at the system boundaries.

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.

Re: Never Use Floats for Money (2016)

#38
How do I properly handle Money on the web frontend side?

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?

Re: Never Use Floats for Money (2016)

#39
I agree that you should not use floating numbers for amounts of money (maybe with some kind of calculations it might be suitable, but this is rare), but I do not deal with calculations involving money on the computer so much. But, yes normally integers are better for the amount of money (and for some other stuff that normally involves fractions, too; if fixed point is suitable (as is the case for money), then integers are probably better than using floating point). (Comment 22322487, and its replies, also mentions why sometimes floating point is used, but normally to record amounts of money, integers are better. As other comments mention, sometimes you might even use other types such as rational type.)

Re: Never Use Floats for Money (2016)

#40
post #38

How do I properly handle Money on the web frontend side? 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?

There are libraries, such as Dinero.JS [0] or Decimal.js (handles decimals as strings). Alternatively, don't use decimals and always convert everything to cents.

[0]https://frontstuff.io/how-to-handle-monetary-values-in-javas...

Post reply on HN