32 lines
520 B
Go
32 lines
520 B
Go
|
package utils
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"gitee.ltd/lxh/logger"
|
||
|
"math"
|
||
|
"strconv"
|
||
|
)
|
||
|
|
||
|
type mathUtil struct{}
|
||
|
|
||
|
func MathUtil() *mathUtil {
|
||
|
return &mathUtil{}
|
||
|
}
|
||
|
|
||
|
func (m mathUtil) Sqrt(datas []float64, avg float64) float64 {
|
||
|
if len(datas) == 0 {
|
||
|
return 0
|
||
|
}
|
||
|
var aaa float64
|
||
|
for _, d := range datas {
|
||
|
co := d - avg
|
||
|
aaa += co * co
|
||
|
}
|
||
|
d, err := strconv.ParseFloat(fmt.Sprintf("%.2f", math.Sqrt(aaa/float64(len(datas)))), 64)
|
||
|
if err != nil {
|
||
|
logger.Say.Errorf("生成标准差失败:%v", err)
|
||
|
return 0
|
||
|
}
|
||
|
return d
|
||
|
}
|