Summary: Calculate average where sum exceed double limits

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

According to the highest vote in:

http://stackoverflow.com/questions/1930454/what-is-a-good-solution-for-calculating-an-average-where-the-sum-of-all-values-e

You can calculate the mean iteratively. This algorithm is simple, fast, you have to process each value just once, and the variables never get larger than the largest value in the set, so you won't get an overflow.

1 double mean(double[] ary) {
2   double avg = 0;
3   int t = 1;
4   for (double x : ary) {
5     avg += (x - avg) / t;
6     ++t;
7   }
8   return avg;
9 }
原文地址:https://www.cnblogs.com/EdwardLiu/p/6358595.html