# why 2 vectors are not the same in matlab?

I have 2 vectors:

a = 1.0e-04*[0.110000000000000   0.140000000000000   0.140000000000000   0.140000000000000];
b =         [0.000011000000000   0.000014000000000   0.000014000000000   0.000014000000000];


by look at by eyes, it seems similar; but when I use this command:

a == b


then I got:

ans =
1×4 logical array
0   0   0   0


The expectation is:

ans =
1×4 logical array
1   1   1   1


Does anyone knows what wrong with this command in the matlab?

Thank you.

• See this SO question.
– Peter K.
May 17, 2017 at 20:37
• One way to fix it can be abs(a-b)<1e-15.
– msm
May 18, 2017 at 0:14

Floating point numbers are stored with a limited precision according to the IEEE754 conventions (search for this term in the net for further details).

Neither 0.11 nor 1E-4 can be exactly represented in floating point arithmetic. And the product of 2 numbers will introduce an extra error.

Testing for equality in floating point arithmetic is therefore more complex than it seems.

Have you tried subtracting them? I'm pretty sure this is due to the limited precision of the floating point numbers

• I tried, and I got all zeros, such as: [ 0.0000000000 -0.0000000000 0.0000000000 0.0000000000 0.0000000000] May 17, 2017 at 19:35
• @phuc nguyen Despite that you have what seems to be a zero vector, it has actually been rounded down. May 17, 2017 at 20:42
• @phucnguyen That negative sign in front of one of the numbers should make you suspect that these are not actually zeros. See also the documentation for the format command..
– MBaz
May 17, 2017 at 22:46
• @Envidia: if they are rounded down, anyway to detect those very small numbers (for ex: 0.00000000000000002), I still cant detect them even use the format long command May 18, 2017 at 13:19