我正在尝试计算浮动数组的平均值。我需要使用索引,因为它在二进制搜索中,因此顶部和底部将移动。(大图,我们正在尝试优化半程估计,因此我们不必在每次通过时都重新创建阵列)。
无论如何,我编写了一个自定义的平均循环,与c#Average()方法相比,我得到的精度低2位
float test = input.Average();
int count = (top - bottom) + 1;//number of elements in this iteration
int pos = bottom;
float average = 0f;//working average
while (pos <= top)
{
average += input[pos];
pos++;
}
average = average / count;
Run Code Online (Sandbox Code Playgroud)
例:
0.0371166766-C# 0.03711666-我的循环 125090.148-C# 125090.281-我的循环