#include "mylib.h" #include // assert() #include #include using namespace std; double scalar(vector const &x, vector const &y) { assert(x.size() == y.size()); size_t const N = x.size(); double sum = 0.0; for (size_t i = 0; i < N; ++i) { sum += x[i] * y[i]; } return sum; } double Kahan_skalar(vector const &x, vector const &y) { double sum = 0; double c = 0; size_t n = x.size(); for (size_t i = 0; i < n; ++i) { double z = x[i]*y[i] - c; // c is the part that got lost in the last iteration double t = sum + z; // when adding sum + z, the lower digits are lost if sum is large c = (t - sum) - z; // now we recover the lower digits to add in the next iteration sum = t; } return sum; }