How can this be true? (<) operator not working

I noticed there had to be a bug in some of my code so I went into the debugger. While in the debugger I noticed a while loop not terminating when it was supposed to. Here is the root of the problem:
increment =
1.0000e-01
K>> increment < .1
ans =
1
How can this be happening?

1 comentario

Jason
Jason el 11 de Jul. de 2011
some extra information
format hex
.1 =
3fb999999999999a
increment =
3fb9999999999999
Why is this happening and what can I do? Thanks.

Iniciar sesión para comentar.

 Respuesta aceptada

James Tursa
James Tursa el 11 de Jul. de 2011

2 votos

0.1 cannot be represented exactly in IEEE double precision floating point format. Welcome to the world of floating point arithmetic. You will have to adjust your algorithms (e.g., use tolerances on comparisons, etc) to account for this effect.

1 comentario

James Tursa
James Tursa el 11 de Jul. de 2011
P.S. If you want to see the exact decimal conversion for any IEEE double or single number you can use this utility:
http://www.mathworks.com/matlabcentral/fileexchange/22239-num2strexact-exact-version-of-num2str

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 11 de Jul. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by