why - if kk = num (where kk = 6.0000 and num = 6 ) gives me false

1 view (last 30 days)
I have a two arrays x1 and x2. At i = 803, x1(i-1) = 4.0000 and x2(i-1)=10
K>> abs(norm(x1(i-1)) - norm(x2(i-1)))
ans =
6.0000
now
K>> abs(norm(x1(i-1)) - norm(x2(i-1))) == 6
ans =
0
Why is matlab not accepting 6.0000 == 6 as true When I just enter
K>> 6.0000==6
ans =
1
this is true, but why not for the variable, I need to use a equal to condition and I cannot

Accepted Answer

James Tursa
James Tursa on 16 Sep 2016
Edited: James Tursa on 16 Sep 2016
The number is close to, but not exactly 6. When a value is exactly 6, MATLAB will display the value without any trailing 0's after the decimal point. If you see 0's printed after the decimal point, that means there are non-zero digits out there that were too small to print using the current display format. E.g.,
>> x = 6
x =
6 <-- exactly 6, so no 0's printed after the decimal point
>> x = 6.00000001
x =
6.0000 <-- not exactly 6, so you get the 0's after the decimal point
>> x-6
ans =
1.0000e-08
Simply subtract 6 from your value to see how far away from 6 your value really is.

More Answers (1)

John D'Errico
John D'Errico on 16 Sep 2016
You THINK the number is 6.0000. But suppose that it was 6.00001, with only that limited number of decimal places reported. What will it look like?
format short
x = 6.00001
x =
6.0000
x == 6
ans =
0
So be careful. Remember that MATLAB always stores roughly 16 decimal digits, even when they are not all reported.
help format

Categories

Find more on Fourier Analysis and Filtering 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!