gitee.com/quant1x/num@v0.3.2/sum.go (about) 1 package num 2 3 import ( 4 "gitee.com/quant1x/num/x32" 5 "gitee.com/quant1x/num/x64" 6 ) 7 8 // Sum 求和 9 func Sum[E Number](x []E) E { 10 if len(x) == 0 { 11 return E(0) 12 } 13 var d any 14 switch fs := any(x).(type) { 15 case []float32: 16 d = x32.Sum(fs) 17 case []float64: 18 d = x64.Sum(fs) 19 default: 20 d = __go_sum(fs.([]E)) 21 } 22 23 return d.(E) 24 } 25 26 func __go_sum[E Number](x []E) E { 27 sum := E(0) 28 for i := 0; i < len(x); i++ { 29 sum += x[i] 30 } 31 return sum 32 }