After comma precision tracking for if statement

2 ビュー (過去 30 日間)
Ibrahim A
Ibrahim A 2021 年 5 月 18 日
編集済み: Alan Stevens 2021 年 5 月 19 日
I am having this number a=85.0093792283283 calculated in the code and I want to compare with another similar value something like this b=85.0093791964294. My question I just want to compare first 3 digit after the comma. Is there way to implement this in the example code?
if(a == b)
disp('Number are equal');
end

回答 (1 件)

Alan Stevens
Alan Stevens 2021 年 5 月 18 日
編集済み: Alan Stevens 2021 年 5 月 19 日
Here's one possibility
a=85.0093792283283;
b=85.0093791964294;
a = floor(a*10^3);
b = floor(b*10^3);
if a==b
disp('numbers are equal')
end
  2 件のコメント
Ibrahim A
Ibrahim A 2021 年 5 月 18 日
Does something like this also work?
if round(a,4) == round(b,4)
Jan
Jan 2021 年 5 月 18 日
@Ibrahim A: Yes, round is better for your problem.

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

カテゴリ

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