A problem with floating point arithmetic

Respond=[0.000065;0.000032;0.000120;0.000231];
for i=0.000000:0.000001:0.000350
for k=1:4
if Respond(k,1)==i
fprintf('i: %d\n',i);
fprintf('k: %d\n',k);
end
end
end
Problem: it prints only the 2 of the 4 slots of table Respond :S Any idea?
Thanks

 Accepted Answer

You are seeing the limits of floating point arithmetic. This is why it is not recommended to use the equality operator on floating point numbers....
What is it you actually want to do??
Here is the difference:
Respond=[0.000065;0.000032;0.000120;0.000231];
for i=0:0.000001:0.000232
for k=1:4
if abs(Respond(k)-i)<20*eps
fprintf('i: %20.20f\n',i);
fprintf('Respond(k): %20.20f\n',Respond(k))
end
end
end

1 Comment

I am doing a simulation about a network and working with 10^-7 timeslots..But i think that i will use higher numbers.Insted of 0.000001 i will use 1.
Thnx a lot for the answer!

Sign in to comment.

More Answers (0)

Categories

Asked:

on 7 Jun 2011

Community Treasure Hunt

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

Start Hunting!