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 Kommentar

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

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

James Tursa
James Tursa am 11 Jul. 2011

2 Stimmen

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 Kommentar

James Tursa
James Tursa am 11 Jul. 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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Rubik's Cube finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by