math.extras: adding the Kahan summation.
							parent
							
								
									fbc43abfdf
								
							
						
					
					
						commit
						b9df0a7582
					
				| 
						 | 
				
			
			@ -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." } ;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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 ;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue