why there is a small error in these simple calculations?

1 visualización (últimos 30 días)
Zixuan Li
Zixuan Li el 11 de Abr. de 2019
Editada: Stephen23 el 11 de Abr. de 2019
3e-4-3*10^-4
sum=0;
for i=1:1000
sum=sum+0.001;
end
sum_output=sum
this gives an answer of -5.421010862427522e-20 and 1.000000000000001
it should be obviously 0 and 1. what happened during the running of program??
  1 comentario
Stephen23
Stephen23 el 11 de Abr. de 2019
Editada: Stephen23 el 11 de Abr. de 2019
"it should be obviously 0 and 1. "
Nope. Not with binary floating point numbers.
Search this forum for "floating point" and you will learn the reasons why this happens.

Iniciar sesión para comentar.

Respuestas (1)

Star Strider
Star Strider el 11 de Abr. de 2019
See the documentation on Floating-Point Numbers (link).

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by