service/vendor/github.com/montanaflynn/stats/max.go

27 lines
453 B
Go
Raw Normal View History

2023-12-21 22:17:40 +08:00
package stats
2024-10-12 16:28:53 +08:00
import (
"math"
)
2023-12-21 22:17:40 +08:00
// Max finds the highest number in a slice
func Max(input Float64Data) (max float64, err error) {
// Return an error if there are no numbers
if input.Len() == 0 {
2024-10-12 16:28:53 +08:00
return math.NaN(), EmptyInputErr
2023-12-21 22:17:40 +08:00
}
// Get the first value as the starting point
max = input.Get(0)
// Loop and replace higher values
for i := 1; i < input.Len(); i++ {
if input.Get(i) > max {
max = input.Get(i)
}
}
return max, nil
}