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 commentaire

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

Connectez-vous pour commenter.

 Réponse acceptée

James Tursa
James Tursa le 11 Juil 2011

2 votes

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 commentaire

James Tursa
James Tursa le 11 Juil 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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Rubik's Cube dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by