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

1 view (last 30 days)
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
Jason
Jason on 11 Jul 2011
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

James Tursa
James Tursa on 11 Jul 2011
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
James Tursa
James Tursa on 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

Sign in to comment.

More Answers (1)

Sean de Wolski
Sean de Wolski on 11 Jul 2011

Categories

Find more on Introduction to Installation and Licensing 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!