1
0
Fork 0
mirror of https://github.com/Luzifer/webcheck.git synced 2024-11-08 14:40:01 +00:00
webcheck/vendor/github.com/montanaflynn/stats/median.go
Knut Ahlers 2fbabd6bd4
Vendor dependencies
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2018-07-23 14:54:12 +02:00

25 lines
592 B
Go

package stats
import "math"
// Median gets the median number in a slice of numbers
func Median(input Float64Data) (median float64, err error) {
// Start by sorting a copy of the slice
c := sortedCopy(input)
// No math is needed if there are no numbers
// For even numbers we add the two middle numbers
// and divide by two using the mean function above
// For odd numbers we just use the middle number
l := len(c)
if l == 0 {
return math.NaN(), EmptyInput
} else if l%2 == 0 {
median, _ = Mean(c[l/2-1 : l/2+1])
} else {
median = float64(c[l/2])
}
return median, nil
}