3 // Copyright 2021 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
13 func Sum[T interface{ int | float64 }](vec []T) T {
15 for _, elt := range vec {
21 func Abs(f float64) float64 {
30 vec2 := []float64{5.8, 9.6}
32 want := vec1[0] + vec1[1]
34 panic(fmt.Sprintf("got %d, want %d", got, want))
38 panic(fmt.Sprintf("got %d, want %d", got, want))
41 fwant := vec2[0] + vec2[1]
42 fgot := Sum[float64](vec2)
43 if Abs(fgot-fwant) > 1e-10 {
44 panic(fmt.Sprintf("got %f, want %f", fgot, fwant))
47 if Abs(fgot-fwant) > 1e-10 {
48 panic(fmt.Sprintf("got %f, want %f", fgot, fwant))