MATLAB Answers

Comparative Operator result is wrong

19 ビュー (過去 30 日間)
여춘 박
여춘 박 2021 年 4 月 12 日 10:12
コメント済み: Stephen Cobeldick 2021 年 4 月 12 日 17:31
I tried to run the if statement but this happens.
The result must be true(=1) in the last instruction but it returns false(=0)...
Anyone knows what is the problem???

回答 (3 件)

KSSV 2021 年 4 月 12 日 10:22
Note that you are comparing two floating point numbers and such numbers cannot be comapred using ==. Follow
tol = 10^-5 ;
idx = abs(val1-val2)<tol
  1 件のコメント
여춘 박
여춘 박 2021 年 4 月 12 日 14:31


Steven Lord
Steven Lord 2021 年 4 月 12 日 13:13
Two numbers that are displayed the same may not represent the same stored value. See this Answers post for more information about floating point numbers.
  1 件のコメント
여춘 박
여춘 박 2021 年 4 月 12 日 14:32


Community Treasure Hunt

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

Start Hunting!

Translated by