Skip to content

Instantly share code, notes, and snippets.

@jdpaton
Created May 12, 2015 06:26
Show Gist options
  • Save jdpaton/dfcec6d7caffe68d1994 to your computer and use it in GitHub Desktop.
Save jdpaton/dfcec6d7caffe68d1994 to your computer and use it in GitHub Desktop.
[Go] Population Standard Deviation
package main
import (
"flag"
"fmt"
"math"
"strconv"
)
func main() {
flag.Parse()
input := []int{}
for _, i := range flag.Args() {
i, _ := strconv.Atoi(i)
input = append(input, i)
}
in_mean := mean(input)
fmt.Println(fmt.Sprintf("StdDev: %.4f", stddev(in_mean, input)))
}
func mean(input []int) float64 {
total := 0.0
for _, i := range input {
total = total + float64(i)
}
return (total / float64(len(input)))
}
func stddev(meanval float64, input []int) float64 {
variance_total := 0.0
for _, i := range input {
j := float64(i)
variance_total = variance_total + ((j - meanval) * (j - meanval))
}
return math.Sqrt(float64(variance_total) / float64(len(input)))
}
@jdpaton
Copy link
Author

jdpaton commented May 12, 2015

Cheated reading ints from cmd arguments using the standard flags package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment