Home  >  Article  >  Backend Development  >  C# code example for calculating standard deviation equivalent to STDEV function in Excel

C# code example for calculating standard deviation equivalent to STDEV function in Excel

黄舟
黄舟Original
2017-03-24 11:47:142344browse

The following editor will bring you an articleC# Calculating the standard deviation is equivalent to the STDEVfunction example in Excel. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look.

The examples are as follows:

public static float StDev(float[] arrData) //计算标准偏差
    {
      float xSum = 0F;
      float xAvg = 0F;
      float sSum = 0F;
      float tmpStDev = 0F;
      int arrNum = arrData.Length;
      for (int i = 0; i < arrNum; i++)
      {
        xSum += arrData[i];
      }
      xAvg = xSum / arrNum;
      for (int j = 0; j < arrNum; j++)
      {
        sSum += ((arrData[j] - xAvg) * (arrData[j] - xAvg));
      }
      tmpStDev = Convert.ToSingle(Math.Sqrt((sSum / (arrNum - 1))).ToString());
      return tmpStDev;
    }

The above is the detailed content of C# code example for calculating standard deviation equivalent to STDEV function in Excel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn