10BC0 Merge pull request #330 from zoulux/main · duke-git/lancet@15a0dad · GitHub
[go: up one dir, main page]

Skip to content

Commit 15a0dad

Browse files
authored
Merge pull request #330 from zoulux/main
fix: return 0 when Average is called with empty slice
2 parents 4c64a16 + 93c777a commit 15a0dad

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

mathutil/mathutil.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ func Sum[T constraints.Integer | constraints.Float](numbers ...T) T {
238238
// Average return average value of numbers.
239239
// Play: https://go.dev/play/p/Vv7LBwER-pz
240240
func Average[T constraints.Integer | constraints.Float](numbers ...T) float64 {
241+
if len(numbers) == 0 {
242+
return 0
243+
}
241244
var sum float64
242245
for _, num := range numbers {
243246
sum += float64(num)

0 commit comments

Comments
 (0)
0