Averages/Simple moving average: Difference between revisions

m (→‎{{header|zkl}}: always return float)
Line 1,054:
 
=={{header|Elena}}==
ELENA 3.x :
<lang elena>#define system.
<lang elena>#defineimport system'routines.
#defineimport system'collections.
#defineimport extensions.
 
#class SMA
{
#fieldobject thePeriod.
#fieldobject theList.
#constructor new : aPeriod
[
thePeriod := aPeriod.
Line 1,070:
]
#method append : aNumber
[
theList += aNumber.
 
#var aCount := theList length.
^ aCount =>
0 ? [ 0.0r ] ;
! [
if (aCount > thePeriod)?
[
theList remove &index:0.
aCount := thePeriod.
].
#var aSum := theList summarize:(Real new &int:0).
var n := aSum / aCount.
^ aSum / aCount.
Line 1,092:
}
 
#symbol program =
[
#var SMA3 := SMA new:3.
#var SMA5 := SMA new:5.
 
1 to:5 &doEach: (:i)
Line 1,108:
console writeLine:"sma5 + " :i :" = ": (SMA5 += i).
].
console readChar.
].</lang>
{{out}}
Anonymous user