Is it a bug?

조회 수: 4 (최근 30일)
Haixiao Xian
Haixiao Xian 2020년 2월 9일
댓글: Star Strider 2020년 2월 9일
I found a weird situation with the number "1.4"
v=0:0.01:5;
vd=1.4;
a=0;
for i=1:length(v)
if v(i)==vd
a=1
end
end
This code doesn't make the value of "a" become "1." However, if I change the value of "vd" to any other numbers (eg. vd=1.5), this code works well. Also, if I change the range of "v" to "v=0:0.01:2", it works as well. I don't know what's wrong with my orignal codes. Can anyone help me? Thank you very much!

채택된 답변

Star Strider
Star Strider 2020년 2월 9일
You have encountered ‘floating-point approximation error’.
See Floating-Point Numbers for an extended discussion.
Also see colon for an explanation of how it calculates the numbers in your ‘v’ vector.

추가 답변 (1개)

Image Analyst
Image Analyst 2020년 2월 9일
Yes, you do have a bug in your program.
bugs.gif
You compared floating point numbers with == instead of ismembertol().
See the FAQ for fixes.
  댓글 수: 1
Star Strider
Star Strider 2020년 2월 9일
+1

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by