Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #107037
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: sum accuracy |
| Date | Fri, 15 Apr 2016 10:36:02 +0100 |
| Lines | 46 |
| Message-ID | <mailman.11.1460712984.6324.python-list@python.org> (permalink) |
| References | <570E78F9.8050409@chamonix.reportlab.co.uk> <1460567110.3933817.577783969.165F6097@webmail.messagingengine.com> <5710B365.5040903@chamonix.reportlab.co.uk> <CAHVvXxSub2Z2j6eedm3zQr7v2aYWMhmGYzXTtx+SPrcAFgXFtw@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| X-Trace | news.uni-berlin.de wFKLyhCw69nEpQaURGr2jAXjnFsTnS1kxveS+l0rKqSw== |
| Return-Path | <oscar.j.benjamin@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.002 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; '(except': 0.05; 'float': 0.05; 'seemed': 0.07; 'cc:addr:python- list': 0.09; 'compute': 0.09; 'grid': 0.09; 'rounding': 0.09; 'def': 0.13; '.........': 0.16; '2016': 0.16; 'assumptions': 0.16; 'cc:name:python list': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'semantically': 0.16; 'subtraction': 0.16; 'wrote:': 0.16; "wouldn't": 0.16; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'algorithm': 0.20; 'fraction': 0.22; 'pos': 0.22; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'example': 0.26; 'points': 0.27; 'error': 0.27; 'message- id:@mail.gmail.com': 0.27; 'types.': 0.29; 'url:activestate': 0.29; 'array': 0.29; 'convert': 0.29; 'error.': 0.31; "can't": 0.32; 'point': 0.33; 'problem': 0.33; 'url:code': 0.34; 'that,': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'problem.': 0.35; 'but': 0.36; 'instead': 0.36; 'received:209.85': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'thought': 0.37; 'no,': 0.38; 'received:209': 0.38; 'end': 0.39; 'well.': 0.40; 'real': 0.62; 'yes': 0.62; 'more': 0.63; 'reverse': 0.66; 'sum': 0.69; 'algorithm,': 0.84; 'becker': 0.84; 'oscar': 0.84; 'strings),': 0.84; 'url:recipes': 0.84 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=WO0OXU7jhysuRnVPwyBtCkT4V7BFLY7Pp6yta/lkifk=; b=ymleLN0VzxurXoUsJjCeziL06nLXN1L92xkNty/ykXTp8A39XZg1+W2hsZLUeacOmR cJr6Aw/e1lObpty8pNq6wFnZGfgfHnNxFFakJ4/UdM4xvC0oL8YL+Qv+l2LkuTU2Mp/o 2JHMd3zBrl2IZnixNkEgrXnuzRtcGDVo7tySwMeWHijl1a9Bs0KtWULtkBpRttaTUNOp r3UPLrwZPiFbyDrkE1X6Mozm9pQRbkXrD/f5T4JSpCO1qvfynriXjYXA19lL10OYuM+c vQk9kVI3FrZXBSz+woGIp3hYfccXTpzS7MqcHptSQX5Ax7msMtze49JDxze7tMNOhY51 uBsw== |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=WO0OXU7jhysuRnVPwyBtCkT4V7BFLY7Pp6yta/lkifk=; b=WtmHyB5OYBt8JunIUxE85L6RdDpmb5QPBeGooyWFpClTaOVcHtCbDBaqN6cnJXUhTz lIeyZB1Q1nllGaKKZiMuKHgD8sQcF9uoNBjEbL+jTWgnHPHLl732hpC6NVpQR864gQ40 na9MPPP+Z1YmPzt9u6CFcXL3Y6yTf3DPcO4lEYxs2HZAqSf5PK9YRoALnXUCi60jDImj PLOBQSFGTutAPlLpP4Uao8FvwVAmu14KWdIB6JY0B2E64WeePvptcqY6j9IvWqKDwlEN ai+Lw1YQiDt8SDLbqM+1BDZAySwxgPoy1mYGyT2aTCFjfv5Ove4Vnk8xCKwTw3GytgvC 20hw== |
| X-Gm-Message-State | AOPr4FV1n1zyDz1SGv7zB1N7EWWCYFftvd7uTOXvTwe7gPrYn6zFSTNpy+p0r4YTk433BBqm+W81mStE0aKbpw== |
| X-Received | by 10.112.172.34 with SMTP id az2mr8512716lbc.121.1460712981706; Fri, 15 Apr 2016 02:36:21 -0700 (PDT) |
| In-Reply-To | <5710B365.5040903@chamonix.reportlab.co.uk> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.21 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| X-Mailman-Original-Message-ID | <CAHVvXxSub2Z2j6eedm3zQr7v2aYWMhmGYzXTtx+SPrcAFgXFtw@mail.gmail.com> |
| X-Mailman-Original-References | <570E78F9.8050409@chamonix.reportlab.co.uk> <1460567110.3933817.577783969.165F6097@webmail.messagingengine.com> <5710B365.5040903@chamonix.reportlab.co.uk> |
| Xref | csiph.com comp.lang.python:107037 |
Show key headers only | View raw
On 15 April 2016 at 10:24, Robin Becker <robin@reportlab.com> wrote:
> 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 :(
Not with Fraction it isn't:
from fractions import Fraction
def exact_sum(nums):
return sum(map(Fraction, nums))
This will give you the exact result with precisely zero rounding
error. You can convert it to float at the end.
--
Oscar
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Re: sum accuracy Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2016-04-15 10:36 +0100
Re: sum accuracy Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-04-15 11:10 +0100
Re: sum accuracy Tony van der Hoff <tony@vanderhoff.org> - 2016-04-15 11:39 +0100
Re: sum accuracy Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-04-15 14:31 +0300
Re: sum accuracy Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-04-15 08:11 -0400
Re: sum accuracy Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2016-04-15 14:49 +0100
Re: sum accuracy Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-04-15 17:54 +0100
Re: sum accuracy Matt Wheeler <m@funkyhat.org> - 2016-04-15 18:04 +0100
csiph-web