why the these two equal values when comparing gives 0 as result ?

조회 수: 17 (최근 30일)
Hindu Rajasree
Hindu Rajasree 2019년 6월 24일
편집: Adam Danz 2019년 6월 27일
>> fsern(221)
ans =
6.0000
>> infqs(5)
ans =
6
>>infqs(5)==fsern(221)
ans =
0
  댓글 수: 2
Hindu Rajasree
Hindu Rajasree 2019년 6월 24일
why its showing result as zero even if the two values are equal pls explain me
James Tursa
James Tursa 2019년 6월 24일
FYI, MATLAB is actually giving you a clue. Those trailing 0's in the display of 6.0000 mean that there are non-zero digits after that but are not being displayed, whereas the 6 without the trailing 0's mean that the number is 6 exactly.

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

답변 (2개)

Adam Danz
Adam Danz 2019년 6월 24일
편집: Adam Danz 2019년 6월 24일
6.00000001 == 6
ans =
0
To see if precision is the issue, one or both lines below should be false.
fsern(221) == 6.0
infqs(5) == 6.0
also set your output format to 'long'
format long
  댓글 수: 6
Steven Lord
Steven Lord 2019년 6월 24일
>> x = 0.6/0.1
x =
6.0000
In exact arithmetic, x would be exactly equal to six since six-tenths divided by one-tenth is six.
>> x-6
ans =
-8.8818e-16
In floating-point arithmetic, 0.6 is not exactly six-tenths and 0.1 is not exactly one-tenth, so 0.6 divided by 0.1 is not exactly six. You can't exactly represent either 0.6 or 0.1 in floating point; you have some roundoff error in each.
>> x == 6
ans =
logical
0
x is very close to 6. The == operator doesn't care how close it is. To the == operator x is not exactly equal to 6, so == returns false.
If you want "close enough" to count, compare using a tolerance as shown in the "Compare Floating-Point Numbers" section on this documentation page.
Adam Danz
Adam Danz 2019년 6월 25일
편집: Adam Danz 2019년 6월 27일
@Hindu Rajasree, to calculate equality when faced with roundoff error, you can follow this example:
A = 6.0;
B = 0.6/0.1;
TF = abs(A-B) < 1e4*eps(min(abs(A),abs(B)));
% True if equal within tolerance
% false otherwise

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


Hindu Rajasree
Hindu Rajasree 2019년 6월 24일
tq soo much for ur explanation

카테고리

Help CenterFile Exchange에서 Numbers and Precision에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by