What is a good solution for calculating an average where the sum of all values exceeds a double's limits?

前端 未结 17 2202

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

17条回答
  •  无人及你
    2020-11-29 18:48

    IMHO, the most robust way of solving your problem is

    1. sort your set
    2. split in groups of elements whose sum wouldn't overflow - since they are sorted, this is fast and easy
    3. do the sum in each group - and divide by the group size
    4. do the sum of the group's sum's (possibly calling this same algorithm recursively) - be aware that if the groups will not be equally sized, you'll have to weight them by their size

    One nice thing of this approach is that it scales nicely if you have a really large number of elements to sum - and a large number of processors/machines to use to do the math

提交回复
热议问题