Vector with values from 0 to 15 with 1e-4 step

10 visualizaciones (últimos 30 días)
Alexandre Englert
Alexandre Englert el 16 de Mzo. de 2021
Comentada: Alexandre Englert el 18 de Mzo. de 2021
Hi,
I am creating a vector for storing values from 0 to 15 with a 1e-4 interval:
x = [0:1e-4:15];
When displaying the values with the default format ("format short"), everything seems ok. For example, the value of x(143274) is displayed as 14.3273. However, when using the "format long" command, the value shown for x(143274) is 14.327299999999999 instead of 14.327300000000000. Why is this small difference being shown? I was expecting the difference between each value in the vector to be exactly 0.000100000000000 (which is not the case, for example, if one calculates x(143274)-x(143273) in the command window).
Thanks,
Alexandre
  2 comentarios
Stephen23
Stephen23 el 17 de Mzo. de 2021
Editada: Stephen23 el 17 de Mzo. de 2021
x = [0:1e-4:15];
% ^ ^ superfluous square brackets.
Square brackets are a concatenation operator. You are not concatenating anything together here, so those square brackets do nothing (except mislead). Get rid of them.
Alexandre Englert
Alexandre Englert el 18 de Mzo. de 2021
Thank you for the hint.

Iniciar sesión para comentar.

Respuesta aceptada

the cyclist
the cyclist el 16 de Mzo. de 2021
Editada: the cyclist el 16 de Mzo. de 2021
Because they are floating-point numbers, and cannot be represented exactly on a binary computer.
Take a look at this answer for a bit more detail, and links to even more detail.

Más respuestas (0)

Categorías

Más información sobre Data Type Identification en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by