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 ExchangeStartup 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