You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
592 B
25 lines
592 B
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 |
|
}
|
|
|