Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in accuracy with floating point division vs multiplication

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.

like image 370
Fakrudeen Avatar asked Jun 26 '11 07:06

Fakrudeen


People also ask

Is floating point multiplication faster than division?

High-performance code often favors multiplication over division because multiplication is faster on most modern processors.

How much slower is division than multiplication?

And the results (see the comments) are similar to that of Intel: division is about 3-6 times slower than multiplication.

Is division slower than multiplication python?

Division is much slower, than multiplication. But some smart compliers / VMs transform division into multiplication, so your tests will have the same results (both tests test multiplication).

Is multiplying by 0.5 faster than dividing by 2?

Yes, indeed, there is a difference. A loop with a million multiplies by 0.5 took 0.11 seconds and a loop with a million divides by 2 took 1.6 seconds. So it's true for the RPG (and probably for the IBM i) that multiplying is quicker than dividing.


2 Answers

There's nothing to be gained from this. You are only changing the scale but you'd don't get any more significant figures in your calculation.

The Wikipedia article on variance explains at a high level some of the options for calculation variance in a robust fashion.

like image 102
David Heffernan Avatar answered Oct 13 '22 18:10

David Heffernan


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.

like image 37
paxdiablo Avatar answered Oct 13 '22 17:10

paxdiablo