After comma precision tracking for if statement

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일

0 개 추천

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

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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2021년 5월 18일

편집:

2021년 5월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by