mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
Update mathutil.go
fix: return 0 when Average is called with empty slice
This commit is contained in:
@@ -238,6 +238,9 @@ func Sum[T constraints.Integer | constraints.Float](numbers ...T) T {
|
|||||||
// Average return average value of numbers.
|
// Average return average value of numbers.
|
||||||
// Play: https://go.dev/play/p/Vv7LBwER-pz
|
// Play: https://go.dev/play/p/Vv7LBwER-pz
|
||||||
func Average[T constraints.Integer | constraints.Float](numbers ...T) float64 {
|
func Average[T constraints.Integer | constraints.Float](numbers ...T) float64 {
|
||||||
|
if len(numbers) == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
var sum float64
|
var sum float64
|
||||||
for _, num := range numbers {
|
for _, num := range numbers {
|
||||||
sum += float64(num)
|
sum += float64(num)
|
||||||
|
|||||||
Reference in New Issue
Block a user