Skip Menu |
 

This queue is for tickets about the List-MoreUtils CPAN distribution.

Report information
The Basics
Id: 131309
Status: new
Priority: 0/
Queue: List-MoreUtils

People
Owner: Nobody in particular
Requestors: rvtol [...] isolution.nl
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: add sumc() (Kahan)
Date: Tue, 31 Dec 2019 14:05:40 +0100
To: bug-List-MoreUtils [...] rt.cpan.org
From: "Ruud H.G. van Tol" <rvtol [...] isolution.nl>
Download (untitled) / with headers
text/plain 564b
sub sumc { # Compensate for loss of precision. # See https://en.wikipedia.org/wiki/Kahan_summation_algorithm my ($sum, $corr)= (shift, 0); for my $v ( @_ ) { my $sum_next= $sum + $v; $corr+= ( abs($sum) >= abs($v) ) ? (($sum - $sum_next) + $v) : (($v - $sum_next) + $sum); $sum= $sum_next; } return $sum + $corr; } $ perl -Mstrict -MList::Util=sum -wE' sub sumc { ... } my @values= (1, 1e100, 1, -1e100); say for sum(@values), sumc(@values); ' 0 2 -- Greetings, Ruud


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.