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. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list