Live data from Hacker News

Using floating-point numbers for money

evanjones.ca

31–40 of 128 posts

Re: Using floating-point numbers for money

#31

No, do not do financial calculations in binary floating point. Converting between base 2 and base 10 fractions can do funny things (including during the rounding step, which is also 99% guaranteed to be inaccurate because base 10 fractions can't be exactly represented as base 2 fractions). Most professional financial packages use fixed point decimal, which can easily be implemented by specifying a fractional unit (su…

> No, do not do financial calculations in floating point.

Floating point is fine, so long as it is big enough for the application and decimal (not binary) floating point. Fixed point decimal is easier to get right, and, really, arbitrary precision decimal (or, for even more generality, arbitrary precision rational) is even easier to get right.

Re: Using floating-point numbers for money

#32

No, do not do financial calculations in binary floating point. Converting between base 2 and base 10 fractions can do funny things (including during the rounding step, which is also 99% guaranteed to be inaccurate because base 10 fractions can't be exactly represented as base 2 fractions). Most professional financial packages use fixed point decimal, which can easily be implemented by specifying a fractional unit (su…

> No, do not do financial calculations in floating point. Floating point is fine, so long as it is big enough for the application and decimal (not binary) floating point. Fixed point decimal is easier to get right, and, really, arbitrary precision decimal (or, for even more generality, arbitrary precision rational) is even easier to get right.

Updated my comment, thanks. I meant binary float. Decimal float is rather unknown (unfortunately), so most people imply binary when they say "float", and most languages don't even have decimal float, so the warning is necessary.

PSA: If your favorite language doesn't support ieee754 decimal float, start badgering them to add it! This has gone on long enough, and the inertia against change is strong.

Re: Using floating-point numbers for money

#33

The author links to an article about how Excel does all its computations in 64-bit floating point, in a way that seems to bolster their point. The actual title of that article? “Floating-point arithmetic may give inaccurate results in Excel” ( https://docs.microsoft.com/en-us/office/troubleshoot/excel/f... ). How coincidental that this should show up on the front page at the same time as the thread on normalization o…

> What is wrong with using a currency library?

Do currency libraries exist for example for GPUs? (Maybe they do.)

Re: Using floating-point numbers for money

#34

The author links to an article about how Excel does all its computations in 64-bit floating point, in a way that seems to bolster their point. The actual title of that article? “Floating-point arithmetic may give inaccurate results in Excel” ( https://docs.microsoft.com/en-us/office/troubleshoot/excel/f... ). How coincidental that this should show up on the front page at the same time as the thread on normalization o…

> What is wrong with using a currency library? Do currency libraries exist for example for GPUs? (Maybe they do.)

If you need your financial code to run in the GPU either you don't need fixed point number (simulations) or your code is extremely inefficient.

Re: Using floating-point numbers for money

#35

Earlier quoted context omitted.

> What is wrong with using a currency library? Do currency libraries exist for example for GPUs? (Maybe they do.)

If you need your financial code to run in the GPU either you don't need fixed point number (simulations) or your code is extremely inefficient.

> you don't need fixed point number

That was my point.

Re: Using floating-point numbers for money

#37
I ran into a weird problem this week trying to convert a string "8.95" to an integer (8.95) that made me question if even Int is good enough and as suggested in the comments here, use BigInt or equivalent for anything to do with numbers.

This also happens with Javascript and Elixir, not just Ruby. So perhaps it has something to do with how the cpu stores the numbers?

I know this isn't StackOverflow, but could someone shed some light on to what's happening here? https://ideone.com/1U7rgf

Re: Using floating-point numbers for money

#38
Disclaimer: 30+ year veteran at fixing these fucking bugs.

My opinion: just NO.

MULTIPLE failures to understand the problem have occurred in this article, and they deal with representation.

A) It is never acceptable to 'round up' on interest payments/loans/debts/balances, without having an explicit line item for why. "Because compiler decision made by programmer" is indefensible.

B) Representation is EVERYTHING. Case in point:

>>>>0.1 + 0.2: Produces 0.30000000000000004 but should be exactly 0.3.

This statement can kill.

C) Standards are there to keep everyone, and everything, 'honest', and by honest it means: everyone understands things implicitly. Explain to grandma why a rounding error has resulted in her teeth falling out, or GTFO.

D) Fixed point is a standard representation. Its not about the precision-fail, its about if everyone gets exactly the same results, every time, in doing the math.

E) Lots and lots of these bugs yet to be fixed.

Re: Using floating-point numbers for money

#39

A lot of financial firms use an integer of the smallest indivisible unit of currency wherever possible (stripe is one). This has the handy property that string and numeric representations are identical so no risk of being flipped into scientific notation by some layer. That happens more often than you’d think. FX is a different world. Spot FX trading systems are fully automated and compute rates to 5 DP. That was 15…

For shits and giggles:

    $ python3
    Python 3.7.4 (default, Sep  7 2019, 18:27:02)
    [Clang 10.0.1 (clang-1001.0.46.4)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> print (0.1 + 0.1 + 0.1)
    0.30000000000000004

    $ irb
    2.5.1 :001 > print 0.1 + 0.1 + 0.1
    0.30000000000000004 => nil

    Chrome browser console:
    > 0.1 + 0.1 + 0.1
    

Re: Using floating-point numbers for money

#40
post #24
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 would argue that financial math by definition needs to be accurate to the penny. Yes, indeed. There is no such thing as "financial math that does not need to be accurate to the penny". I wonder if OP has ever worked with a bookkeeper...

Bookkeeping needs to be accurate to the penny. Estimating growth for the next quarter or last years GDP doesn't.
Post reply on HN