MATLAB: Why – if kk = num (where kk = 6.0000 and num = 6 ) gives me false

if statement

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

Best Answer

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.