why its showing result as zero even if the two values are equal pls explain me
why the these two equal values when comparing gives 0 as result ?
조회 수: 17 (최근 30일)
이전 댓글 표시
>> fsern(221)
ans =
6.0000
>> infqs(5)
ans =
6
>>infqs(5)==fsern(221)
ans =
0
댓글 수: 2
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
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
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Numbers and Precision에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!