Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Oscar Benjamin Newsgroups: comp.lang.python Subject: Re: sum accuracy Date: Fri, 15 Apr 2016 14:49:19 +0100 Lines: 43 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> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de BYWfJC8TEN+Bp+kRnvkXtA7xAxNaWXSLK35gNt4XGA6w== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'float': 0.05; 'cc:addr :python-list': 0.09; 'benjamin': 0.09; 'rounding': 0.09; 'valueerror': 0.09; 'def': 0.13; '2016': 0.16; 'above)': 0.16; 'add.': 0.16; 'cc:name:python list': 0.16; 'constructor.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'numerical': 0.18; 'input': 0.18; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'fraction': 0.22; 'errors': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'message- id:@mail.gmail.com': 0.27; 'errors.': 0.27; 'correct': 0.28; '(it': 0.29; 'convert': 0.29; 'objects': 0.29; 'raise': 0.29; 'error.': 0.31; 'source': 0.33; 'combination': 0.33; 'impression': 0.33; 'received:google.com': 0.35; 'but': 0.36; 'skip:i 20': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'subject:: ': 0.37; 'expect': 0.37; 'wanted': 0.37; 'received:209': 0.38; 'still': 0.40; 'your': 0.60; 'yes': 0.62; 'sum': 0.69; 'becker': 0.84; 'oscar': 0.84; 'thing).': 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=mKjgRmaS4k5HgHeb4le/rxweQwwSpT4k7fBFI1IOfSA=; b=IjcQMDkW6e3RD5xFfUmsR7lYX1UX8bJYbYIoprUi8LM1hf2e6wfFOLBEbA75Wp+b/E nyo5TrTIYgpObOl9v5bVts405HqeqZgbsvfK5LBJuQl2OiIeojTyv4VOy/6UtvkH2xso 7RrlssfXOXkLET/ssBYJbfQ4GVjqqJenORak0b3i+ZuZcZNUBwACgTqmPYAU2DzA9VeC ivBzfEQamkRIiyfhgqlJ/yQGB0/PNFC9fmttP6KwetdOzLm6si6shM2m/6r61tN8Jagl P6rHDl8bVPRXyACJ00awAbBQVi/FIOv5QyHWDvx7bg3GXsTELHbGisLIMvIEU4aSFvDX szcg== 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=mKjgRmaS4k5HgHeb4le/rxweQwwSpT4k7fBFI1IOfSA=; b=CY/7exOdWfxEBXbyd1GiUA1CppNNp7kRgKazQ57+jHTIkgfNkUUQDc+kYGkKUWtZlv DuLuHOsIVMsdiOJ9u/PjfZPhPyE8DjWmaZrat1irSPfoYyj2QR7epat8+PqijI08u5bZ f0Zwspe0p4zkiYAWnVg5NLBmp+O3qqc4wTCYd4+7pM+z2v+cHO1FvfJB+Ptra0eABDW5 6gUJjhTGpPCfhhd/O7OxFJkfllb78MvG1r66UB4PrqoY8PQ3Lhc6iztZ+ar6lFIQCaA2 5pZEF46Z0ol2YT0tEyk1So8yz8b/F/ubP71fuOciIx6uiyvkKNatcskTqqz7nX9q3Pre FAgQ== X-Gm-Message-State: AOPr4FU56FfRgii9J1XwKe6DGZ2NqGwGM3ArtFTNAuDd+aNhWvsb7FYxxxs5zAK8KQwZU+U70HVEdTtlPWKq4w== X-Received: by 10.112.198.6 with SMTP id iy6mr749727lbc.43.1460728179169; Fri, 15 Apr 2016 06:49:39 -0700 (PDT) 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: 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:107056 On 15 April 2016 at 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. There are no conversion errors in the Fraction constructor. This will exactly sum any combination of int/float/Fraction/Decimal without errors. (It will raise ValueError on nan/inf but I consider that a good thing). > The uninitiated might expect > > exact_sum([0.3, 0.7]) > > to be 1. That's true but I wanted to correct the impression (from above) that *converting* to Fraction is a source of rounding error. It is your responsibility to give exact_sum the exact numbers that you want to add. You can even use strings if you want to write numbers in decimal: exact_sum(['0.3', '0.7']) -- Oscar