필터 지우기
필터 지우기

Relational Operators Issue - 1 is not equal to 1.000

조회 수: 4 (최근 30일)
usr0815
usr0815 2012년 5월 29일
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!

채택된 답변

Thomas
Thomas 2012년 5월 29일
  댓글 수: 1
Walter Roberson
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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by