フィルターのクリア

Equality operator error with exact same value

1 回表示 (過去 30 日間)
Glenn Franco Gacal
Glenn Franco Gacal 2022 年 4 月 13 日
コメント済み: Steven Lord 2022 年 4 月 14 日
I have the following code and it presents a bug:
data = 22.7:0.1:24.2; TEMP(1) = 23.1;
if TEMP(1) == data(5)
3-2 %this is only sample effect
else
5-2 %this is only sample effect
end
This outputs
3
But if I use
if TEMP(1) == 23.1
3-2 %this is only sample effect
else
5-2 %this is only sample effect
end
This outputs
1
You can see from the "data" matrix that data(5) is 23.1.
What kind of bug is this and how can I avoid this kind of bug in the future? I've tried 2 different PCs with different MATLAB versions and they both have the same problem.

採用された回答

Steven Lord
Steven Lord 2022 年 4 月 13 日
This is not a bug. See the section "Avoiding Common Problems with Floating-Point Arithmetic" on this documentation page.
  2 件のコメント
Glenn Franco Gacal
Glenn Franco Gacal 2022 年 4 月 14 日
Thank you for your clarification and I apologize for this noob dilemma!
Steven Lord
Steven Lord 2022 年 4 月 14 日
No need to apologize. You aren't the first person to encounter this behavior (it's a common enough question that our documentation staff created a documentation page for it, after all) and you almost certainly won't be the last.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by