0

for a homework assignment I need to create two equivalent vectors, x1 and x2, using two different methods in Matlab. This should be pretty straight forward however I am getting an error I do not understand.

x1 = linspace(2,14,61);
x2 = 2:.2:14;

These should create equivalent vectors correct? However, the 8th element of each is supposedly "different".

x1(8)
    ans =

    3.4000
x2(8)
    ans = 

    3.4000

x1(8) == x2(8)
    ans =

        0

Can someone explain to me why this is? All indexes not a multiple of 8 do not exhibit this behavior. Only the 8th, 16th, etc.

  • Hmm this might be it. The examples in that answer also hold for matlab (ex .1+.2 ~= .3) – lpbrown999 Jan 15 '16 at 22:18
  • 1
    I've re-closed this question with a better duplicate as you are specifically asking for behaviour in MATLAB. However, the previous duplicate link still applies with regards to floating-point precision in its behaviour and its mutual frustration shared by all programmers. – rayryeng Jan 15 '16 at 22:23
  • For completeness, the previous duplicate link can be found here: http://stackoverflow.com/questions/588004/is-floating-point-math-broken – rayryeng Jan 15 '16 at 22:28

0 Answers0