This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Double precision accumulatior accuracy problem

Hi, I have bump into some accuracy problem with double precision accumulator. Please refer to the code below:

for (i=start; i<=stop; i++){
sum_x += (float64)i;
sum_y += pdata[i];
sum_xy += (float64)i * pdata[i];
sum_xSquared += (float64)i * (float64)i;
}

I have four different double precision accumulation occur in the same loop, but due to high precision demanding on my algorithm, the accumulation results gave the algorithm lots of errors. But then I've splitted the accumulation into two separated loops as below, the algorithm returns correct result and passed the test. So, I just like to have an explaination on why putting all accumulators in one loop will not give me accurate results. Thank you.

for (i=start; i<=stop; i++){
sum_x += (float64)i;
sum_y += pdata[i];
}

for (i=start; i<=stop; i++){
sum_xy += (float64)i * pdata[i];
sum_xSquared += (float64)i * (float64)i;
}