I need to implement the following calculation:

f = (a*b) + (c*d)

where a,b,c,d are given double values and f is a double variable for

the result

I found out that using two different implementations gives two

different results:

1) In the first implementation I compute the entire equation in memory

2) In the second implementation I store intermediate to variables, and

then load them to compute the final result.

The results differ by a tiny amount (1E-15). I suspect that the reason

for the difference is from round off errors of the compiler.

Nonetheless, I need to know the reason for the difference as I need to

have an absolute reference for optimization work.

The two ways are as follows:

1)

Calculating two double values and then summing them to a single double

value

double f1 = (a*b) + (c*d)

2)

Calculating two double values and storing them to two double variables

Summing the two double variables to a single double value

double e1 = (a*b);

double e2 = (c*d);

double f2 = e1 + e2;

The difference is calculated as follows:

double diff = f2-f1;

I expect the difference to be exactly 0 but instead, I get a tiny

value.

Could someone explain the reason for differences in the result?

I’m compiling with gcc

Thanks

Avi