Show all decimal places

216 views (last 30 days)
Tamara Szecsey
Tamara Szecsey on 17 Oct 2017
Edited: Walter Roberson on 5 Mar 2024
I am using matlab 2016b. I would like to see all decimal places of the numbers I store. For example, the input is:
x = 1.000001
then matlab returns:
x =
1.0000
When substracting 1, matlab is showing the rest by writing
1.0000e-6
So how can I change this, so matlab displays all decimal places that are stored?
  5 Comments
Travis Briles
Travis Briles on 13 Apr 2021
@Walter Roberson I get that but I don't get why the workspace shows many, many more decimals than the command window. Mathematica does basically the same thing but it seems much less egregious than Matlab. Cheers.
Walter Roberson
Walter Roberson on 14 Apr 2021
Preferences -> Command Window -> Text display -> Numeric format
controls the default "format" command for your session; this is which format will be used at the command window each time you start MATLAB
Preferences -> Variables -> Format -> Default array format:
controls the default "format" that is used for the Variable Browser
If I recall correctly, both of those preferences default to "short".
MATLAB computes in IEEE 754 Double Precision by default (except it does not handle NaN exactly the same as in the standard, and a couple of functions operate differently with signed zeros.) The full 53 bits of precision are available, and some people need them (or at least more than 5 decimal digits) so MATLAB gives you the option of looking at them.

Sign in to comment.

Accepted Answer

KSSV
KSSV on 17 Oct 2017
Edited: KSSV on 17 Oct 2017
doc format. Read about format.
format long
should show what you want.

More Answers (2)

Matthew Murrian
Matthew Murrian on 11 Jun 2020
Edited: Matthew Murrian on 12 Jun 2020
num2str accepts precision as a second argument. If you wish to see the "exact value" of a particular floating-point value, you can use num2str with a sufficiently large value for precision. (e.g., 100 to be certain).
x = 1.000001;
num2str(x, 100)
ans = '1.0000009999999999177333620536956004798412322998046875'
You can see the next larger and smaller representable values with num2str(x + eps(x), 100) and num2str(x - eps(x), 100), respectively (not generally true for other values).
NOTE: Behavior is MATLAB version dependent. This may not work for you.
  5 Comments
Matthew Murrian
Matthew Murrian on 12 Jun 2020
Edited: Matthew Murrian on 12 Jun 2020
The warning "if you specify precision to exceed the precision of the input floating-point data type, the results might not match the input values to the precision you specified. The result depends on your computer hardware and operating system" is entirely consistent with my answer.
"If you specify precision to exceed the precision of the input floating-point data type": e.g., if you specify precision to exceed (about) 16 for a double.
"the results might not match the input values to the precision you specified": e.g., the results might not match 1.000001 at and beyond the specified 6 decimal places.
E.g.,
num2str(1.000001, 16) will output exactly 1.000001
num2str(1.000001, 17) may differ from 1.000001 at and beyond the 6th decimal place.
And how that result manifests at and beyond the 6th decimal place is hardware and operating system dependent.
That does not mean that my hardware and operating system is required context for this answer. If num2str does what the documentation says it does then your implementation of num2str will output results that are relevant to your hardware/OS.
And I'll note that this warning does not mention MATLAB version. If different versions of MATLAB, hardware/OS otherwise equal, produce different results from the same command without explicitly documenting that change in behavior... sloppy.
John D'Errico
John D'Errico on 5 Mar 2024
I wonder how many confused people we will see over the years, due to an option like this. For example,
num2str(1/3,30)
ans = '0.333333333333333314829616256247'
Surely some will think that if I specified 30 digits, then why are the last 14 digits in my number incorrect?
Oh well. Just a thought.

Sign in to comment.


Jan
Jan on 17 Oct 2017
Matlab stores floating point values in the IEE754 format, which store them as binary values. Remember that binary numbers need not have an exact decimal format with a limited number of digits, and the other way around. This is the reason for the old effect (not "problem"):
0.1 + 0.1 + 0.1 == 0.3 % FALSE!
Considering this it will be very hard to define uniquely, what "all decimal places" mean. Is 0.1+0.1+0.1 or 0.3 the "correct" answer? Neither format long g nor fprintf('.16g') will be sufficient, if you do not decide at first, what you want to consider as precision.
  5 Comments
Felipe Jiménez Hernández
But in order to know how many zeros must be added, you have to know how many digits of precision are in x. I actually expected char(x) to give me that info visually. If x is any vpa number, how do you find how many digits of precision are in x?
Walter Roberson
Walter Roberson on 5 Mar 2024
Edited: Walter Roberson on 5 Mar 2024
digits_target = 66;
%example number deliberately truncated small
x = vpa(1/23, 52)
x = 
0.04347826086956521739130434782608695652173913043478261
chx = char(x);
dotoffset = regexp(chx, '\.');
padding_needed = digits_target - (length(chx) - dotoffset);
chx = [chx, repmat('0', 1, padding_needed)]
chx = '0.043478260869565217391304347826086956521739130434782610000000000000'
length(chx)
ans = 68
leading 0 is 1 digit, decimal point is 1 digits, then 66 digits after = 68.

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!