summaryrefslogtreecommitdiff
path: root/src/nbc/kahan.sml
diff options
context:
space:
mode:
authorCalvin <calvin@EESI>2013-03-15 15:26:20 -0400
committerCalvin <calvin@EESI>2013-03-15 15:26:20 -0400
commitb632667ce57af89691407bb8668e1512775278ae (patch)
treeb5742cef185f1cc4a7ba6005b5b4116ce7558a01 /src/nbc/kahan.sml
parent39e39f82cc38d71018882b0aaaf58255858a7c56 (diff)
nbc added
Diffstat (limited to 'src/nbc/kahan.sml')
-rw-r--r--src/nbc/kahan.sml31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/nbc/kahan.sml b/src/nbc/kahan.sml
new file mode 100644
index 0000000..70c6b47
--- /dev/null
+++ b/src/nbc/kahan.sml
@@ -0,0 +1,31 @@
+(* Kahan summation *)
+
+signature KAHAN = sig
+ type t
+ val zero: t
+ val add: t * real -> t
+ val sum: t -> real
+ val sequence: real Sequence.t -> real
+ val list: real list -> real
+ val array: real array -> real
+end
+
+structure Kahan :> KAHAN = struct
+ type t = real * real
+ val zero = (0.0, 0.0)
+ fun add ((s, c), x) =
+ let
+ val y = x - c
+ val t = s + y
+ in
+ (t, t - s - y)
+ end
+ fun sum (s, c) = s
+ local
+ fun swappedAdd (a, b) = add (b, a)
+ in
+ fun sequence e = sum (Sequence.fold swappedAdd zero e)
+ fun list l = sum (foldl swappedAdd zero l)
+ fun array a = sum (Array.foldl swappedAdd zero a)
+ end
+end