1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

feat: add StdDev and fix bug of Average for math package

This commit is contained in:
dudaodong
2024-11-18 17:01:27 +08:00
parent 6b2c91b0f6
commit 8322951475
7 changed files with 218 additions and 88 deletions

View File

@@ -224,14 +224,12 @@ 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) T {
var sum T
n := T(len(numbers))
for _, v := range numbers {
sum += v
func Average[T constraints.Integer | constraints.Float](numbers ...T) float64 {
var sum float64
for _, num := range numbers {
sum += float64(num)
}
return sum / n
return sum / float64(len(numbers))
}
// Range creates a slice of numbers from start with specified count, element step is 1.
@@ -405,11 +403,17 @@ func Variance[T constraints.Float | constraints.Integer](numbers []T) float64 {
}
avg := Average(numbers...)
var sum T
var sum float64
for _, v := range numbers {
sum += (v - avg) * (v - avg)
sum += (float64(v) - avg) * (float64(v) - avg)
}
return float64(sum) / float64(n)
return sum / float64(n)
}
// StdDev returns the standard deviation of numbers.
// Play: todo
func StdDev[T constraints.Float | constraints.Integer](numbers []T) float64 {
return math.Sqrt(Variance(numbers))
}

View File

@@ -178,7 +178,7 @@ func ExampleAverage() {
fmt.Println(result2)
// Output:
// 1
// 1.5
// 1.3
}
@@ -490,3 +490,15 @@ func ExampleVariance() {
// 2
// 2.42
}
func ExampleStdDev() {
result1 := TruncRound(StdDev([]int{1, 2, 3, 4, 5}), 2)
result2 := TruncRound(StdDev([]float64{1.1, 2.2, 3.3, 4.4, 5.5}), 2)
fmt.Println(result1)
fmt.Println(result2)
// Output:
// 1.41
// 1.55
}

View File

@@ -143,11 +143,22 @@ func TestAverage(t *testing.T) {
assert := internal.NewAssert(t, "TestAverage")
assert.Equal(0, Average(0, 0))
assert.Equal(1, Average(1, 1))
tests := []struct {
numbers []int
expected float64
}{
{[]int{0}, 0},
{[]int{1, 1, 1}, 1},
{[]int{1, 2, 3, 4}, 2.5},
{[]int{1, 2, 3, 4, 5}, 3},
}
avg := Average(1.2, 1.4)
assert.Equal(1.3, RoundToFloat(avg, 1))
for _, tt := range tests {
assert.Equal(tt.expected, Average(tt.numbers...))
}
avg := Average(1.1, 1.2, 1.3, 1.4)
assert.Equal(1.25, avg)
}
func TestSum(t *testing.T) {
@@ -425,7 +436,7 @@ func TestVariance(t *testing.T) {
}{
{[]int{0}, 0},
{[]int{1, 1, 1}, 0},
{[]int{1, 2, 3, 4}, 1.5},
{[]int{1, 2, 3, 4}, 1.25},
{[]int{1, 2, 3, 4, 5}, 2.0},
}
@@ -446,3 +457,37 @@ func TestVariance(t *testing.T) {
assert.Equal(tt.expected, TruncRound(Variance(tt.numbers), 2))
}
}
func TestStdDev(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestStdDev")
testIntNumbers := []struct {
numbers []int
expected float64
}{
{[]int{0}, 0},
{[]int{1, 1, 1}, 0},
{[]int{1, 2, 3, 4}, 1.118},
{[]int{1, 2, 3, 4, 5}, 1.414},
}
for _, tt := range testIntNumbers {
assert.Equal(tt.expected, TruncRound(StdDev(tt.numbers), 3))
}
testFloatNumbers := []struct {
numbers []float64
expected float64
}{
{[]float64{0}, 0},
{[]float64{1, 1, 1}, 0},
{[]float64{1.1, 2.2, 3.3, 4.4}, 1.229},
}
for _, tt := range testFloatNumbers {
assert.Equal(tt.expected, TruncRound(StdDev(tt.numbers), 3))
}
}