# mean of two floating point numbers: why $a+\frac{b-a}{2}$ is better than $\frac{a+b}{2}$?

$$a$$ and $$b$$ are the floating point representation of two real numbers with no constraints (they can be both negative or both positive or one positive and the other negative and so on).

I read in the Armadillo codebase, a linear algebra library, that the "robust" mean of the two $$a$$ and $$b$$ is computed as $$a+\frac{b-a}{2}$$ and not as the (naïve) $$\frac{a+b}{2}$$.

I imagine that it is more robust for what regard the overflow that it can happen when $$a$$ and $$b$$ are similar to half the largest representable value; moreover I feel that if $$a\approx b$$ then the cancellation error incurring in their subtraction will not be a problem.

But, how one can rigorously prove these intuitions?

• Are the numbers positive? Do we know that $a\leq b$? – Arthur Aug 5 '20 at 11:58
• It should be just to avoid exceeding INT_MAX = 2147483647 (or any maximum integer allowed) when calculating a+b. – VIVID Aug 5 '20 at 12:35
• dl.acm.org/doi/10.1145/2493882 – Dhanvi Sreenivasan Aug 5 '20 at 12:35
• @VIVID: But it is counter-productive in that regard if $a$ and $b$ have opposite signs. – TonyK Aug 5 '20 at 12:36
• Yes, it was relative to my first comment. For the original question, a more general case was extensively discussed in math.stackexchange.com/questions/907327/… – Lutz Lehmann Aug 5 '20 at 18:32