Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

Kahan 求和

减少浮点数误差。

查看代码
1
2
3
4
5
6
7
8
9
10
11
double kahanSum(vector<double> nums)
{
double sum = 0, c = 0;
for (double x : nums)
{
double y = x + c, t = sum + y;
c = y - (t - sum);
sum = t;
}
return sum;
}