Skip to content

Commit

Permalink
✨ feat: math - add generic func Abs
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 24, 2024
1 parent 479e259 commit 45cdda2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
11 changes: 11 additions & 0 deletions mathutil/calc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package mathutil

import "github.com/gookit/goutil/comdef"

// Abs get absolute value of given value
func Abs[T comdef.Int](val T) T {
if val >= 0 {
return val
}
return -val
}
13 changes: 13 additions & 0 deletions mathutil/calc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package mathutil_test

import (
"testing"

"github.com/gookit/goutil/mathutil"
"github.com/gookit/goutil/testutil/assert"
)

func TestAbs(t *testing.T) {
assert.Eq(t, 1, mathutil.Abs(1))
assert.Eq(t, 1, mathutil.Abs(-1))
}

0 comments on commit 45cdda2

Please sign in to comment.