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

 Respuesta aceptada

Matt Fig
Matt Fig el 7 de Jun. de 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 comentario

Nick
Nick el 7 de Jun. de 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!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 7 de Jun. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by