From 19ff52a1d1ff2fc6bdecf3aa0c7091da38542d5a Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Fri, 6 Jul 2012 14:13:18 -0400 Subject: [PATCH] use K-B-N summation for float arrays, with thanks to @JeffreySarnoff written carefully it is no more than 20% slower closes #199 --- base/array.jl | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/base/array.jl b/base/array.jl index fe6a7639c67c6..7ee9efbfaab84 100644 --- a/base/array.jl +++ b/base/array.jl @@ -1185,6 +1185,27 @@ function sum{T}(A::StridedArray{T}) v end +function sum{T<:Float}(A::StridedArray{T}) + n = length(A) + if (n == 0) + return zero(T) + end + s = A[1] + c = zero(T) + for i in 2:n + Ai = A[i] + t = s + Ai + if abs(s) >= abs(Ai) + c += ((s-t) + Ai) + else + c += ((Ai-t) + s) + end + s = t + end + + s + c +end + function prod{T}(A::StridedArray{T}) if isempty(A) return one(T)