math.extras: adding the Kahan summation.

db4
John Benediktsson 2013-10-13 08:27:58 -07:00
parent fbc43abfdf
commit b9df0a7582
2 changed files with 9 additions and 0 deletions

View File

@ -86,3 +86,7 @@ HELP: round-to-decimal
{ $example "USING: math.extras prettyprint ;" "1.23456 2 round-to-decimal ." "1.23" }
{ $example "USING: math.extras prettyprint ;" "12345.6789 -3 round-to-decimal ." "12000.0" }
} ;
HELP: kahan-sum
{ $values { "seq" sequence } { "n" float } }
{ $description "Calculates the summation of the sequence using the Kahan summation algorithm." } ;

View File

@ -289,3 +289,8 @@ M: real round-away-from-zero
[ swapd @ [ 1 + ] [ max 0 ] if ] keep swap
] reduce nip max
] if ; inline
: kahan-sum ( seq -- n )
[ 0.0 0.0 ] dip [
rot - 2dup + [ -rot [ - ] bi@ ] keep
] each nip ;