I have a requirement to calculate the average of a very large set of doubles (10^9 values). The sum of the values exceeds the upper bound of a double, so does anyone know a
Please clarify the potential ranges of the values.
Given that a double has a range ~= +/-10^308, and you're summing 10^9 values, the apparent range suggested in your question is values of the order of 10^299.
That seems somewhat, well, unlikely...
If your values really are that large, then with a normal double you've got only 17 significant decimal digits to play with, so you'll be throwing away about 280 digits worth of information before you can even think about averaging the values.
I would also note (since no-one else has) that for any set of numbers X
:
mean(X) = sum(X[i] - c) + c
-------------
N
for any arbitrary constant c
.
In this particular problem, setting c = min(X)
might dramatically reduce the risk of overflow during the summation.
May I humbly suggest that the problem statement is incomplete...?