Search code examples
matlabfloating-pointrounding-error

Matlab sin(pi) and its relation to machine epsilon


I understand that the reason why sin(pi) is not equal to zero is because of the fact that there is not enough bits to store all the significant digits of "pi", but what does that have to do with machine epsilon?

I read online what machine epsilon was, but after an hour of reading various definitions worded differently I got confused and did not understand the concept of epsilon. I ended up getting really frustrated in my own foolishness...

This following example was given in the MATLAB documentation and I don't understand it, can someone explain to me what the example is trying to show?

Find the distance from 10.0 to the next largest double-precision number.

d = eps(10.0)

d =

   1.7764e-15

http://www.mathworks.com/help/matlab/ref/eps.html


Solution

  • There are a couple of different definitions of machine epsilon, but Matlab eps is fairly typical, being the gap between 1.0 and the next largest double precision floating point number.

    We can actually make this more general: for any floating point number between 2kx < 2k+1, the gap between x and the next largest floating point number is 2k × eps (i.e. eps(x) in Matlab). Moreover, the gap between any real number and its nearest floating point approximation is half this.

    Since 2 ≤ π < 4;, this means that the gap between pi (the numerical approximation) and π (the exact irrational number) is bounded by eps. In actual fact, it is just over half that:

    eps ≈ 2.22 × 10-16

    |pi - π| ≈ 1.22 × 10-16

    Now using the result from @aka.nice answer, and that sin(π) = 0, we have that

    sin(pi) = | sin(pi) - sin(π) | ≈ |pi - π| < eps

    i.e. it is also bounded by eps.

    Note: there is also some slight rounding between sin(pi) (the numeric result) and sin(pi) (the exact result), but this is of order eps2, so can be ignored in this case.