Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Precision error with two mathematically identical expressions around zero

When plotting two mathematically equivalent expressions very close to zero we get two similar results, but one of the curve has steps instead of being smooth.

1-cosh(x) == -2*sinh(x/2)^2

Zero sum

Now a quick observation reveals that the height of the step is indeed equal to the precision of Matlab, i.e. the variable eps = 2.2204e-16 = 2^-52

This graph was introduced with the name "zero sum", obviously not referencing a zero sum game. But apparently this only occurs with results of additions (or substractions) being very close to zero.

However, to my knowledge calculations with floating point numbers (or doubles) are similar in precision regardless of the scale at which the calculations are being made. So I'd expect error to only creep when something really big is being operated on with something really small, in which case the smaller number gets rounded off.

Matlab code to reproduce this:

x = linspace(-5*10^-8, 5*10^-8, 1001);
y1 = @(x) 1 - cosh(x);
y2 = @(x) -2*(sinh(x/2)).^2;

plot(x,y1(x),'k',x,y2(x),'r')
legend('1-cosh(x)', '-2sinh(x/2)^2')

Can someone explain how this.. works?

like image 947
Felix Avatar asked Dec 12 '25 18:12

Felix


1 Answers

The rounding happens in the cosh function. If you plot it and zoom in to the graph at the same scale, you'll see the same staircase-like effect, but centered around 1 on the y-axis.

That is because you cannot represent those intermediate values using doubles.

like image 131
Cris Luengo Avatar answered Dec 14 '25 13:12

Cris Luengo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!