"email@example.com" <firstname.lastname@example.org> wrote in message <email@example.com>... > I have three algorithms to calculate the inner product of vector > A(i,j) and vector V for M x N times. > With the line A = round(A), B1 = B2 = B3. > Without the line A = round(A);, B1 != B2, B2 = B3. > Is this a precision error? > Which one is correct? > ........ - - - - - - - - - When you take the sum of terms (as in an inner product,) if the quantities involved have one-bits in their binary representations occurring at or near their least significant bit positions, the results will generally be subject to round off errors. In this case the order in which the additions are performed may affect the result, though of course only in these least bits.
In other words, with such quantities, the associative law of addition is not strictly adhered to, so that (a+b)+c does not always give exactly the same result as a+(b+c). Try this on your computer:
a = 3/14; b = 3/14; c = 15/14; (a+b)+c == a+(b+c)
This would presumably account for your results when the 'rand' function is used without doing a 'round' on A. The matrix multiply operation in algorithm 1 apparently performs its additions in a different order than is used in the element-by-element multiplication followed by the 'sum' function of algorithms 2 and 3.
The same comment applies to a series of successive multiplications. The associative law of multiplication does not always hold so that the ordering of multiplications would affect the result.
You must always be prepared for differing results in the least few bits of an answer when different, though supposedly mathematically equivalent, series of arithmetic operations are performed, due to differences in rounding errors.