MATLAB Answers


How do I test MATLAB's stored value of PI for relative and absolute error?

Asked by Yakub Shalmiyev on 15 Sep 2018
Latest activity Commented on by Walter Roberson
on 16 Sep 2018
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?)


Sign in to comment.


1 Answer

Answer by Walter Roberson
on 16 Sep 2018
 Accepted 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)
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
Without that contribution, you could still use
>> num2hex(pi)
ans =
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 =
which shows the exact value stored.
>> vpa(pi,100)
ans =
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


Thanks for helping me out again, Walter.
I have one question for you, why does this command show the stored value?
>> vpa(sym(pi,'f'),50)
I understand that 52 bits are stored for 16 digits, 11 bits for 2^E, and 1 bit for the sign. How does this come into play here?
sym() has four ways to convert double precision values to symbolic numbers. The default is the same as if you specify the option 'r'. That examines the numeric values to determine whether the value is within tolerance of being one of the special values such as pi, and if not then it does a partial fraction reduction to try to find rational fractions or square roots that it is within tolerance of. The 'd' option just takes the ieee bit pattern in terms of mantissa and exponent and constructs the rational fraction directly. The 'f' option tells it to use a software floating point representation instead of a rational representation. There is also a 'e' option that involves error estimates.

Sign in to comment.