Math Is Fun.

I was working out a better way to handle light detection when the player is within range of multiple lights. I decided to dump all values into a giant list but needed some helper methods to pull specific information out of the list to determine the amount of light on the player. Below is a simple class I wrote that will allow you to find the Mean of all values in a list as well as the Average of all values in a list. This is setup for doubles and floats only, you could easily make it work with int’s though.

just call it like:

double meanFloat = MathIsFun.Mean(theListYouWant);
double meanDbl = MathIsFun.Mean(theListYouWant);
float average = MathIsFun.Average(theListYouWant);

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MathIsFun {

  public static double Mean(System.Collections.Generic.List<float> Values) {
    if(Values.Count == 0)
      return 0.0;
    double ReturnValue = 0.0;
    for(int x = 0; x < Values.Count; x++) {
      ReturnValue += Values[x];
    }
    return ReturnValue / (float)Values.Count;
  }

  public static double Mean(System.Collections.Generic.List<double> Values) {
    if(Values.Count == 0)
      return 0.0;
    double ReturnValue = 0.0;
    for(int x = 0; x < Values.Count; x++) {
      ReturnValue += Values[x];
    }
    return ReturnValue / (double)Values.Count;
  }

  public static float Average(System.Collections.Generic.List<float> Values) {
    if(Values.Count == 0) {
      return 0.0f;
    }
    float ReturnValue = 0.0f;
    for(int x = 0; x < Values.Count; x++) {
      ReturnValue += Values[x];
    }
    return ReturnValue / (float)Values.Count;
  }

  public static float Sum(System.Collections.Generic.List<float> Values) {
    if(Values.Count == 0) {
      return 0.0f;
    }
    float ReturnValue = 0.0f;
    for(int x = 0; x < Values.Count; x++) {
      ReturnValue += Values[x];
    }
    return ReturnValue;
  }
}

facebooktwittergoogle_plusredditpinterestlinkedinmail