Live data from Hacker News

Floats Are Weird

a.exozy.me

21–30 of 120 posts

Re: Floats Are Weird

#21
post #11
post #8

Earlier quoted context omitted.

That's not what the post says if I understand correctly - the post explains why in certain situations the "noise" disappears, and in other cases it doesn't. See comparison between f and g functions.

I see! yes, the magic is you can cancel the noise by repeating it twice: ``` In [1]: math.exp(1e-15)-1 Out[1]: 1.1102230246251565e-15 In [2]: math.log(math.exp(1e-15)) Out[2]: 1.110223024625156e-15 ``` risky business though, I imagine it's implementation dependent

Agreed, this is risky business. The intermediate values still need to fit into floats and are still losing precision.

From the article:

    g(1e-9)  returns 1.0000000005,
    g(1e-12) returns 1.0000000000005,
    g(1e-15) returns 1.0000000000000004
but... g(1e-16) throws ZeroDivisionError: float division by zero.

Re: Floats Are Weird

#23
That's a neat trick, "cancelling" out catastrophic cancellation. Of course it doesn't work anymore once x is smaller than machine epsilon, whereas expm1(x) / x will continue to work.

In general herbie is pretty good at suggesting the right functions / rearrangements. For this example, it finds expm1: https://herbie.uwplse.org/demo/378a0682ec4d3e735c790a58fa97e...

Re: Floats Are Weird

#24
post #17

I decided years ago that the next time I hear someone suggesting we use floats / doubles to represent money amounts, I am going to punch them in the face.

I mean, it depends on what you're doing.

Assuming you want your money to actually add up correctly, then floats are always the wrong choice.

If you’re not interested in accurate accounting, the. Sure floats are fine, but when you’re working with money, accurate accounting tends to be the expectation.

Re: Floats Are Weird

#25

Earlier quoted context omitted.

I mean, it depends on what you're doing.

Assuming you want your money to actually add up correctly, then floats are always the wrong choice. If you’re not interested in accurate accounting, the. Sure floats are fine, but when you’re working with money, accurate accounting tends to be the expectation.

If you're the one settling the books at your bank, sure. If you just need to display the price of something, a float+money formatter is mostly fine.

Re: Floats Are Weird

#26
post #17

I decided years ago that the next time I hear someone suggesting we use floats / doubles to represent money amounts, I am going to punch them in the face.

This gets repeated a lot, and I don't disagree. But I find odd that doubles would be so unsuitable for monetary (and other similar) arithmetic; in principle you have 15 significant digits which should be more than enough, and precise control how the results are rounded. And all the basic arithmetic should return correctly rounded values to the last ULP. So it is weird that those tools are still not good enough and it is also difficult (at least for me) to fully characterize why exactly they are not suitable.

Part of me wonders if this (justified!) fear of floats is in part because a history of bad implementations (looking at x87) and difficulties in controlling floating-point env (looking at libs randomly poking fpenv), and less due floats intrinsically being bad.

Re: Floats Are Weird

#27
post #17

I decided years ago that the next time I hear someone suggesting we use floats / doubles to represent money amounts, I am going to punch them in the face.

The rule as stated is way too strong, for example option prices are money amounts but floats are unavoidable in calculating them.

For a less exotic example, consider that Excel, widely used by actual accountants, uses floats throughout to represent numbers.

Re: Floats Are Weird

#28

Earlier quoted context omitted.

I mean, it depends on what you're doing.

Assuming you want your money to actually add up correctly, then floats are always the wrong choice. If you’re not interested in accurate accounting, the. Sure floats are fine, but when you’re working with money, accurate accounting tends to be the expectation.

Addition is one of those things that does work pretty predictably and error free with floats. The problem with 0.30000000000000004 etc is usually that the things you are adding are not what you expect (float(0.1) != 0.1), i.e. the difficulty usually is stringfloat conversions rather than float arithmetic itself.

Re: Floats Are Weird

#29

I guess "floats are weird" is a catchier title than "numerical computing is an acquired skill based in part on understanding the various consequences of value representation density being inversely proportional to the absolute value".

I think the title is alright. The curious thing is that both `f` and `g` have catastrophic cancellation, so you expect both to be inaccurate, but magically `g` recovers from it. Alternative title: catastrophic cancellation cancels out

> I think the title is alright

I disagree (for the title I see now, which is “Floats are weird”). The problem isn’t about floats, but about inexact computation and, as you say, catastrophic cancellation.

If, for example, you do all your computations in 7.6 digit base 11 numbers (seven digits before the undecimal point, six after it), you get the same problem.

Post reply on HN