Kahan summation: Difference between revisions
Content added Content deleted
No edit summary |
Drkameleon (talk | contribs) (added Arturo) |
||
Line 284: | Line 284: | ||
{{out}}<pre>Simple: 1.00000E +4 + 3.14159E +0 + 2.71828E +0 = 1.00058E +4 |
{{out}}<pre>Simple: 1.00000E +4 + 3.14159E +0 + 2.71828E +0 = 1.00058E +4 |
||
Kahan : 1.00000E +4 + 3.14159E +0 + 2.71828E +0 = 1.00059E +4</pre> |
Kahan : 1.00000E +4 + 3.14159E +0 + 2.71828E +0 = 1.00059E +4</pre> |
||
=={{header|Arturo}}== |
|||
<syntaxhighlight lang="arturo">kahanSum: function [inp][ |
|||
c: 0 |
|||
result: 0 |
|||
loop inp 'val [ |
|||
y: val - c |
|||
t: result + y |
|||
c: (t-result) - y |
|||
result: t |
|||
] |
|||
return result |
|||
] |
|||
eps: 1.0 |
|||
while [1 <> 1 + eps]-> |
|||
eps: eps / 2 |
|||
a: 1.0 |
|||
b: eps |
|||
c: neg eps |
|||
print ["Sum of 1.0, epsilon and -epsilon for epsilon:" eps] |
|||
print ["Is result equal to 1.0?"] |
|||
print ["- simple addition:" 1 = (a + b) + c] |
|||
print ["- using Kahan sum:" one? kahanSum @[a b c]]</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Sum of 1.0, epsilon and -epsilon for epsilon: 1.110223024625157e-16 |
|||
Is result equal to 1.0? |
|||
- simple addition: false |
|||
- using Kahan sum: true</pre> |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |