2.4 Programming Examples on Loss of Precision and Round-offErrors 31
Using single precision results in a standard deviation ofσ= 40. 05720139 for the first and
most used algorithm, while the exact answer isσ= 36. 80579758 , a number which also results
from the above second algorithm. With double precision, thetwo algorithms result in the
same answer.
The reason for such a difference resides in the fact that the first algorithm includes the
subtraction of two large numbers which are squared. Since the average value for this exam-
ple isx= 100063. 00 , it is easy to see that computing∑ix(i)^2 −x∑ix(i)can give rise to very
large numbers with possible loss of precision when we perform the subtraction. To see this,
consider the case wherei= 64. Then we have
x^264 −xx 64 = 100352 ,
while the exact answer is
x^264 −xx 64 =100064!
You can even check this by calculating it by hand.
The second algorithm computes first the difference betweenx(i)and the average value.
The difference gets thereafter squared. For the second algorithm we have fori= 64
x 64 −x= 1 ,
and we have no potential for loss of precision.
The standard text book algorithm is expressed through the following program, where we
have also added the second algorithm
http://folk.uio.no/mhjensen/compphys/programs/chapter02/cpp/program6.cpp
// program to calculate the mean and standard deviation of
// a user created data set stored in array x[]
using namespacestd;
#include
intmain()
{
int i;
float sum, sumsq2, xbar, sigma1, sigma2;
// array declaration with fixed dimension
floatx[127];
// initialise the data set
for( i=0; i < 127 ; i++){
x[i] = i + 100000.;
}
// The variable sum is just the sum over all elements
// The variable sumsq2 is the sum over x^2
sum=0.;
sumsq2=0.;
// Now we use the text book algorithm
for( i=0; i < 127; i++){
sum += x[i];
sumsq2 += pow((double) x[i],2.);
}
// calculate the average and sigma
xbar=sum/127.;
sigma1=sqrt((sumsq2-sumxbar)/126.);
/
Here comes the second algorithm where we evaluate
separately first the average and thereafter the
sum which defines the standard deviation. The average
has already been evaluated through xbar
*/