Why do I get empty double vector
古いコメントを表示
I'm a new user of Matlab and I have met a weird problem. I tried to create a simple function like this:
y=0:0.0025:2.5;
x=2*y;
disp(x(y==1.15));
and I will get a 1x0 empty double row vector instead of 2.3. When I tried y==1.12 or y==1.16, I could get the correct result. I suppose that might because of the double integer but I don't really understand.
回答 (1 件)
James Tursa
2020 年 11 月 10 日
編集済み: James Tursa
2020 年 11 月 10 日
0 投票
This is normal floating point arithmetic behavior. None of 0.0025 or 1.15 or 1.12 or 1.16 can be represented in IEEE double precision exactly, so you will get small differences showing up when compared to exact decimal representations in calculations, and these differences can propagate through further calculations. If this makes a difference to your algorithm, you will need to rewrite your algorithm to account for these effects. E.g., using tolerances when making comparisons.
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!