如何获得加权平均值,其中最新值的权重是所有先前值的两倍?

How to get weighted average where latest value is weighted twice as all previous?

以前用过,就是想不起来了,也找不到。

这是一个 运行 平均值,其中最新值的权重是所有先前值(总和)的两倍,因此随着时间的推移,最旧的值的影响越来越小。而且我没有足够的内存来存储旧值。

求平均值:

int sum=0;
int n = 0;
float aver = 0;
for(;;){
  float new_value = some_function();
  sum += new_value;
  ++n;
  aver = sum / n;  
}

但是如何获得 new_value 的权重是之前平均值的两倍的平均值?

float aver = 0;
for(;;){
  float new_value = some_function();
  aver = aver * ??? + new_value * ???;

}

a running average where the newest value has twice the weight of all the previous values, ...

how to I get an average where the new_value is weighted to be twice the weighting of the previous average?

计算总和和最新的特殊平均值。

int sum = 0;
int n = 0;
float average_special = 0.0;
for(;;) {
  float new_value = some_function();
  sum += new_value;
  ++n;
  average_special = (sum + new_value) / (n+1);  
}