Jump to content

Averages/Simple moving average: Difference between revisions

added zkl
(Updated both D entry)
(added zkl)
Line 2,938:
sum(...) will sum them<br>
sum(...)/(i-z) → r[i] will average them and store the result in the appropriate place in the result list<br>
 
=={{header|zkl}}==
<lang zkl>class [static] SMA{var P=3,ns=L(); // only one instance of this class
fcn init(n){
if(P<ns.append(n.toFloat()).len()) ns.del(0);
returnClass(ns.sum(0.0)/ns.len()); // return average, not Class
}
fcn reset(p){P=p;ns=L();} // to make examples easier
}</lang>
<lang zkl>zkl: T(1,2,3,4,5,5,4,3,2,1).apply(SMA)
L(1,1.5,2,3,4,4.66667,4.66667,4,3,2)
 
zkl: SMA.reset(5)
zkl: T(1,2,3,4,5,5,4,3,2,1).apply(SMA)
L(1,1.5,2,2.5,3,3.8,4.2,4.2,3.8,3)</lang>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.