Kahan summation: Difference between revisions

Added Wren
m (→‎{{header|Raku}}: Fix-up some Perl6 -> Raku references)
(Added Wren)
Line 2,283:
(a + b) + c = 1
Kahan sum = 1</pre>
 
=={{header|Wren}}==
Wren's only 'native' number type (Num) is double-precision floating point and so the alternative task is performed. Although it appears that there is no difference between the left associative sum and Kahan summation, there is in fact a difference of epsilon but the accuracy of System.print (14 significant figures) is insufficient to indicate this directly.
<lang ecmascript>var kahanSum = Fn.new { |a|
var sum = 0
var c = 0
for (f in a) {
var y = f - c
var t = sum + y
c = (t - sum) - y
sum = t
}
return sum
}
 
var epsilon = Fn.new {
var eps = 1
while (1 + eps != 1) eps = eps/2
return eps
}
 
var a = 1
var b = epsilon.call()
var c = -b
 
var s = (a + b) + c
var k = kahanSum.call([a, b, c])
var d = k - s
System.print("Epsilon = %(b)")
System.print("(a + b) + c = %(s)")
System.print("Kahan sum = %(k)")
System.print("Delta = %(d)")</lang>
 
{{out}}
<pre>
Epsilon = 1.1102230246252e-16
(a + b) + c = 1
Kahan sum = 1
Delta = 1.1102230246252e-16
</pre>
 
=={{header|zkl}}==
9,488

edits