Relational Operators Issue - 1 is not equal to 1.000
7 ビュー (過去 30 日間)
古いコメントを表示
Hey there,
the following CODE (MATLAB 2011b) results in an unexpected problem, which I'd like to know the reason for:
x = 0;
y = 1;
for i = 1:10
x = x + 0.1;
end
Result:
x = 1.0000
y = 1
The use of some relational operators to compare x and y
a = y == x;
b = y < x;
c = y > x;
Results:
a = 0
b = 0
c = 1
It seems as if the value of x after passing the for-loop is less than 1, even though the workspace displays x = 1.0000... and y = 1.
Actually I experienced this problem in some measurement automation loop and solved it by using
d = y == round(x*100000)/100000;
Result:
d = 1
The question arises: Is this the only/best way to solve this problem or is there a better work-around - maybe a generalized solution - that avoids such problems in advance!
Additional information: Both variables x and y are DOUBLE after the creation.
Thanks for your help!
0 件のコメント
採用された回答
Thomas
2012 年 5 月 29 日
1 件のコメント
Walter Roberson
2012 年 5 月 29 日
Yes, see that FAQ.
You are only displaying a few digits after the decimal place, and for *display* purposes the value you are displaying rounds to 1.000 but that does not mean that the value *is* 1
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!