1
0
mirror of https://github.com/duke-git/lancet.git synced 2025-12-19 17:02:23 +08:00

Update mathutil.go

fix: return 0 when Average is called with empty slice
This commit is contained in:
jake
2025-09-22 10:04:23 +08:00
committed by GitHub
parent 5ff1c6578f
commit 93c777a418

View File

@@ -238,6 +238,9 @@ func Sum[T constraints.Integer | constraints.Float](numbers ...T) T {
// Average return average value of numbers.
// Play: https://go.dev/play/p/Vv7LBwER-pz
func Average[T constraints.Integer | constraints.Float](numbers ...T) float64 {
if len(numbers) == 0 {
return 0
}
var sum float64
for _, num := range numbers {
sum += float64(num)