Kahan summation: Difference between revisions

m
→‎{{header|REXX}}: added/changed whitespace.
(Implementation in Crystal)
m (→‎{{header|REXX}}: added/changed whitespace.)
Line 1,919:
call show 10000.0, 3.14169, 2.71828 /*invoke SHOW to sum & display numbers.*/
numeric digits 30 /*from now on, use 30 decimal digits.*/
epsilon= 1
do while 1+epsilon \= 1 /*keep looping 'til we can't add unity.*/
epsilon= epsilon / 2 /*halve the value of epsilon variable.*/
end /*while*/
say /*display a blank line before the fence*/
Line 1,928:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
kahan: procedure; $= 0; c= 0; do j=1 for arg() /*perform for each argument. */
y=arg(j) - c y= arg(j) - c /*subtract C from argument.*/
t=$ + y t= $ + y /*use a temporary sum (T). */
c=t - $ - y c= t - $ - y /*compute the value of C. */
$=t $= t /*redefine the sum ($). */
end /*j*/
return $
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: procedure; parse arg a,b,c /*obtain the arguments. */
say 'decimal digits =' digits() /*show number of decimal digs*/
say ' a = ' left(''"", a>=0) a a /*display A justified. */
say ' b = ' left(''"", b>=0) b b /* " B " */
say ' c = ' left(''"", c>=0) c c /* " C " */
say 'simple summation of a,b,c = ' a+ b+c c /*compute simple summation. */
say 'Kahan summation of a,b,c = ' kahan(a, b, c) /*sum via Kahan summation. */
return</lang>
{{out|output|text=&nbsp; using PC/REXX and also Personal REXX:}}
Line 2,017:
call show 10000.0, 3.14169, 2.71828 /*invoke SHOW to sum & display numbers.*/
numeric digits 30 /*from now on, use 30 decimal digits.*/
epsilon= 1
do while 1+epsilon \= 1 /*keep looping 'til we can't add unity.*/
epsilon= epsilon / 2 /*halve the value of epsilon variable.*/
end /*while*/
say /*display a blank line before the fence*/
Line 2,028:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
kahan: procedure; $= 0; c= 0; do j=1 for arg() /*perform for each argument. */
y=arg(j) - c y= arg(j) - c /*subtract C from argument.*/
t=$ + y t= $ + y /*use a temporary sum (T). */
c=t - $ - y c= t - $ - y /*compute the value of C. */
$=t $= t /*redefine the sum ($). */
end /*j*/
return $
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: procedure; parse arg a,b,c /*obtain the arguments. */
say 'decimal digits =' digits() /*show number of decimal digs*/
say ' a = ' left(''"", a>=0) a a /*display A justified. */
say ' b = ' left(''"", b>=0) b b /* " B " */
say ' c = ' left(''"", c>=0) c c /* " C " */
say 'simple summation of a,b,c = ' a+ b+c c /*compute simple summation. */
say 'Kahan summation of a,b,c = ' kahan(a, b, c) /*sum via Kahan summation. */
return</lang>
{{out|output|text=&nbsp; using Regina version 3.4 and also for later versions:}}
Line 2,060:
simple summation of a,b,c = 1.0000000000000000000000000000001
Kahan summation of a,b,c = 1.0000000000000000000000000000000</pre>
</pre>
 
'''output''' for ooRexx:
<pre>decimal digits = 6
a = 10000.0
b = 3.14169
c = 2.71828
simple summation of a,b,c = 10005.8
Kahan summation of a,b,c = 10005.9
 
ªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªª
 
decimal digits = 32
a = 1.0
b = 0.00000000000000000000000000000315544362088404722164691426133
c = -0.00000000000000000000000000000315544362088404722164691426133
simple summation of a,b,c = 1.0000000000000000000000000000001
Kahan summation of a,b,c = 1.0000000000000000000000000000000
 
ªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªªª</pre>
 
=={{header|Ruby}}==