1package stats
2
3import "math"
4
5// Median gets the median number in a slice of numbers
6func Median(input Float64Data) (median float64, err error) {
7
8	// Start by sorting a copy of the slice
9	c := sortedCopy(input)
10
11	// No math is needed if there are no numbers
12	// For even numbers we add the two middle numbers
13	// and divide by two using the mean function above
14	// For odd numbers we just use the middle number
15	l := len(c)
16	if l == 0 {
17		return math.NaN(), EmptyInput
18	} else if l%2 == 0 {
19		median, _ = Mean(c[l/2-1 : l/2+1])
20	} else {
21		median = float64(c[l/2])
22	}
23
24	return median, nil
25}
26