![]() Registered Member ![]()
|
I am trying to calculate x.transpose()*x where x is a vector in two different ways and I don't get the same answer. My code is below
I get fabs(val-valTest)= 5.82077e-09. Is the difference coming from the addition valTest+=x(i)*x(i) ? Many thanks for your help. Compiler is gcc 4.8.1 with flags -std=c++11 -pedantic -Wall -Wextra -Wfatal-errors -g Sree |
![]() Registered Member ![]()
|
This seems to be just the normal roundoff error. You are adding a million numbers, randomly distributed between 0 and 1, so the sum is approximately 500,000. Thus an error of 5e-9 translates to a relative error of 5e-14 or about 200 times epsilon, which seems reasonable for a million operations. If you compare the loop that you wrote by hand with the same loop backwards (as below), you get a similar difference in the result.
|
![]() Registered Member ![]()
|
Makes sense. I ran the loop you suggested to see the result. I got fabs(val-valTest2)= 9.02219e-09 which is comparable to earlier value. Thank you.
Sree |
Registered users: Baidu [Spider], Bing [Bot], Google [Bot], rblackwell