Skip to content

Commit

Permalink
assert not zero on div
Browse files Browse the repository at this point in the history
  • Loading branch information
jairad26 committed Oct 8, 2024
1 parent 7c2c6aa commit 9bb277f
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions sdk/go/pkg/vectors/vectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func MultiplyNumberInPlace[T constraints.Integer | constraints.Float](a []T, b T

// DivideNumber divides each element of a vector by a number and returns the result.
func DivideNumber[T constraints.Integer | constraints.Float](a []T, b T) []T {
assertNotZero(b)
result := make([]T, len(a))
for i := range a {
result[i] = a[i] / b
Expand All @@ -110,6 +111,7 @@ func DivideNumber[T constraints.Integer | constraints.Float](a []T, b T) []T {

// DivideNumberInPlace divides each element of a vector by a number and stores the result in the vector.
func DivideNumberInPlace[T constraints.Integer | constraints.Float](a []T, b T) {
assertNotZero(b)
for i := range a {
a[i] /= b
}
Expand Down Expand Up @@ -235,3 +237,9 @@ func assertNonEmpty[T constraints.Integer | constraints.Float](a []T) {
panic("vector must be non-empty")
}
}

func assertNotZero[T constraints.Integer | constraints.Float](a T) {
if a == 0 {
panic("value must be non-zero")
}
}

0 comments on commit 9bb277f

Please sign in to comment.