我试图找出一系列双值的“加权移动平均线”。
我试图从一些互联网示例中获得所有和平,但结果总是为零。
问题是“权重”的计算,它是零,但它不应该为零,例如1/107 = 0,0093457943925234但是权重倍数值变为零,我尝试改变为长和小数并得到相同问题
有什么想法吗?
public static double WeighteedMovingAverage(double[] data)
{
double aggregate = 0;
double weight;
int item = 1;
int count = data.Count();
foreach (var d in data)
{
weight = item / count;
aggregate += d * weight;
count++;
}
return (double)(aggregate / count);
}
答案 0 :(得分:3)
weight = (double)item / (double)count;
需要double
以避免在操作前进行投射
答案 1 :(得分:0)
public static double WeighteedMovingAverage(double[] data)
{
double aggregate = 0;
double weight;
int item = 1;
int count = data.Count();
foreach (var d in data)
{
//replace with line below weight = item / count;
weight = (double)item / (double)count;
aggregate += d * weight;
count++;
}
//replace with line below return (double)(aggregate / count);
return (double)(aggregate / (double)count);
}