Why is this if statement does not work?

1 回表示 (過去 30 日間)
Cantor Set
Cantor Set 2020 年 2 月 20 日
編集済み: madhan ravi 2020 年 2 月 20 日
if (oval1x ~= sObjVal1(1) || oval1x ~= sObjVal1(end))
cd=1;
else
cd=Inf;
end
it happened that oval1x=4.2139; and sObjVal1(1)=4.2139; but the loop did not work correctly
Edit: is there a way to correct this issue?

採用された回答

madhan ravi
madhan ravi 2020 年 2 月 20 日
編集済み: madhan ravi 2020 年 2 月 20 日
Because they are float numbers, reading the below links might help you better about floating numbers:
You could add tolerance to satisfy the conditions , search inside the forum with the tag "floating point" you would find lots of explanations about it.
  2 件のコメント
Cantor Set
Cantor Set 2020 年 2 月 20 日
I don't understand
madhan ravi
madhan ravi 2020 年 2 月 20 日
編集済み: madhan ravi 2020 年 2 月 20 日
because maybe oval1x is not exactly 4.2139 if you use sprintf('%.32f',oval1x) you could see that its not exactly the same value as you think.
An example to illustrate:
>> x = 1.24213
x =
1.2421
>> x == 1.2421 % false
ans =
logical
0
>> (x - 1.2421) < 1e-4 % true where 1e-4 is the tolerance
ans =
logical
1
>>

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by