Floats Are Weird
a.exozy.me
Floats Are Weird
1–10 of 120 posts
Re: Floats Are Weird
#2Re: Floats Are Weird
#31.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.
Re: Floats Are Weird
#4Re: Floats Are Weird
#5Re: Floats Are Weird
#6 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-15), 0)
1.0000000000000004
Both are within 1 ulp of the more precise value from WolframAlpha: 1.0000000000000005000000000000001666666666666667083333333333333416...Re: Floats Are Weird
#7Then 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.
Re: Floats Are Weird
#8Nothing 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.
See comparison between f and g functions.
Re: Floats Are Weird
#9Then 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.
Re: Floats Are Weird
#10Nothing 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.
It’s just that, due to the quirks of the floating point calculations involved, the numerator and denominator wind up being nearly the same noisy approximation to x, whereas in the original calculation that wasn’t true.