github.com/blend/go-sdk@v1.20220411.3/mathutil/mean.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package mathutil
     9  
    10  import "time"
    11  
    12  // Mean gets the average of a slice of numbers
    13  func Mean(input []float64) float64 {
    14  	if len(input) == 0 {
    15  		return 0
    16  	}
    17  
    18  	sum := Sum(input)
    19  	return sum / float64(len(input))
    20  }
    21  
    22  // MeanInts gets the average of a slice of numbers
    23  func MeanInts(input []int) float64 {
    24  	if len(input) == 0 {
    25  		return 0
    26  	}
    27  	sum := SumInts(input)
    28  	return float64(sum) / float64(len(input))
    29  }
    30  
    31  // MeanDurations gets the average of a slice of numbers
    32  func MeanDurations(input []time.Duration) time.Duration {
    33  	if len(input) == 0 {
    34  		return 0
    35  	}
    36  
    37  	sum := SumDurations(input)
    38  	mean := uint64(sum) / uint64(len(input))
    39  	return time.Duration(mean)
    40  }