Averages/Simple moving average: Difference between revisions

→‎{{header|Go}}: improved to avoid accumulated error, added output
(→‎{{header|Mercury}}: cleaned up solution after Mercury people were reasonably appalled :-))
(→‎{{header|Go}}: improved to avoid accumulated error, added output)
Line 961:
<lang go>package main
 
import ("fmt"
"fmt"
)
 
func sma(n int) func(float64) float64 {
Line 974 ⟶ 972:
return sum / float64(len(s))
}
sum += x - s[i]
s[i] = x
i++
if i == n {
i = 0
"fmt" }
sum += x - s[i]0
for _, x = range s {
sum += x
}
return sum * rn
Line 992 ⟶ 993:
}
}</lang>
Output:
<pre>
x sma3 sma5
1.000 1.000 1.000
2.000 1.500 1.500
3.000 2.000 2.000
4.000 3.000 2.500
5.000 4.000 3.000
5.000 4.667 3.800
4.000 4.667 4.200
3.000 4.000 4.200
2.000 3.000 3.800
1.000 2.000 3.000
</pre>
 
=={{header|Groovy}}==
1,707

edits