Averages/Simple moving average: Difference between revisions

Changed to use "deques" instead of "queues" as the latter is no longer supported. Changed output format to that of version 1.4.
No edit summary
(Changed to use "deques" instead of "queues" as the latter is no longer supported. Changed output format to that of version 1.4.)
Line 2,706:
 
=={{header|Nim}}==
<lang nim>import queuesdeques
 
proc simplemovingaverage(period: int): auto =
Line 2,713:
var
summ, n = 0.0
values: = initQueueDeque[float]()
for i in 1..period:
values.addaddLast(0)
 
proc sma(x: float): float =
values.addaddLast(x)
summ += x - values.dequeuepopFirst()
n = min(n+1, float(period))
result = summ / n
Line 2,735:
for i in countdown(5,1): echo sma2(float(i))</lang>
{{out}}
<pre>1.0000000000000000e+000
1.5
1.5000000000000000e+00
2.0
2.0000000000000000e+00
3.0
3.0000000000000000e+00
4.0
4.0000000000000000e+00
4.666666666666667
4.6666666666666670e+00
4.666666666666667
4.6666666666666670e+00
4.0
4.0000000000000000e+00
3.0
3.0000000000000000e+00
2.0
2.0000000000000000e+00
 
1.0
1.0000000000000000e+00
1.5
1.5000000000000000e+00
2.0
2.0000000000000000e+00
2.5
2.5000000000000000e+00
3.0
3.0000000000000000e+00
3.8
3.7999999999999998e+00
4.2
4.2000000000000002e+00
4.2
4.2000000000000002e+00
3.8
3.7999999999999998e+00
3.0000000000000000e+000</pre>
 
=={{header|Objeck}}==
Anonymous user