why the these two equal values when comparing gives 0 as result ?
16 views (last 30 days)
Show older comments
>> fsern(221)
ans =
6.0000
>> infqs(5)
ans =
6
>>infqs(5)==fsern(221)
ans =
0
2 Comments
James Tursa
on 24 Jun 2019
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.
Answers (2)
Adam Danz
on 24 Jun 2019
Edited: Adam Danz
on 24 Jun 2019
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 Comments
Steven Lord
on 24 Jun 2019
>> 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.
See Also
Categories
Find more on Numbers and Precision in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!