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

 채택된 답변

Matt Fig
Matt Fig 2011년 6월 7일

0 개 추천

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

Nick
Nick 2011년 6월 7일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

질문:

2011년 6월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by