Created
May 12, 2015 06:26
-
-
Save jdpaton/dfcec6d7caffe68d1994 to your computer and use it in GitHub Desktop.
[Go] Population Standard Deviation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cheated reading ints from cmd arguments using the standard
flags
package.