Anonymous user
Kahan summation: Difference between revisions
m
→{{header|REXX}}: added/changed comments and whitespace, changed indentations, used one fence, used output templates.
m (→{{header|PARI/GP}}: Removed superfluous right brace from heading) |
m (→{{header|REXX}}: added/changed comments and whitespace, changed indentations, used one fence, used output templates.) |
||
Line 1,356:
<br>of '''30''' (decimal digits) was chosen. The default precision for REXX is '''9''' decimal digits.
===vanilla version===
<lang rexx>/*REXX program demonstrates simple addition versus using Kahan summation algorithm. */
numeric digits 6 /*use
call show 10000.0, 3.14169, 2.71828 /*invoke SHOW to sum
numeric digits 30 /*from now on, use 30 decimal digits.*/
do while 1+epsilon \= 1 /*keep looping 'til we can't add unity.*/
▲epsilon=1.0
epsilon=epsilon / 2
end
say
call show 1.0, epsilon, -epsilon /*invoke SHOW to sum
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
kahan: procedure;
/*──────────────────────────────────────────────────────────────────────────────────────*/
▲return sum
show: procedure; parse arg a,b,c /*obtain the arguments. */
say '
say '
say '
say '
return</lang>▼
▲say; say copies('▒',70); say /*display a fence.*/
▲return</lang>
▲'''output''' using PC/REXX and also Personal REXX:
<pre>
decimal digits = 6
Line 1,406 ⟶ 1,402:
simple summation of a,b,c = 1.00000000000000000000000000000
Kahan summation of a,b,c = 1.00000000000000000000000000000
</pre>
<pre>
decimal digits = 6
Line 1,426 ⟶ 1,420:
simple summation of a,b,c = 0.999999999999999999999999999997
Kahan summation of a,b,c = 1.00000000000000000000000000000
</pre>
<pre>
decimal digits = 6
Line 1,446 ⟶ 1,438:
simple summation of a,b,c = 0.999999999999999999999999999997
Kahan summation of a,b,c = 1.00000000000000000000000000000
</pre>
<pre>
decimal digits = 6
Line 1,466 ⟶ 1,456:
simple summation of a,b,c = 1.00000000000000000000000000000
Kahan summation of a,b,c = 1.00000000000000000000000000000
</pre>
===tweaked version===
The following tweaked REXX version causes Regina (3.4 and later) to work properly.
<lang rexx>/*REXX program demonstrates simple addition versus using Kahan summation algorithm. */
numeric digits 6 /*use
call show 10000.0, 3.14169, 2.71828 /*invoke SHOW to sum
numeric digits 30 /*from now on, use 30 decimal digits.*/
do while 1+epsilon \= 1 /*keep looping 'til we can't add unity.*/
▲epsilon=1.0
epsilon=epsilon / 2
end
say
say copies('▒', 70);
numeric digits digits()+2 /*bump the precision by two dec digits.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
kahan: procedure; $=0;
y=arg(j) - c /*
t=
c=
end /*j*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: procedure; parse arg a,b,c /*obtain the
say 'decimal digits =' digits()
say ' a = ' left('', a>=0) a /*display A justified.
say ' b = ' left('', b>=0)
say ' c = ' left('', c>=0)
say 'simple summation of a,b,c = '
say 'Kahan summation of a,b,c = ' kahan(a,b,c)
return</lang>▼
▲return</lang>
▲'''output''' for Regina 3.4 and later versions:
<pre>
decimal digits = 6
Line 1,521 ⟶ 1,508:
simple summation of a,b,c = 1.0000000000000000000000000000001
Kahan summation of a,b,c = 1.0000000000000000000000000000000
</pre>
'''output''' for ooRexx:
|