Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Robin Becker Newsgroups: comp.lang.python Subject: Re: sum accuracy Date: Fri, 15 Apr 2016 10:24:53 +0100 Lines: 34 Message-ID: References: <570E78F9.8050409@chamonix.reportlab.co.uk> <1460567110.3933817.577783969.165F6097@webmail.messagingengine.com> <5710B365.5040903@chamonix.reportlab.co.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de yQdes+eKPAsw5hi3iKE3gAMzMrBCeh0M55Kk6DGy94IA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'operator': 0.03; '(except': 0.05; 'seemed': 0.07; 'compute': 0.09; 'grid': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'assumptions': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'semantically': 0.16; 'subtraction': 0.16; 'wrote:': 0.16; "wouldn't": 0.16; 'algorithm': 0.20; 'pos': 0.22; 'header:In- Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'example': 0.26; 'header:X-Complaints-To:1': 0.26; 'points': 0.27; 'error': 0.27; 'types.': 0.29; 'url:activestate': 0.29; 'array': 0.29; "can't": 0.32; 'point': 0.33; 'problem': 0.33; 'url:code': 0.34; 'that,': 0.34; 'could': 0.35; 'problem.': 0.35; 'but': 0.36; 'instead': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'received:org': 0.37; 'thought': 0.37; 'no,': 0.38; 'end': 0.39; 'well.': 0.40; 'to:addr:python.org': 0.40; 'real': 0.62; 'yes': 0.62; 'more': 0.63; 'reverse': 0.66; 'sum': 0.69; 'received:109': 0.75; '..........': 0.84; 'algorithm,': 0.84; 'becker': 0.84; 'strings),': 0.84; 'url:recipes': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: 109.174.168.73 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.2 In-Reply-To: <1460567110.3933817.577783969.165F6097@webmail.messagingengine.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <5710B365.5040903@chamonix.reportlab.co.uk> X-Mailman-Original-References: <570E78F9.8050409@chamonix.reportlab.co.uk> <1460567110.3933817.577783969.165F6097@webmail.messagingengine.com> Xref: csiph.com comp.lang.python:107036 On 13/04/2016 18:05, Random832 wrote: ......... > > No, it doesn't. Sum works on any type that can be added (except > strings), it can't make any assumptions about the characteristics of > floating point types. For non-numeric types, the addition operator may > not be semantically commutative or associative. > I thought as much. My problem was that the sum of an array of small floats was being used to compute a grid of points by subtraction like this height = sum(H) pos = [height] for h in H: height -= h pos.append(height) the value of height[0] came out negative which was a problem. I could reduce the error by using Kahan summation instead of sum, but that required Kahan style subtraction as well. In the end it just seemed better to reverse the loop and compute pos by addition. > Look at > http://code.activestate.com/recipes/393090-binary-floating-point-summation-accurate-to-full-p/ > for an example of a more accurate algorithm, but note that, for example, > this algorithm wouldn't work on complex numbers (you'd have to sum the > real and imaginary components separately) > yes indeed summation is hard :( -- Robin Becker