How to compute standard deviation accurately
The most convenient way to compute sample variance by hand may not work in a program. Sample variance is given by
If you compute the two summations and then carry out the subtraction above, you might be OK. Or you might have a large loss of precision. You might get a negative result even though in theory the quantity above cannot be negative. If you want the standard deviation rather than the variance, you may be in for an unpleasant surprise when you try to take your square root.
There is a simple but non-obvious way to compute sample variance that has excellent numerical properties. The algorithm was first published back in 1962 but is not as well known as it should be. Here are some notes explaining the algorithm and some C++ code for implementing the algorithm.
Accurately computing running variance
The algorithm has the added advantage that it keeps a running account of the mean and variance as data are entered sequentially.