MATLAB: How to test MATLAB’s stored value of PI for relative and absolute error

pi

I don't understand how I would estimate an absolute error of matlabs' stored value of pi. From what I understand, 16 digits are displayed, but is that what is stored? I'm trying to take those 16 digits as the result value, and use "Pi" as the true value when I compute absolute error. But what I get isn't the answer, as it should be C * 10^-16 , where c is the answer.
What I really don't get is, isn't this a really stupid question? We rely on computers to generate the next new digits of pi, or the next largest prime number. How is it even possible to take a never-ending value (which we receive from computer code) and find the absolute error (using the same coding platform?)

Best Answer

If you have a finite approximation, then you cannot find the absolute error relative to an irrational value: you can only find bounds on the absolute error.
The actual value stored is
>> fprintf('%.999g\n', pi)
3.141592653589793115997963468544185161590576171875
However, you would need to be using a Mac to get this exact value with that command. The Windows libraries are quite broken for this purpose; the Linux libraries are less broken but still cannot display exact value. For Windows or Linux, you should probably grab https://www.mathworks.com/matlabcentral/fileexchange/22239-num2strexact-exact-version-of-num2str
Without that contribution, you could still use
>> num2hex(pi)
ans =
'400921fb54442d18'
and then analyze the IEEE 754 representation given there. This shows you the hex version of the exact bit pattern used. You might want to try it on your own system: if it gives you the same hex then you can rely upon the decimal value that I posted.
If you have the symbolic toolbox, then
>> vpa(sym(pi,'f'),50)
ans =
3.141592653589793115997963468544185161590576171875
which shows the exact value stored.
>> vpa(pi,100)
ans =
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068
which shows pi itself rounded to 100 digits.
The symbolic toolbox recognizes the bit pattern for the numeric value stored in pi and typically converts it to the internal symbol representing the irrational number. You can be extra careful with that by using sym('pi') instead of pi