Live data from Hacker News

Using floating-point numbers for money

evanjones.ca

71–80 of 128 posts

Re: Using floating-point numbers for money

#71

No, please don't. Often you have to combine money with quantities that also need unit of measure conversions and this just is one step too far. Frankly, chasing the error goes from a programming problem to a contract problem very quickly. Also, you are going to end up storing in decimal fields in the database anyway.

> Also, you are going to end up storing in decimal fields in the database anyway.

why not use cents as the unit and store it without decimals?

Re: Using floating-point numbers for money

#72

> Solution: Round after every operation No, the solution is not to use floating point numbers to store money. I worked on an iOS app in fintech for years, and let me assure you, using floating point numbers to calculate currency is an exercise in frustration and lack of correctness. When balances are wrong you're losing your customers money, which in turn loses trust in your product. You know it's inexact, an approxi…

Indeed, I think the underlying issue is that people use the last decimal as a kind of primitive error checking code that worked before the computer age. So it's a social matter. The legal implications stem from the social ones.

Re: Using floating-point numbers for money

#73
Others have mentioned most of the pitfalls with the suggested approach.

Another potential issue is that the floating point rounding mode can be modified by shared libraries or plugins.

[0] has examples of application crashes caused by a change in the rounding mode.

[0] http://www.virtualdub.org/blog/pivot/entry.php?id=53

Re: Using floating-point numbers for money

#74
post #50
post #49

Earlier quoted context omitted.

Easy, either implement a decimal type (4 fractional digits) as most programming languages have them implemented by users. or multiply by 10000 then do your calculation and then divide by 10000 again. I used a decimal type for the administration program I coded.

> Easy, either implement a decimal type Great, how do you become confident that your hand-rolled decimal type is less buggy than the known hazards of using floating-point?

If you’ve never done something like this, you might want to try it. It’s a nice simple programming exercise and the basic version, with tests, will take less than an evening.

Re: Using floating-point numbers for money

#75
I once worked at a place that had a third party vendor create a reporting front-end to the outdated minicomputer that the business ran on. After I was there a couple years, the CFO said "It's really weird, the numbers we get out of the reporting system are close, but never exactly right when we compare them by hand to the system."

This person was seriously thinking "this is just how computers are", and after a day of looking into it I was able to determine all the issues they were facing were floating point problems when the Windows front-end did it's calculations. It was pulling all the data directly from the old system, after all. How could it be different?

Re: Using floating-point numbers for money

#76
post #63
post #5

Sorry, but I remain skeptical. The same people who use `float` for financial calculations are probably the same people who don't understand how/why to do the rounding described to avoid these problems. Way too many programmers think that they are working in base 10 when using float. Why not just make them use it, and keep them out of trouble. Also, I wonder what the overhead of rounding every operation is? Comparable…

> I wonder what the overhead of rounding every operation is? Comparable to the cost of using a proper Decimal class? For what it’s worth, for the major ICs (Intel, NVIDIA, etc.) there is zero extra overhead. A choice of rounding modes is part of the floating point operation’s instruction. And keep in mind that a floating point op is always rounding no matter what you do, the question is whether it’s always using the…

It has great benefits to the user, though. Once you have $20 million in the bank, you can continue to withdraw $1 at a time as often as you like without spending any of your principal!

Re: Using floating-point numbers for money

#77
post #76
post #63

Earlier quoted context omitted.

> I wonder what the overhead of rounding every operation is? Comparable to the cost of using a proper Decimal class? For what it’s worth, for the major ICs (Intel, NVIDIA, etc.) there is zero extra overhead. A choice of rounding modes is part of the floating point operation’s instruction. And keep in mind that a floating point op is always rounding no matter what you do, the question is whether it’s always using the…

It has great benefits to the user, though. Once you have $20 million in the bank, you can continue to withdraw $1 at a time as often as you like without spending any of your principal!

By benefits to the user which would mean the bank made a mistake and now the IT department has a production P0 issue that will probably have everyone working to fix that yesterday then sure.

Re: Using floating-point numbers for money

#79
post #71

No, please don't. Often you have to combine money with quantities that also need unit of measure conversions and this just is one step too far. Frankly, chasing the error goes from a programming problem to a contract problem very quickly. Also, you are going to end up storing in decimal fields in the database anyway.

> Also, you are going to end up storing in decimal fields in the database anyway. why not use cents as the unit and store it without decimals?

Because it is a massive pain in the butt for your report writers / business intelligence people whose tools expect decimal fields. Never mind all the existing code that expects an actual decimal and not an integer. Plus tax folks like like their mills.

Re: Using floating-point numbers for money

#80
The hidden "gotcha" in most spreadsheets is that formatting cells to currency or 2 decimals changes only the display, not the number itself. To do that and eliminate potential floating point errors, wrapping EVERY currency formula in one of the round functions is standard practice for bookkeeping...
Post reply on HN