MATLAB: How to estimate the difference or “round-off error” between the floating-point representation of a number and the actual value (i.e. “pure mathematical” value)

errorieee754MATLABroundoffSymbolic Math Toolbox

Is there a function in MATLAB which reports the amount of error in the double-precision representation of a number?
For example, if I do the following in MATLAB:
>> a = double(2.2e-11);
Then a will come back when displayed using "format long" as:
a = 2.200000000000000e-11
so it appears there is perfect representation of a. But this is misleading I believe.
For example, I can prove this using another simple example:
d = 0.1;
d1 = d + eps(d)
Both d and d1 will display using format long as:
0.100000000000000
But when displayed using format hex they are different by 1 bit place as expected:
d = 3fb999999999999a
d1 = 3fb999999999999b
Now back to the original question, is there a way to quantify the difference, i.e. error, between the double precision representation of a double precision number like 2.2e-11 (which is 0x3db83073119f21d8) and the pure mathematical number 2.2e-11?
The function eps only tells me the step size for a given floating point value. It doesn't tell me error however. The error will be somewhere between 0 and eps, i.e. it's bounded by eps, but one cannot tell more than that.

Best Answer

I can suggest two approaches to obtain a better estimate of this error -- one via the "fprintf" function and another via the Symbolic Math Toolbox. However, please note that each approach has its limitations, as is included in the details below.
Option 1
You can use the "fprintf" function to displaying additional digits, in comparison to the 15 digits displayed after the decimal point via "format long". For example, to display the double-precision representation of 2.2e-11 with 1074 digits after the decimal point, you can execute the command provided below.
>> fprintf('%.1074f\n', 2.2E-11)
Note that the limitation here is that you would have to manually inspect the digits, and so this might be tedious for certain scenarios, e.g. if the "pure mathematical number" does not have a terminating decimal. Also note that I chose 1074, because there would not be any more than 1074 digits after the decimal point available to display for doubles (<http://www.exploringbinary.com/maximum-number-of-decimal-digits-in-binary-floating-point-numbers/ here is a relevant link>).
Option 2
You can use the "sym" function to get a closer approximation of this "error". The Symbolic Math Toolbox facilitates computations in exact representation, and you can convert between symbolic and numeric (e.g. double precision floating point) values. Note that the "sym" functions accepts the numeric input as is when the numeric input is provided within single quotes. Additional details are provided in the documentation for the "sym" function and the page "Numeric to Symbolic Conversion".
>> str = sprintf('%.1074f',2.2e-11) %ensures that all digits are printed for the double
>> sFloatingPoint = sym(str)
>> sExact = sym('2.2e-11')
>> err = sFloatingPoint - sExact
Note that a limitation in this approach is that some precision can be lost when converting between symbolic and floating-point due to the inherent nature of floating point representation. Hence, the result might not be exactly the error between the floating point representation and the "pure mathematical" value; however, it could provide a closer estimate than that achieved from the "eps" function.