我希望你能帮助一些令人头疼的问题。
我写了一个模板类来计算标准偏差:
template <class T>
double GetStandardDeviation(T* valueArray, int populationSize)
{
double average;
T cumulativeValue = 0;
double cumulativeSquaredDeviation = 0;
// calculate average
for (int i = 0; i < populationSize; i++)
{
cumulativeValue += valueArray[i];
}
average = (double)cumulativeValue / (double)populationSize;
// calculate S.D.
for (int i = 0; i < populationSize; i++)
{
double difference = average - (double)valueArray[i];
double squaredDifference = difference * difference;
cumulativeSquaredDeviation += squaredDifference;
}
return cumulativeSquaredDeviation / (double)populationSize;
}
这似乎做的一切都是正确的,除了它只将结果返回到小数点后 5 位。任何人都可以提出一个理由吗?我难住了!