0

I tried belows on MATLAB.

A = rand(2, 20);
B = rand(20, 1);
D1 = A * B;
D1 = D1(1);
D2 = A(1, :) * B;
D1 == D2

ans =

0

D1 - D2

ans =

-8.8818e-16

I expected that two results are the exactly same. What differences between both calc ways?..

Thanks,

Hong
  • 15
  • 5
  • 2
    See: [What Every Computer Scientist Should Know About Floating-Point Arithmetic](https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html). They're the same. – sco1 Dec 08 '17 at 02:23
  • 4
    Possible duplicate of [Why is 24.0000 not equal to 24.0000 in MATLAB?](https://stackoverflow.com/questions/686439/why-is-24-0000-not-equal-to-24-0000-in-matlab) – sco1 Dec 08 '17 at 02:23
  • Isnt the OP asking how is Matlab implementing this two operations that there is this "difference"? I know it is in the range where one would say they are equal, but still it is interesting that Matrix multiplication and vector times matrix result are different. Your links are pointing into the direction where a comparison is made with a fixed value. But one would expect that `a*b` is always `a*b` and the difference will be zero – Irreducible Dec 08 '17 at 06:18
  • Yes, that is exactly what I was about to say. I wonder why those both results of a*b are different. – Hong Dec 08 '17 at 08:57
  • @Irreducible within the limits of floating point comparison the two values *are* the same. The delta is well within machine precision. Nothing about this is novel, it's a fundamental limitation of floating point approximation. – sco1 Dec 08 '17 at 14:39
  • @excaza, I completely agree with you that within the limits of floating point comparison these values are the "same". However, I am still wondering, cause these results do not origin from different operations or a fix comparison with a integer but from the same operation, namely `a*b`, I would expect that no matter how often I calculate this on a machine, the result should always be the same or in other words the difference is always zero, so the question sustains: how does the implementation differ? – Irreducible Dec 08 '17 at 14:55
  • @Irreducible I don't agree with the assumption that the two operations above are both *exactly* `a*b`. – sco1 Dec 08 '17 at 14:57
  • @excaza, that is what my first comment pointed out, how do they differ? (" how is Matlab implementing this two operations ?") – Irreducible Dec 08 '17 at 14:58
  • @Irreducible get a job at TMW and you may eventually be able to find out. – sco1 Dec 08 '17 at 14:59
  • @excaza as no one is able add something substantial here ,I guess we are stopping this conversation! However, it is clear that they are different and the question of the OP is not answered! – Irreducible Dec 08 '17 at 15:02
  • Guys relax...I agree with Irreducible's opinion.. But I think no one can exactly explain about this. – Hong Jan 10 '18 at 10:57

0 Answers0