RosettaCodeData/Task/Cumulative-standard-deviation/Phix/cumulative-standard-deviation-1.phix
2026-04-30 12:34:36 -04:00

26 lines
501 B
Text

with javascript_semantics
atom sdn = 0, sdsum = 0, sdsumsq = 0
procedure sdadd(atom n)
sdn += 1
sdsum += n
sdsumsq += n*n
end procedure
function sdavg()
return sdsum/sdn
end function
function sddev()
return sqrt(sdsumsq/sdn - power(sdsum/sdn,2))
end function
--test code:
constant testset = {2, 4, 4, 4, 5, 5, 7, 9}
integer ti
for i=1 to length(testset) do
ti = testset[i]
sdadd(ti)
printf(1,"N=%d Item=%d Avg=%5.3f StdDev=%5.3f\n",{i,ti,sdavg(),sddev()})
end for