Kahan summation: Difference between revisions

Line 1,077:
(a + b) + c = 0.99999994
Kahan sum = 1.0</pre>
 
=={{header|Julia}}==
Julia can use its BigFloat data type to avoid floating point epsilon errors, as shown below.
<lang julia>epsilon() = begin eps = 1.0; while 1.0 + eps != 1.0 eps = eps / 2.0 end; eps end
 
function kahansum(arr)
tot = temp = 0.0
for x in arr
y = x - temp
t = tot + y
temp = (t - tot) - y
tot = t
end
return tot
end
 
const a = 1.0
const ep = epsilon()
const b = -ep
const v = [a, ep, b]
 
println("Epsilon is $ep")
println("(a + ep) + b = ", (a + ep) + b)
println("Kahan sum is ", kahansum(v))
println("BigFloat sum is ", (BigFloat(a) + ep) + b)
</lang>{{out}}
<pre>
Epsilon is 1.1102230246251565e-16
(a + ep) + b = 0.9999999999999999
Kahan sum is 1.0
BigFloat sum is 1.0
</pre>
 
 
=={{header|Kotlin}}==
Line 1,112 ⟶ 1,145:
(a + b) + c = 0.99999994
Kahan sum = 1.0</pre>
 
=={{header|Lua}}==
{{trans|C}}
4,105

edits