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

16 views (last 30 days)
>> fsern(221)
ans =
6.0000
>> infqs(5)
ans =
6
>>infqs(5)==fsern(221)
ans =
0
  2 Comments
Hindu Rajasree
Hindu Rajasree on 24 Jun 2019
why its showing result as zero even if the two values are equal pls explain me
James Tursa
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.

Sign in to comment.

Answers (2)

Adam Danz
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
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.
Adam Danz
Adam Danz on 25 Jun 2019
Edited: Adam Danz on 27 Jun 2019
@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

Sign in to comment.


Hindu Rajasree
Hindu Rajasree on 24 Jun 2019
tq soo much for ur explanation

Community Treasure Hunt

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

Start Hunting!