Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #107065

Re: sum accuracy

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.python
Subject Re: sum accuracy
Date 2016-04-15 17:54 +0100
Organization A noiseless patient Spider
Message-ID <87vb3iu817.fsf@bsb.me.uk> (permalink)
References (3 earlier) <CAHVvXxSub2Z2j6eedm3zQr7v2aYWMhmGYzXTtx+SPrcAFgXFtw@mail.gmail.com> <mailman.11.1460712984.6324.python-list@python.org> <87d1prw5b7.fsf@bsb.me.uk> <CAHVvXxTe-2ABYmGkAz9nb3xsr-a3t1kDHA=VmqBQ-pYpp1XziA@mail.gmail.com> <mailman.24.1460728181.6324.python-list@python.org>

Show all headers | View raw


Oscar Benjamin <oscar.j.benjamin@gmail.com> writes:

> On 15 April 2016 at 11:10, Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
>> Oscar Benjamin <oscar.j.benjamin@gmail.com> writes:
>>
>>> On 15 April 2016 at 10:24, Robin Becker <robin@reportlab.com> wrote:
>> <snip>
>>>> 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).

Yes, that's a good point.  Starting with Fraction objects is just one
way to know exactly what you are exactly summing.

>> 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.

The bad phrase "input or conversion errors" was intended to refer to the
conversion Python does when you write 0.3 in the source or when you read
your data and convert it to floating-point.  It can certainly be read as
if I was talking about the constructor but, as you say, *that*
conversion is exact.

<snip>
-- 
Ben.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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