I can't believe the alternative method I just saw to calculate the average of two numbers:
I use the following:
(a+b)/2 = avrg(a,b)
(b+a)/2 = avrg(a,b)
Found someone using this:
a+((b-a)/2) = avrg(a,b)
b+((a-b)/2) = avrg(a,b)
How to calculate avrg(a,b,c)
using the second method? (e.g for the first one is (a+b+c)/3
)
How can I transform the first one into the second one, or otherwise find some proof they both are equally equal?
b - a
looks a good bit easier thana + b
. To average 83 and 183 I'd rather add 50 to 83 than add 183 to it. Naturally my PC doesn't care one way or the other, unless it overflows on one of the two formulae but not the other. – Steve Jessop Jun 06 '14 at 00:38b > a
, or you'll need to use a sign bit anyway. – Keith Jun 06 '14 at 03:09unsigned
variables, or you have a reasonable guarantee that the values are in fact of the same sign: for example, if you're dealing with real-world units, salaries, time, or many other things. – wchargin Jun 07 '14 at 05:00