Re: sum accuracy

2016-04-15 Thread Matt Wheeler
So we could build on this On 15 April 2016 at 11:10, Ben Bacarisse 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

Re: sum accuracy

2016-04-15 Thread Ben Bacarisse
Oscar Benjamin writes: > 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 :(

Re: sum accuracy

2016-04-15 Thread Oscar Benjamin
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

Re: sum accuracy

2016-04-15 Thread Jussi Piitulainen
Tony van der Hoff writes: > 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

Re: sum accuracy

2016-04-15 Thread Tony van der Hoff
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

Re: sum accuracy

2016-04-15 Thread Ben Bacarisse
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

Re: sum accuracy

2016-04-15 Thread Oscar Benjamin
On 15 April 2016 at 10:24, Robin Becker 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

Re: sum accuracy

2016-04-15 Thread Robin Becker
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

Re: sum accuracy

2016-04-13 Thread Peter Otten
Robin Becker wrote: > Does anyone know if sum does anything special to try and improve accuracy? > My simple tests seem to show it is exactly equivalent to a for loop > summation. If you are worried about accuracy and your values are floating point numbers use math.fsum(): """ fsum(...)

Re: sum accuracy

2016-04-13 Thread Jussi Piitulainen
Robin Becker writes: > Does anyone know if sum does anything special to try and improve > accuracy? My simple tests seem to show it is exactly equivalent to a > for loop summation. You want math.fsum. -- https://mail.python.org/mailman/listinfo/python-list

Re: sum accuracy

2016-04-13 Thread Random832
On Wed, Apr 13, 2016, at 12:51, Robin Becker wrote: > Does anyone know if sum does anything special to try and improve > accuracy? My > simple tests seem to show it is exactly equivalent to a for loop > summation. No, it doesn't. Sum works on any type that can be added (except strings), it can't

sum accuracy

2016-04-13 Thread Robin Becker
Does anyone know if sum does anything special to try and improve accuracy? My simple tests seem to show it is exactly equivalent to a for loop summation. -- Robin Becker -- https://mail.python.org/mailman/listinfo/python-list