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 Comment

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

Sign in to comment.

 Accepted Answer

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 Comment

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

Sign in to comment.

Categories

Find more on Rubik's Cube in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!