c++中的 Kahan算法



我编写了以下kahan算法;当n从100万到1000万时,它是合理的,但是当n为1亿时,它会产生非常大的误差。我测试了很多次,但我不知道为什么会发生这种情况。

#include <iostream>
#include <vector>
#include <random>
#include <cfloat>
#include <iomanip>
using namespace std;
float KahanAlgorithm(const vector<float> &myarray){
float sum{0.0f};
float ac{0.0f};
for(unsigned int i=0; i<myarray.size();i++){
float temp{sum+myarray[i]};
if(sum>=myarray[i]){
ac=ac+(sum-temp)+myarray[i];
}
else{
ac=ac+(myarray[i]-temp)+sum;
}
sum=temp;
}
return sum+ac;
}
int main() {
int n=100000000;
random_device r;
default_random_engine g(r());
uniform_real_distribution<float> d(0.f, nextafter(1.f, DBL_MAX));
vector<float> a(n);
vector<double> b(n);
for(auto i=0;i<n;i++){
a[i]=d(g);
b[i]=static_cast<double> (a[i]);
}
double exact_sum;
float kahan_sum;
exact_sum=accumulate(b.begin(),b.end(),0.0);
cout<<"exact "<<exact_sum<<endl;
kahan_sum=KahanAlgorithm(a);
cout<<" Kahan sum "<<kahan_sum<<endl;
return 0;
}

sum =

exact 5.00045e+07
Kahan sum 3.35544e+07

我做了一个快速测试,使用我之前写的Kahan求和的实现,并将其与您的比较:

#include <cfloat>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <numeric>
#include <random>
#include <vector>
namespace Kahan {
template <class InIt>
typename std::iterator_traits<InIt>::value_type accumulate(InIt begin, InIt end) {
typedef typename std::iterator_traits<InIt>::value_type real;
real sum = real();
real running_error = real();
for ( ; begin != end; ++begin) {
real difference = *begin - running_error;
real temp = sum + difference;
running_error = (temp - sum) - difference;
sum = temp;
}
return sum;
}
}
using namespace std;
float KahanAlgorithm(const vector<float> &myarray){
float sum{0.0f};
float ac{0.0f};
for(unsigned int i=0; i<myarray.size();i++){
float temp{sum+myarray[i]};
if(sum>=myarray[i]){
ac=ac+(sum-temp)+myarray[i];
}
else{
ac=ac+(myarray[i]-temp)+sum;
}
sum=temp;
}
return sum+ac;
}
int main() {
int n=100000000;
random_device r;
default_random_engine g(r());
uniform_real_distribution<float> d(0.f, nextafter(1.f, DBL_MAX));
vector<float> a(n);
vector<double> b(n);
for(auto i=0;i<n;i++){
a[i]=d(g);
b[i]=static_cast<double> (a[i]);
}
double exact_sum;
float kahan_sum;
exact_sum=accumulate(b.begin(),b.end(),0.0);
cout<<"exact "<<exact_sum<<endl;
kahan_sum=KahanAlgorithm(a);
cout<<" Kahan sum "<<kahan_sum<<endl;
float jerry = Kahan::accumulate(a.begin(), a.end());
cout << "Jerry's implementation of Kahan: " << jerry << "n";
return 0;
}

样本结果:

exact 4.99944e+07
Kahan sum 3.35544e+07
Jerry's implementation of Kahan: 4.99944e+07

相关内容

  • 没有找到相关文章

最新更新