Kahan summation: Difference between revisions

Line 1,294:
{{out}}
<pre>%1 = 1.000000000000000000</pre>
 
=={{header|Objeck}}==
{{trans|C#}}
<lang objeck>class KahanSum {
function : Main(args : String[]) ~ Nil {
a := 1.0f;
b := Epsilon();
c := -b;
 
abc := (a + b) + c;
fa := Float->New[3]; fa[0] := a; fa[1] := b; fa[2] := c;
sum := KahanSum(fa);
 
"Epsilon = {$b}"->PrintLine();
"(a + b) + c = {$abc}"->PrintLine();
"Kahan sum = {$sum}"->PrintLine();
}
 
function : KahanSum(fa : Float[]) ~ Float {
sum := 0.0f;
c := 0.0f;
each(i : fa) {
f := fa[i];
y := f - c;
t := sum + y;
c := (t - sum) - y;
sum := t;
};
 
return sum;
}
 
function : Epsilon() ~ Float {
eps := 1.0f;
while(1.0f + eps <> 1.0f) { eps /= 2.0f; };
return eps;
}
}</lang>
 
{{output}}
<pre>
Epsilon = 1.11022e-16
(a + b) + c = 1
Kahan sum = 1
</pre>
 
=={{header|Perl 6}}==
760

edits