Kahan summation: Difference between revisions

no edit summary
(Ada version)
No edit summary
Line 2,547:
(a + b) + c = 1
Kahan sum = 1</pre>
 
=={{header|Vlang}}==
{{trans|go}}
Vlang has no floating point decimal type. Its floating point types are f64 and f32, implementations of IEEE 754 binary64 and binary32 formats. Alternative task:
<lang vlang>fn kahan(s ...f64) f64 {
mut tot, mut c := 0.0, 0.0
for x in s {
y := x - c
t := tot + y
c = (t - tot) - y
tot = t
}
return tot
}
fn epsilon() f64 {
mut e := 1.0
for 1+e != 1 {
e /= 2
}
return e
}
fn main() {
a := 1.0
b := epsilon()
c := -b
println("Left associative: ${a+b+c}")
println("Kahan summation: ${kahan(a, b, c)}")
println("Epsilon: $b")
}</lang>
{{out}}
<pre>Left associative: 0.9999999999999999
Kahan summation: 1
Epsilon: 1.1102230246251565e-16
</pre>
With float defined as float32,
<pre>
Left associative: 0.99999994
Kahan summation: 1
Epsilon: 5.9604645e-08</pre>
 
=={{header|Wren}}==
338

edits