0
$\begingroup$

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.

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

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

$\endgroup$
1
$\begingroup$

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.

$\endgroup$
0
$\begingroup$

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

$\endgroup$
  • $\begingroup$ I tried, and I got all zeros, such as: [ 0.0000000000 -0.0000000000 0.0000000000 0.0000000000 0.0000000000] $\endgroup$ – phuc nguyen May 17 '17 at 19:35
  • $\begingroup$ @phuc nguyen Despite that you have what seems to be a zero vector, it has actually been rounded down. $\endgroup$ – Envidia May 17 '17 at 20:42
  • $\begingroup$ @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.. $\endgroup$ – MBaz May 17 '17 at 22:46
  • $\begingroup$ @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 $\endgroup$ – phuc nguyen May 18 '17 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.