LGTM
On 11/02/2015 04:59 PM, 'Klaus Aehlig' via ganeti-devel wrote:
For floating point numbers of about equal value, this is a numerically
more stable way of summing them up.
Signed-off-by: Klaus Aehlig <[email protected]>
---
src/Ganeti/Utils.hs | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/src/Ganeti/Utils.hs b/src/Ganeti/Utils.hs
index 46cfc67..8db0c71 100644
--- a/src/Ganeti/Utils.hs
+++ b/src/Ganeti/Utils.hs
@@ -45,6 +45,7 @@ module Ganeti.Utils
, commaJoin
, ensureQuoted
, divideList
+ , balancedSum
, tryRead
, readMaybe
, formatTable
@@ -218,6 +219,15 @@ divideList (a:b:xs) = let (ls, rs) = divideList xs in
(a:ls, b:rs)
-- * Mathematical functions
+-- | Compute the sum of a list of numbers, all about the same value,
+-- and do so in a balanced way to avoid adding numbers of too different
+-- values (and thus too bad inaccuracies).
+balancedSum :: Num a => [a] -> a
+balancedSum [] = 0
+balancedSum [x] = x
+balancedSum xs = let (ls, rs) = divideList xs
+ in balancedSum ls + balancedSum rs
+
-- Simple and slow statistical functions, please replace with better
-- versions