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

前端 未结 17 2158

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条回答
  •  -上瘾入骨i
    2020-11-29 19:12

    Why so many complicated long answers. Here is the simplest way to find the running average till now without any need to know how many elements or size etc..

    long int i = 0; double average = 0; while(there are still elements) { average = average * (i / i+1) + X[i] / (i+1); i++; } return average;

提交回复
热议问题