# Merging two average values without multiplication

Consider I have two sequences of numbers: 1 and 2. I don't have these sequences exactly but I know their means and numbers of elements in them: mean1, mean2, count1, count2.

I need to calculate mean of both sequences together knowing only means and counts of those sequences separately. The obvious formula is: mean12 = (mean1 * count1 + mean2 * count2)/(count1 + count2).

What I want to know: is there a way to calculate mean12 without multiplying mean1 * count1 and mean2 * count2. The reason for this: for my task mean and count could be huge numbers and multiplication could cause overflow, so I want to know if there is another solution, which avoids multiplication of those huge numbers.

• If you're computing means in a computer program, you are probably working with floating-point numbers, which are much harder to overflow than your regular 32-bit signed integers. Sep 24 at 19:16

How about $$\text{mean}1 \times \frac{\text{count}1}{\text{count}1+\text{count}2} + \text{mean}2 \times \frac{\text{count}2}{\text{count}1+\text{count}2}$$
This way you're only ever multiplying by a number between $$0$$ and $$1$$.
• If this is supposed to be implemented in a computer program, make sure you aren't performing integer division, otherwise $\frac{count1}{count1+count2}$ will likely evaluate to zero. Sep 24 at 19:15
$$\dfrac{\dfrac{mean_1}{count_2}+\dfrac{mean_2}{count_1}}{\dfrac{1}{count_2}+\dfrac{1}{count_1}}$$