I understand that the normal distribution is undefined if the standard deviation is zero, but I need to handle the case where all values are equal in a computer algorithm. The following method must return a valid value, even if the standard deviation is zero. How can I fix this method so it does not divide by zero?
public static double NormalDist(double x, double mean, double standard_dev)
{
double fact = standard_dev * Math.Sqrt(2.0 * Math.PI);
double expo = (x - mean) * (x - mean) / (2.0 * standard_dev * standard_dev);
return Math.Exp(-expo) / fact;
}
My idea was to insert this at the beginning of the method:
if (standard_dev == 0.0)
{
return x == mean ? 1.0 : 0.0;
}
Would this be correct?