Show all decimal places

355 visualizaciones (últimos 30 días)
Tamara Szecsey
Tamara Szecsey el 17 de Oct. de 2017
Editada: Walter Roberson el 5 de Mzo. de 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 comentarios
Travis Briles
Travis Briles el 13 de Abr. de 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 el 14 de Abr. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

KSSV
KSSV el 17 de Oct. de 2017
Editada: KSSV el 17 de Oct. de 2017
doc format. Read about format.
format long
should show what you want.

Más respuestas (2)

Matthew Murrian
Matthew Murrian el 11 de Jun. de 2020
Editada: Matthew Murrian el 12 de Jun. de 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 comentarios
Matthew Murrian
Matthew Murrian el 12 de Jun. de 2020
Editada: Matthew Murrian el 12 de Jun. de 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 el 5 de Mzo. de 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.

Iniciar sesión para comentar.


Jan
Jan el 17 de Oct. de 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 comentarios
Felipe Jiménez Hernández
Felipe Jiménez Hernández el 5 de Mzo. de 2024
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 el 5 de Mzo. de 2024
Editada: Walter Roberson el 5 de Mzo. de 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.

Iniciar sesión para comentar.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by