Skip to content

Commit

Permalink
Adding the 'HarmonicMean' function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabri432 committed Jul 6, 2023
1 parent ae04323 commit f045ab0
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions mathem/conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,24 @@ func GeometricMean(values ...float64) float64 {
return math.Pow(product, float64(1.0/len(values)))
}

/*
Calculates the Harmonic mean of the user given values.
Example:
func main() {
mathem.HarmonicMean(1,2,3,4) // [4/(1/1 + 1/2 + 1/3 + 1/4)] == 1.92
}
*/
func HarmonicMean(values ...float64) float64 {
n := len(values)
var div float64
for i := 0; i < n; i++ {
div += 1 / values[i]
}
return float64(n) / div
}

/*
Variance is the ratio between the square deviations of each value from the mean and the number of values.
Expand Down

0 comments on commit f045ab0

Please sign in to comment.