On 02/13/2013 03:48 PM, FG wrote:
You can use reduce and put the division and subtraction into the reduce itself
to prevent overflows. You also won't end up with jaw-dropping tuples, sorry. :)

     float[] a = [10_000.0f, 10_001.0f, 10_002.0f];
     auto n = a.length;
     auto avg = reduce!((a, b) => a + b / n)(0.0f, a);
     auto var = reduce!((a, b) => a + pow(b - avg, 2) / n)(0.0f, a);
     auto sd = sqrt(var);
     writeln(avg, "\t", sd);

Output: 10001   0.816497

Nice :-)

The thing that's desirable about the example given in the docs is that it's a one-pass approach to getting standard deviation (or rather, mean and standard deviation together). Two reduce commands as you suggest is fine for small data, but if you have something really large you'd rather compute it in one pass. You might also have input that is not [easily] repeatable, so you _have_ to do it online -- imagine that your input range is e.g. a file byLine (you'd prefer to avoid parsing that twice), or input from a tape (OK, OK, old fashioned) or perhaps data from an external source.

Reply via email to