Live data from Hacker News

Floats Are Weird

a.exozy.me

11–20 of 120 posts

Re: Floats Are Weird

#11
post #8
post #3

Nothing weird about it. It should be obvious that subtracting two floats that are very close to each other results in a loss of numerical precision: 1.000000003456e0 - 1.000000002345e0 = 0.000000001111e0 = 1.111numericalnoise e-9 It's exactly the same issue here. `math.exp(1e-15)` is `1.000000000000001`. If you subtract 1, you get 1 significant digit and numerical noise.

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

Re: Floats Are Weird

#12

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

Re: Floats Are Weird

#13
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

It’s not (or shouldn’t be), it’s simply a result of math, as the article explains in length.

Re: Floats Are Weird

#14
post #6

For this specific case, use Python's expm1, see https://docs.python.org/3/library/math.html#math.expm1 and history of expm1 at https://en.wikipedia.org/wiki/Exponential_function#expm1 def f(x): return math.expm1(x)/x The expm1(x) means "exp(x) minus 1". >>> f(1e-15) 1.0000000000000007 The technique described gives 1.0000000000000004 which is 1 step smaller than the value computed via expm1(): >>> math.nextafter(f(1e-…

To be precise, it’s C’s `expm1`. Like other things in math.h, it’s just adopted as is to most popular languages, even including PHP and JS.

Re: Floats Are Weird

#15

Then what's the best way to handle these cases? Are there any set of rules we should use while implementing the mathematical equations dealing with limits in floating points.

Basically

- don't subtract almost equal numbers

- don't add numbers of vastly different magnitudes

Re: Floats Are Weird

#16
post #7

Then what's the best way to handle these cases? Are there any set of rules we should use while implementing the mathematical equations dealing with limits in floating points.

For a summation, add the smaller numbers first. Smaller as in 0.0000000000053, not like -5172365126.

how about small as in ²³⁷

Re: Floats Are Weird

#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.

Re: Floats Are Weird

#18
post #7

Then what's the best way to handle these cases? Are there any set of rules we should use while implementing the mathematical equations dealing with limits in floating points.

For a summation, add the smaller numbers first. Smaller as in 0.0000000000053, not like -5172365126.

For summation just go with Kahan summation or some improved variant of it https://en.wikipedia.org/wiki/Kahan_summation_algorithm

Re: Floats Are Weird

#19
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.

Is it fraud to willingly/knowingly use floats for money?

Re: Floats Are Weird

#20
post #14
post #6

For this specific case, use Python's expm1, see https://docs.python.org/3/library/math.html#math.expm1 and history of expm1 at https://en.wikipedia.org/wiki/Exponential_function#expm1 def f(x): return math.expm1(x)/x The expm1(x) means "exp(x) minus 1". >>> f(1e-15) 1.0000000000000007 The technique described gives 1.0000000000000004 which is 1 step smaller than the value computed via expm1(): >>> math.nextafter(f(1e-…

To be precise, it’s C’s `expm1`. Like other things in math.h, it’s just adopted as is to most popular languages, even including PHP and JS.

expm1 is specified in IEEE 754-2008 (didn't bother to check old revisions)
Post reply on HN