Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Tony van der Hoff Newsgroups: comp.lang.python Subject: Re: sum accuracy Date: Fri, 15 Apr 2016 11:39:45 +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> <87d1prw5b7.fsf@bsb.me.uk> <5710C4F1.8050805@vanderhoff.org> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de cz/G6pybmAXRGogE9vPbjASGUa0D197qX3ReGUBP6yQA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.018 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'float': 0.05; 'benjamin': 0.09; 'nameerror:': 0.09; 'rounding': 0.09; 'def': 0.13; '2016': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'numerical': 0.18; 'input': 0.18; '>>>': 0.20; 'fraction': 0.22; 'defined': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'van': 0.26; 'errors.': 0.27; 'helpful.': 0.29; 'convert': 0.29; 'objects': 0.29; "i'm": 0.30; 'error.': 0.31; 'case,': 0.34; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'expect': 0.37; 'received:localdomain': 0.38; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'yes': 0.62; 'charset:windows-1252': 0.62; 'becker': 0.84; 'from:addr:tony': 0.84; 'oscar': 0.84; 'received:192.168.1.7': 0.91 X-Virus-Scanned: Debian amavisd-new at mail.vanderhoff.org User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Icedove/38.7.0 In-Reply-To: <87d1prw5b7.fsf@bsb.me.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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <5710C4F1.8050805@vanderhoff.org> X-Mailman-Original-References: <570E78F9.8050409@chamonix.reportlab.co.uk> <1460567110.3933817.577783969.165F6097@webmail.messagingengine.com> <5710B365.5040903@chamonix.reportlab.co.uk> <87d1prw5b7.fsf@bsb.me.uk> Xref: csiph.com comp.lang.python:107041 On 15/04/16 11:10, Ben Bacarisse wrote: > Oscar Benjamin writes: > >> On 15 April 2016 at 10:24, Robin Becker wrote: > >>> 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. > > Just a word of warning for people new to numerical work: there's no > rounding error, but unless you start with Fraction objects you still > have input or conversion errors. The uninitiated might expect > > exact_sum([0.3, 0.7]) > > to be 1. > So I'm uninitiated: NameError: name 'exact_sum' is not defined I appreciate the word of warning, but, in my case, it's not helpful. -- Tony van der Hoff | mailto:tony@vanderhoff.org Buckinghamshire, England |