Averages/Simple moving average: Difference between revisions

m
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
imported>Arakov
(4 intermediate revisions by 4 users not shown)
Line 1,256:
2 3.8
1 3.0</syntaxhighlight></div>
 
=={{header|EasyLang}}==
<syntaxhighlight>
prefix sma_
global p[] ind[] sum[] smpl[][] .
func new p .
p[] &= p
ind[] &= 0
sum[] &= 0
smpl[][] &= [ ]
return len p[]
.
func get id x .
ind[id] = (ind[id] + 1) mod1 p[id]
ind = ind[id]
if len smpl[id][] < ind
len smpl[id][] ind
else
sum[id] -= smpl[id][ind]
.
sum[id] += x
smpl[id][ind] = x
return sum[id] / len smpl[id][]
.
prefix
#
sma5 = sma_new 5
sma3 = sma_new 3
numfmt 2 4
for v in [ 1 2 3 4 5 5 4 3 2 1 ]
print sma_get sma3 v & " " & sma_get sma5 v
.
</syntaxhighlight>
 
=={{header|EchoLisp}}==
Line 1,291 ⟶ 1,324:
 
=={{header|Elena}}==
ELENA 56.0x :
<syntaxhighlight lang="elena">import system'routines;
import system'collections;
Line 1,314 ⟶ 1,347:
count =>
0 { ^0.0r }
:! {
if (count > thePeriod)
{
theList.removeAt:(0);
count := thePeriod
Line 1,328 ⟶ 1,361:
}
}
 
// --- Program ---
 
public program()
{
var SMA3 := SMA.new:(3);
var SMA5 := SMA.new:(5);
 
for (int i := 1,; i <= 5,; i += 1) {
console.printPaddingRight(30, "sma3 + ", i, " = ", SMA3.append:(i));
console.printLine("sma5 + ", i, " = ", SMA5.append:(i))
};
 
for (int i := 5,; i >= 1,; i -= 1) {
console.printPaddingRight(30, "sma3 + ", i, " = ", SMA3.append:(i));
console.printLine("sma5 + ", i, " = ", SMA5.append:(i))
};
Line 2,537 ⟶ 2,572:
<syntaxhighlight lang="lua">function sma(period)
local t = {}
function sum(a, ...t)
sum = 0
if a then return a+sum(...) else return 0 end
for _, v in ipairs(t) do
sum = sum + v
end
return sum
end
function average(n)
if #t == period then table.remove(t, 1) end
t[#t + 1] = n
return sum(unpack(t)) / #t
end
return average
Line 4,848 ⟶ 4,887:
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var sma = Fn.new { |period|
Line 4,871 ⟶ 4,910:
for (x in [1, 2, 3, 4, 5, 5, 4, 3, 2, 1]) {
Fmt.precision = 3
SystemFmt.print("%(Fmt.f(5,$5f x)) $5f %(Fmt.f(5$5f", x, sma3.call(x))) %(Fmt.f(5, sma5.call(x)))")
}</syntaxhighlight>
 
Anonymous user