Vector initialized as 0:0.01:100 contains entries that are not exact

Hi,
I have a time vector initialized as 0:0.01:100. However, some of the entries are not exactly 0.01 apart. In other words, I am trying to understand why the difference for the two following vectors is not zero:
t1 = 0:0.01:100; % original time vector
t2 = round(t*100)./100; % rounded to two decimals
sum(t-a) % difference
ans =
-3.4989e-013
Thanks

 Respuesta aceptada

James Tursa
James Tursa el 10 de Jun. de 2015
Editada: James Tursa el 10 de Jun. de 2015
Asked and answered many times on this forum. MATLAB uses binary floating point representations of numbers. A decimal number such as 0.01 has no exact representation in this system. E.g., see this link:
Mathematical identities (e.g., multiplying and dividing by the same number) will not always hold true in floating point arithmetic. You simply need to be aware of this and account for it in any code you write.

3 comentarios

Renato
Renato el 10 de Jun. de 2015
Editada: Renato el 10 de Jun. de 2015
Thank you for the quick reply. I should have spent more time looking for the answer on past questions. Would you mind suggesting some previous posts for me to read in which this question was answered in more detail?
The post I listed is a good start, and there are several links in it to other posts you can follow as well.
Thanks again.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 10 de Jun. de 2015

Comentada:

el 10 de Jun. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by