Please help - get average
clifford.wells at comcast.net
Wed Oct 27 10:21:54 CEST 2004
On Wed, 2004-10-27 at 09:07 +0100, Robin Becker wrote:
> Cliff Wells wrote:
> > total = 0.0
> > count = 0
> > number = None
> > while number not in ('q', 'Q'):
> > number = raw_input("Enter a number (or Q to quit): ")
> > try:
> > number = float(number)
> > except ValueError:
> > continue
> > total += number
> > count += 1
> > print "The average is:", total / count
> ...... interestingly the standard algorithms for means/deviations are
> numerically poor. As an example
> >>> (1.e30+1-1.0e30)/3
> Where we have total loss of information in one of the terms.
> There are better algorithms involving recursion,
> mu[n] = (data[n]+(n-1)*mu[n-1])/n
> The error in the existing estimate, mu[n-1], gets multiplied by the
> number (n-1)/n which is less than one (ie the errors are damped).
> However, in the case of widely differing magnitudes I think you need to
> accumulate the sums specially.
Luckily I'm a programmer not a mathematician, so most days I can happily
continue averaging things the way I did in grade school. And if
something goes wrong, well I can always count on there being only 32
bits in an integer and just catch the error there <wink>.
Cliff Wells <clifford.wells at comcast.net>
More information about the Python-list