gitee.com/quant1x/num@v0.3.2/mean.go (about) 1 package num 2 3 import ( 4 "gitee.com/quant1x/num/x32" 5 "gitee.com/quant1x/num/x64" 6 ) 7 8 // Mean 求均值 9 func Mean[T Number](x []T) T { 10 return UnaryOperations1[T](x, x32.Mean, x64.Mean, __go_mean[T]) 11 } 12 13 func __go_mean[T Number](x []T) T { 14 return __go_sum(x) / T(len(x)) 15 } 16 17 func Mean2[T BaseType](x []T) T { 18 var d any 19 switch vs := any(x).(type) { 20 case []float32: 21 d = Mean(vs) 22 case []float64: 23 d = Mean(vs) 24 case []int: 25 d = Mean(vs) 26 case []int8: 27 d = Mean(vs) 28 case []int16: 29 d = Mean(vs) 30 case []int32: 31 d = Mean(vs) 32 case []int64: 33 d = Mean(vs) 34 case []uint: 35 d = Mean(vs) 36 case []uint8: 37 d = Mean(vs) 38 case []uint16: 39 d = Mean(vs) 40 case []uint32: 41 d = Mean(vs) 42 case []uint64: 43 d = Mean(vs) 44 case []uintptr: 45 d = Mean(vs) 46 //case []string: 47 // d = __go_max(vs) 48 default: 49 // 其它类型原样返回 50 panic(TypeError(any(x))) 51 } 52 53 return d.(T) 54 }