Add generic math abs
This commit is contained in:
parent
4612ee993e
commit
e8aa16622b
1 changed files with 13 additions and 0 deletions
13
math/math.go
Normal file
13
math/math.go
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
package mathutils
|
||||||
|
|
||||||
|
type SignedNumber interface {
|
||||||
|
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~float32 | ~float64
|
||||||
|
}
|
||||||
|
|
||||||
|
func Abs[T SignedNumber](num T) T {
|
||||||
|
if num > 0 {
|
||||||
|
return num
|
||||||
|
} else {
|
||||||
|
return num * -1
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue