max.go 445 B

123456789101112131415161718192021222324
  1. package stats
  2. import "math"
  3. // Max finds the highest number in a slice
  4. func Max(input Float64Data) (max float64, err error) {
  5. // Return an error if there are no numbers
  6. if input.Len() == 0 {
  7. return math.NaN(), EmptyInput
  8. }
  9. // Get the first value as the starting point
  10. max = input.Get(0)
  11. // Loop and replace higher values
  12. for i := 1; i < input.Len(); i++ {
  13. if input.Get(i) > max {
  14. max = input.Get(i)
  15. }
  16. }
  17. return max, nil
  18. }