Averages/Simple moving average: Difference between revisions
Content added Content deleted
m (→{{header|zkl}}: always return float) |
|||
Line 1,054: | Line 1,054: | ||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA 3.x : |
|||
<lang elena>#define system. |
|||
# |
<lang elena>#import system'routines. |
||
# |
#import system'collections. |
||
# |
#import extensions. |
||
class SMA |
|||
{ |
{ |
||
object thePeriod. |
|||
object theList. |
|||
constructor new : aPeriod |
|||
[ |
[ |
||
thePeriod := aPeriod. |
thePeriod := aPeriod. |
||
Line 1,070: | Line 1,070: | ||
] |
] |
||
append : aNumber |
|||
[ |
[ |
||
theList += aNumber. |
theList += aNumber. |
||
var aCount := theList length. |
|||
^ aCount => |
^ aCount => |
||
0 |
0 [ 0.0r ]; |
||
! [ |
! [ |
||
(aCount > thePeriod) |
if (aCount > thePeriod) |
||
[ |
[ |
||
theList remove &index:0. |
theList remove &index:0. |
||
⚫ | |||
aCount := thePeriod. |
aCount := thePeriod. |
||
]. |
]. |
||
var aSum := theList summarize:(Real new &int:0). |
|||
⚫ | |||
^ aSum / aCount. |
^ aSum / aCount. |
||
Line 1,092: | Line 1,092: | ||
} |
} |
||
program = |
|||
[ |
[ |
||
var SMA3 := SMA new:3. |
|||
var SMA5 := SMA new:5. |
|||
1 to:5 &doEach: (:i) |
1 to:5 &doEach: (:i) |
||
Line 1,108: | Line 1,108: | ||
console writeLine:"sma5 + " :i :" = ": (SMA5 += i). |
console writeLine:"sma5 + " :i :" = ": (SMA5 += i). |
||
]. |
]. |
||
console readChar. |
|||
].</lang> |
].</lang> |
||
{{out}} |
{{out}} |