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

 Réponse acceptée

Matt Fig
Matt Fig le 7 Juin 2011
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 commentaire

Nick
Nick le 7 Juin 2011
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!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Startup and Shutdown 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