tl;dr Variables in MATLAB use double precision. The same precision is used for variables in both the Command Window and the Variable Editor. The only difference is the display format and the number of decimal places of accuracy you print it to.
MATLAB by default uses double precision for its variables. These variables in MATLAB are stored in the Workspace. Both the Command Window and the Variable Editor pull their variables from the same Workspace and hence underneath use the same precision.
The default format for displaying variables in the Variable Editor and the Command Window is the short
format. You can check this for the Command Window with
>> get(0, 'format')
ans =
short
and for the Variable Editor by going to Preferences -> Variables -> Format
. The short
format by default displays variables to 4 decimal places of accuracy.
It appears to me that you have changed the default display format for variables in your Variable Editor to the long
format. This format displays double variables with 15 decimal places of accuracy. Both the long
and short
formats round variables, so pi
which is 3.14159
gets rounded to 3.1416
because of the 9
in the 5th decimal place when displayed with the short
format
>> format short
>> pi
ans =
3.1416
This is directly equivalent to the output produced by fprintf
>> fprintf(1, '%.4f\n', pi);
3.1416
However, the long
format, which I'm guessing, you've set as the default for your Variable Editor rounds to 15 decimal places and so displays
>> format long
>> pi
ans =
3.141592653589793
which is directly equivalent to
>> fprintf(1, '%.15f\n', pi);
3.141592653589793
When you use fprintf(1, '%.16f\n', pi);
you are printing pi
to 16 decimal places and not 15 as specified by the long
format. This is why your output is
>> fprintf(1, '%.16f\n', pi);
3.1415926535897931
Note, the 1
at the end of this. This is why the 3
directly preceding it isn't rounded to 4
when displayed in your Variable Editor.
Summary
- Variables in MATLAB by default use double precision
- MATLAB variables are stored in the Workspace
- Variables available in the Command Window and the Variable Editor both come from the Workspace and use the same precision
Precalculated Values
In MATLAB you should use the variable name pi180
in function calls or when manipulating other numeric data. This will use double precision and eliminate any copy and paste errors that may arise by using values output by fprintf
or in the Variable Editor.
fprintf
Quirks
tl;dr MATLAB's short
and long
formats switch between the %d
, %.f
, %.g
and %.e
specifiers depending on the most appropriate method for the input.
@horchler pointed out that %.f
is only directly equivalent to the short
and long
formats for specific inputs and this is true. There is no direct equivalent for all inputs between fprintf
and MATLAB's short
and long
formats.
For instance let's look at eps
and 100.5
and try to print the numbers exactly like MATLAB's short
and long
formats.
>> format short
>> eps
ans =
2.2204e-16
>> 100.5
ans =
100.5000
and
>> format long
>> eps
ans =
2.220446049250313e-16
>> 100.5
ans =
1.005000000000000e+02
Now we know from above that fprintf(1, '%.4f\n', pi);
and fprintf(1, '%.15f\n', pi);
are directly equivalent to short
and long
respectively for pi
but are they for eps
and 100.5
>> fprintf(1, '%.4f\n', eps);
0.0000
>> fprintf(1, '%.15f\n', eps);
0.000000000000000
>> fprintf(1, '%.4f\n', 100.5);
100.5000
>> fprintf(1, '%.15f\n', 100.5);
100.500000000000000
No they aren't the only direct equivalent is fprintf(1, '%.4f\n', 100.5);
. What if we try with %.g
?
>> fprintf(1, '%.4g\n', eps);
2.22e-16
>> fprintf(1, '%.15g\n', eps);
2.22044604925031e-16
>> fprintf(1, '%.4g\n', 100.5);
100.5
>> fprintf(1, '%.15g\n', 100.5);
100.5
Now none of the fprintf
statements are directly equivalent. However, we can produce a direct equivalent for eps
using the long
format with
>> fprintf(1, '%.16g\n', eps);
2.220446049250313e-16
because the number directly following the .
for the %g
format specifier specifies the number of significant digits (including those preceding the decimal point, .
) we need to use 16 and not 15.
To produce a direct equivalent for all of these input types for the short
format we need to mix the %.f
, %.g
and %.e
specifiers as well as adjusting the field width.
>> format short
>> pi
ans =
3.1416
>> eps
ans =
2.2204e-16
>> 100.5
ans =
100.5000
>> fprintf(1, '%.4f\n', pi);
3.1416
>> fprintf(1, '%.5g\n', eps);
2.2204e-16
>> fprintf(1, '%.4f\n', 100.5);
100.5000
Not trivial at all. The same can be done for the long
format.
>> format long
>> pi
ans =
3.141592653589793
>> eps
ans =
2.220446049250313e-16
>> 100.5
ans =
1.005000000000000e+02
>> fprintf(1, '%.15f\n', pi);
3.141592653589793
>> fprintf(1, '%.16g\n', eps);
2.220446049250313e-16
>> fprintf(1, '%.15e\n', 100.5);
1.005000000000000e+02
Even worse than for the short
format.
So in short, MATLAB's short
and long
formats switch between the %d
, %.f
, %.g
and %.e
specifiers depending on the most appropriate method for the input.
Additional Reading
You can find information on the different display formats available in MATLAB through the format
documentation. There is also a helpful document on Display Format for Numeric Values. And lastly, there is information about the Variable Editor and its preferences.