RMS 计算结果始终为零

时间:2021-05-11 05:41:26

标签: c#

我想从列表中的一堆十进制数计算 RMS 值。但结果总是零。有什么建议吗?

list0.Add(0.5);
list0.Add(0.5);
list0.Add(0.5);
list0.Add(0.6);
            
//RMS
double square = 0;
double mean = 0, root = 0;
int n = list0.Count;

for (int i = 0; i < n; i++)
{
    square += (int)Math.Pow(list0[i], 2);                
}
mean = (square / (int)(n));
root = (double)Math.Sqrt(mean);

Console.WriteLine(root);

1 个答案:

答案 0 :(得分:2)

我修改了你的代码:我从程序中删除了不必要的 int 类型转换。

请试试:

var list0 = new List<double>();

list0.Add(0.5);
list0.Add(0.5);
list0.Add(0.5);
list0.Add(0.6);

//RMS
double square = 0;
double mean = 0;
double root = 0;
int n = list0.Count;

for (int i = 0; i < n; i++)
{
    square += Math.Pow(list0[i], 2);
}

mean = square / n;
root = Math.Sqrt(mean);

Console.WriteLine(root);