if(0.41 == (41*0.01)) is False and if(0.42 == (42*0.01)) is True .. Why?

1 回表示 (過去 30 日間)
mathmach
mathmach 2020 年 7 月 30 日
コメント済み: mathmach 2020 年 7 月 30 日
In following script, I am getting 'false' for first condition and 'true' for second condition.
clc
clear all
if(0.41 == (41*0.01)) %First Condition
disp('true')
else
disp('false')
end
if(0.42 == (42*0.01)) %Second Condition
disp('true')
else
disp('false')
end
  2 件のコメント
mathmach
mathmach 2020 年 7 月 30 日
Thanks for these links :)
Now I got it and will read more on it.

サインインしてコメントする。

採用された回答

KSSV
KSSV 2020 年 7 月 30 日
Read about comparing floating-point numbers.
tol = 10^-5 ;
if(abs(0.41-(41*0.01)<=tol)) %First Condition
disp('true')
else
disp('false')
end
if(abs(0.42 - (42*0.01))<=tol ) %Second Condition
disp('true')
else
disp('false')
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImage Data Workflows についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by