Dear all,

I'm doing some work with images, and I've come across the strangest thing:

I have an array of float values, if I initialize it to zero and sum a series of floats on the array itself I obtain one result.
On the other hand if I use an auxiliary variable as an accumulator and after summing all values I assign it to the corresponding position in my array, the result is slightly different to the former one.

I have no idea why this is and would appreciate if anyone could give me any tips here. I'm using g++ 4.3.3.
I post below the excerpt of the code in question and briefly comment on the variables.

- nCols, nRows: numbers of columns and rows of my images.
- u, meanu, normu: different images. The data elements are of type unsigned char for 'u' and float for the others.
- _(img, x, y): this is an accessor macro. The data is just stored in an array, so the macro computes the offset corresponding to the indexes x and y.

The commented lines are the alternative that yields different results.

Code:
for (int x = limit; x < nCols - limit; x++) {
    for (int y = limit; y < nRows - limit; y++) {
        //float accum = 0.0;
        _(normu,x,y) = 0.0;

        for (int i = -halfWindow; i <= halfWindow; i++) {
            for(int j= -halfWindow; j<= halfWindow; j++) {
                //  accum+=(float)(_(u,x+i,y+j)-_(meanu,x,y))*(_(u,x+i,y+j)-_(meanu,x,y));
                
                 _(normu,x,y)+=(float)(_(u,x+i,y+j)-_(meanu,x,y))*(_(u,x+i,y+j)-_(meanu,x,y));
            }
        }
	//_(normu,x,y)=accum;
    }
}
You can think of _(normu,x,y) as normu->gray[x + y*nCols], were gray is an array of type float.

If you need any more details let me know and I'll be happy to provide them, I tried to keep the post as short as possible.

Thank you!