Hi,

I need to convert a complicated Excel document that's been created by some Maths boffin into a program in C#. I've been doing really well (despite my Mathematical handicap) until I hit the Excel NORMDIST(x,mean,stdDev,cumulative) function. System.Math doesn't have any functions to help me out here, and I just can't seem to work out how to convert the formula into C# code.

Can anyone help?

LittleC

I need to convert a complicated Excel document that's been created by some Maths boffin into a program in C#. I've been doing really well (despite my Mathematical handicap) until I hit the Excel NORMDIST(x,mean,stdDev,cum

Can anyone help?

LittleC

You can see the help topic with the formula also here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/office97/html/sec02.asp

More info:

http://support.microsoft.com/default.aspx?kbid=827371&product=xl2003

What normal distribution is:

http://pirate.shu.edu/~wachsmut/Teaching/MATH1101/Testing/distribution.html

System.Diagnostics.Debug.W

System.Diagnostics.Debug.W

produces the output:

0.908788780275089

0.109340049783996

which matches Microsoft Excel's help file:

NORMDIST(42,40,1.5,TRUE) = 0.908789

NORMDIST(42,40,1.5,FALSE) = 0.10934005

### C#.NET CODE ###

/// <summary>

/// Normal Distribution

/// </summary>

/// <param name="x">The value for which you want the distribution.</param>

/// <param name="mean">The arithmetic mean of the distribution.</param>

/// <param name="deviation">The standard deviation of the distribution.</param>

/// <param name="cumulative">If cumulative is true, functions returns the cumulative distribution, otherwise the function returns the probability mass.</param>

/// <returns>Returns the normal distribution for the specified mean and standard deviation.</returns>

public static double NormalDistribution(double x, double mean, double deviation, bool cumulative)

{

if(cumulative)

return CumulativeDistribution(x, mean, deviation);

else

return NormalDensity(x, mean, deviation);

}

private static double NormalDensity(double x, double mean, double deviation)

{

return Math.Exp(-(Math.Pow((x - mean)/deviation, 2)/2))/Math.Sqrt(2*Math.PI

}

private static double CumulativeDistribution(dou

{

// TODO: Change the number of iterations (16) for more or less precision.

// You could also change the logic of the recursive function (stop calling

// for more terms, when the values are below a specific threshold for example.

return (ErrorFunction((x - mean)/deviation/Math.Sqrt(

}

private static double ErrorFunction(double x, int iteration, int iterations)

{

double partValue;

partValue = 2/Math.Sqrt(Math.PI)*Math.

if(iteration==iterations)

return partValue;

else

return ErrorFunction(x, iteration + 1, iterations) + partValue;

}

private static int Factorial(int x)

{

if(x==0)

return 1;

else

return x*Factorial(x-1);

}

###

Don't trust me though - I'd have the experts at https://www.experts-exchange.com/Miscellaneous/Math_Science/ or someone else take a look at this code.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.