Difference in accuracy with floating point division vs multiplication

Viewed 1831

Is there a difference between this:

average = (x1+x2)/2;
deviation1 = x1 -average;
deviation2 = x2 -average;
variance = deviation1*deviation1 + deviation2*deviation2;

and this:

average2 = (x1+x2);
deviation1 = 2*x1 -average2;
deviation2 = 2*x2 -average2;
variance = (deviation1*deviation1 + deviation2*deviation2) / 4;

Note that in the second version I am trying to delay division as late as possible. Does the second version [delay divisions] increase accuracy in general?

Snippet above is only intended as an example, I am not trying to optimize this particular snippet.

BTW, I am asking about division in general, not just by 2 or a power of 2 as they reduce to simple shifts in IEEE 754 representation. I took division by 2, just to illustrate the issue using a very simple example.

4 Answers
Related