On 2007-02-27, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > On Tue, 27 Feb 2007 10:06:29 +0000, Duncan Booth wrote: >> Adding up a long list of values and then dividing by the >> number of values is the classic computer science example of >> how to get an inaccurate answer from a floating point >> calculation. > > I'm not entirely ignorant when it comes to computational > mathematics, but I must admit this is a new one to me. > > If the values vary greatly in magnitude, you probably want to add them > from smallest to biggest; other than that, how else can you calculate the > mean? > > The only alternative I thought of was to divide each value by the count > before summing, but that would be horribly inaccurate. > > Or would it? > >>>> def mean1(*args): > ... return sum(args)/len(args) > ... >>>> def mean2(*args): > ... n = len(args) > ... return sum([x/n for x in args]) > ... >>>> L = range(25, 597) # 572 values >>>> L = [x/3.3 for x in L] >>>> >>>> mean1(*L) > 94.090909090909108 >>>> mean2(*L) > 94.090909090909108 > > The first calculation has 571 additions and one division; the > second calculation has 571 additions and 572 divisions, but > they both give the same result to 15 decimal places.
How about this? >>> L = range(5, 10) >>> L = [pow(2, -x) for x in L] >>> "%40.40f" % mean1(*L) '0.0121093750000000000000000000000000000000' >>> "%40.40f" % mean2(*L) '0.0121093750000000020000000000000000000000' Offhand, I think the first is "righter". Weird! -- Neil Cerutti -- http://mail.python.org/mailman/listinfo/python-list