Max finds the highest number in a slice
(input Float64Data)
| 6 | |
| 7 | // Max finds the highest number in a slice |
| 8 | func Max(input Float64Data) (max float64, err error) { |
| 9 | |
| 10 | // Return an error if there are no numbers |
| 11 | if input.Len() == 0 { |
| 12 | return math.NaN(), EmptyInputErr |
| 13 | } |
| 14 | |
| 15 | // Get the first value as the starting point |
| 16 | max = input.Get(0) |
| 17 | |
| 18 | // Loop and replace higher values |
| 19 | for i := 1; i < input.Len(); i++ { |
| 20 | if input.Get(i) > max { |
| 21 | max = input.Get(i) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | return max, nil |
| 26 | } |