Upload files to "ex1D_kahan_summation"
This commit is contained in:
parent
c70572f1b9
commit
9098bcc99a
3 changed files with 97 additions and 0 deletions
33
ex1D_kahan_summation/main.cpp
Normal file
33
ex1D_kahan_summation/main.cpp
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#include "mylib.h"
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
for(size_t i = 1; i < 8; ++i)
|
||||
{
|
||||
size_t n = pow(10,i);
|
||||
vector<double> x(n);
|
||||
for (size_t k = 0; k < n; ++k)
|
||||
x[k] = 1.0/(k + 1);
|
||||
|
||||
|
||||
// compute scalar products
|
||||
double sum_1 = scalar(x, x);
|
||||
double sum_2 = Kahan_skalar(x, x);
|
||||
|
||||
// compute error
|
||||
double err_1 = abs(sum_1 - pow(M_PI,2)/6);
|
||||
double err_2 = abs(sum_2 - pow(M_PI,2)/6);
|
||||
|
||||
cout << "n = " << n << endl;
|
||||
cout << "Normal scalar product: " << sum_1 << "\terror: " << err_1 << endl;
|
||||
cout << "Kahan scalar product: " << sum_2 << "\terror: " << err_2 << endl;
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue