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

@@ -52,6 +52,8 @@ import (
- [Sum](#Sum)
- [Abs](#Abs)
- [Div](#Div)
- [Variance](#Variance)
- [StdDev](#StdDev)
<div STYLE="page-break-after: always;"></div>
@@ -64,7 +66,7 @@ import (
<b>函数签名:</b>
```go
func Average[T constraints.Integer | constraints.Float](numbers ...T) T
func Average[T constraints.Integer | constraints.Float](numbers ...T) float64
```
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/HFd70x4DrMj)</span></b>
@@ -87,7 +89,7 @@ func main() {
fmt.Println(result2)
// Output:
// 1
// 1.5
// 1.3
}
```
@@ -1166,7 +1168,7 @@ func main() {
### <span id="Variance">Variance</span>
<p>计算方差</p>
<p>计算方差</p>
<b>函数签名:</b>
@@ -1195,4 +1197,37 @@ func main() {
// 2
// 2.42
}
```
### <span id="StdDev">StdDev</span>
<p>计算标准差。</p>
<b>函数签名:</b>
```go
func StdDev[T constraints.Float | constraints.Integer](numbers []T) float64
```
<b>示例:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/mathutil"
)
func main() {
result1 := mathutil.TruncRound(mathutil.StdDev([]int{1, 2, 3, 4, 5}), 2)
result2 := mathutil.TruncRound(mathutil.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

@@ -53,6 +53,7 @@ import (
- [Abs](#Abs)
- [Div](#Div)
- [Variance](#Variance)
- [StdDev](#StdDev)
<div STYLE="page-break-after: always;"></div>
@@ -65,7 +66,7 @@ import (
<b>Signature:</b>
```go
func Average[T constraints.Integer | constraints.Float](numbers ...T) T
func Average[T constraints.Integer | constraints.Float](numbers ...T) float64
```
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/Vv7LBwER-pz)</span></b>
@@ -88,7 +89,7 @@ func main() {
fmt.Println(result2)
// Output:
// 1
// 1.5
// 1.3
}
```
@@ -1186,13 +1187,46 @@ import (
func main() {
result1 := mathutil.Variance([]int{1, 2, 3, 4, 5})
result2 := mathutil.Variance([]float64{1.1, 2.2, 3.3, 4.4, 5.5})
result2 := mathutil.Variance([]float64{1.1, 2.2, 3.3, 4.4, 5.5})
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result1)
fmt.Println(result2)
// Output:
// 2
// 2.42
// Output:
// 2
// 2.42
}
```
### <span id="StdDev">StdDev</span>
<p>Returns the standard deviation of numbers.</p>
<b>Signature:</b>
```go
func StdDev[T constraints.Float | constraints.Integer](numbers []T) float64
```
<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/mathutil"
)
func main() {
result1 := mathutil.TruncRound(mathutil.StdDev([]int{1, 2, 3, 4, 5}), 2)
result2 := mathutil.TruncRound(mathutil.StdDev([]float64{1.1, 2.2, 3.3, 4.4, 5.5}), 2)
fmt.Println(result1)
fmt.Println(result2)
// Output:
// 1.41
// 1.55
}
```