Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 any neat little tricks for calculating an average that doesn't require also calculating the sum?

I am using Java 1.5.

like image 520
Simon Avatar asked Dec 18 '09 20:12

Simon


People also ask

How do you average a large number?

Find the average or mean by adding up all the numbers and dividing by how many numbers are in the set.


1 Answers

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.

double mean(double[] ary) {   double avg = 0;   int t = 1;   for (double x : ary) {     avg += (x - avg) / t;     ++t;   }   return avg; } 

Inside the loop avg always is the average value of all values processed so far. In other words, if all the values are finite you should not get an overflow.

like image 118
martinus Avatar answered Sep 22 '22 15:09

martinus