Alex Rivera | Logout

Difference in accuracy with floating point division vs multiplication

Asked 2011-06-26T07:18:30.270
9

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.

Edit
Report

1 Answer

2

You do not gain precision from this since IEEE754 (which is probably what you're using under the covers) gives you the same precision (number of bits) at whatever scale you're working. For example 3.14159 x 107 will be as precise as 3.14159 x 1010.

The only possible advantage (of the former) is that you may avoid overflow when setting the deviations. But, as long as the values themselves are less than half of the maximum possible, that won't be a problem.

answered 2011-06-26T07:43:09.403

Your Answer