0

I can’t wrap my head around this output from the Octave terminal:

>> y
y =    
   0.064900   0.195800   0.045100   0.694200
>> z
z =
   0.064900   0.195800   0.045100   0.694200
>> y == z
ans =
   1   1   0   1
>> y(1) == z(1)
ans =  1
>> y(3) == z(3)
ans = 0

How is this possible? They become equal if I copy and paste the values.

macleginn
  • 321
  • 2
  • 10
  • Don't test floating point equality using `==`. Instead, calculate the vector of absolute element-wise differences between `x` and `y`. If and only if the difference is very small, the elements are sort of equal. –  Nov 23 '16 at 09:46
  • That seems to be it, thanks! I worked with a couple of languages and have never run into this problem with only four digits of precision. – macleginn Nov 23 '16 at 10:05
  • 1
    It's not 4 digits of precision, it's only *displaying* 5 significant digits (by default) and the difference between elements is smaller than that. If it were a problem of precision, the values would be equal. Type `format long` to see more digits, or `fprintf('%.17g\n', y)`. – beaker Nov 23 '16 at 19:47

0 Answers0