Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better algorithm to find average [closed]

Tags:

c

algorithm

I am doing an exercise of a programming book A Book on C . The exercise suggests that to find average of a group of numbers, algorithm:

avg += (x - avg) / i;

is better than:

sum += x;
avg = sum / i;

'x' is a variable used to store the input numbers. It also suggests beside preventing overflow, the first algorithm do have some other benefits than the second algorthim, can anyone help me? Thanks!

like image 503
Oliver Avatar asked Jun 03 '11 13:06

Oliver


2 Answers

I'm assuming we're talking about floating-point arithmetic here (otherwise the "better" average will be terrible).

In the second method, the intermediate result (sum) will tend to grow without bound, which means you'll eventually lose low-end precision. In the first method, the intermediate result should stay of a roughly similar magnitude to your input data (assuming your input doesn't have an enormous dynamic range). which means that it will retain precision better.

However, I can imagine that as i gets bigger and bigger, the value of (x - avg) / i will get less and less accurate (relatively). So it also has its disadvantages.

like image 70
Oliver Charlesworth Avatar answered Oct 17 '22 18:10

Oliver Charlesworth


It is better in the sense that it computes a running average, i.e. you don't need to have all your numbers in advance. You can calculate that as you go, or as numbers become available.

like image 30
mbatchkarov Avatar answered Oct 17 '22 17:10

mbatchkarov