Cumulative standard deviation: Difference between revisions

Content deleted Content added
m →‎{{header|REXX}}: changed/add comments and whitespace, changed section header comment.
Line 2,719: Line 2,719:


=={{header|REXX}}==
=={{header|REXX}}==
Uses running sums.
This REXX version uses running sums.
<lang rexx>/*REXX pgm finds & displays the standard deviation of a given set of #s.*/
<lang rexx>/*REXX pgm finds & displays the standard deviation of a given set of numbers.*/
parse arg # /*any optional args on the C.L. ?*/
parse arg # /*any optional arguments on the C.L. ? */
if #='' then #=2 4 4 4 5 5 7 9 /*None given? Then use default.*/
if #='' then # = 2 4 4 4 5 5 7 9 /*None specified? Then use the default*/
w=words(#); s=0; ss=0 /*define: #items; a couple sums.*/
w=words(#); L=length(w); $=0; $$=0 /*# items; item width; couple of sums*/
/* [↓] process each number in the list*/

do j=1 for w; _=word(#,j); s=s+_; ss=ss+_*_
do j=1 for w; _=word(#,j); $=$+_; $$=$$+_**2
say ' item' right(j,length(w))":" right(_,4),
say ' item' right(j,L)":" right(_,4) ' average=' left($/j,12),
' average=' left(s/j,12),
' standard deviation=' left(sqrt( $$/j - ($/j)**2 ), 15)
' standard deviation=' left(sqrt( ss/j - (s/j)**2 ),15)
end /*j*/ /* [↑] prettify output with whitespace*/
end /*j*/
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
exit /*stick a fork in it, we're done.*/
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); numeric form
/*──────────────────────────────────SQRT subroutine─────────────────────*/
numeric digits 11; parse value 11 format(x,2,1,,0) 'E0' with m. g 'E' _ .
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); numeric digits 11
g=g*.5'E'_%2; p=d+d%4+2; do j=0 while p>9; m.j=p; p=p%2+1; end
numeric form; m.=11; p=d+d%4+2; parse value format(x,2,1,,0) 'E0' with g 'E' _ .
g=g*.5'E'_%2; do j=0 while p>9; m.j=p; p=p%2+1; end
do k=j+5 to 0 by -1; if m.k>11 then numeric digits m.k; g=.5*(g+x/g); end
numeric digits d; return g/1</lang>
do k=j+5 to 0 by -1; if m.k>11 then numeric digits m.k; g=.5*(g+x/g); end
'''output''' &nbsp; when using the default input of: &nbsp; <tt> 2 &nbsp; 4 &nbsp; 4 &nbsp; 4 &nbsp; 5 &nbsp; 5 &nbsp; 7 &nbsp; 9 </tt>
numeric digits d; return g/1</lang>
{{out}} using the default input
<pre>
<pre>
item 1: 2 average= 2 standard deviation= 0
item 1: 2 average= 2 standard deviation= 0