Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #107066
| From | Matt Wheeler <m@funkyhat.org> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: sum accuracy |
| Date | 2016-04-15 18:04 +0100 |
| Message-ID | <mailman.30.1460740251.6324.python-list@python.org> (permalink) |
| References | (2 earlier) <5710B365.5040903@chamonix.reportlab.co.uk> <CAHVvXxSub2Z2j6eedm3zQr7v2aYWMhmGYzXTtx+SPrcAFgXFtw@mail.gmail.com> <mailman.11.1460712984.6324.python-list@python.org> <87d1prw5b7.fsf@bsb.me.uk> <CAG93HwGWHV9+SHmTd=jvXhhUaVfU759Rfet1U=Gqc54nGxb4dg@mail.gmail.com> |
So we could build on this
On 15 April 2016 at 11:10, Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
>> 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.
and make
def not_exact_but_probably_the_sum_you_wanted(nums):
return sum(map(lambda x:Fraction(x).limit_denominator(), nums))
--
Matt Wheeler
http://funkyh.at
Back to comp.lang.python | Previous | Next — Previous 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