2024-06-18 23:38:33 +02:00
|
|
|
package obiutils
|
|
|
|
|
|
|
|
|
|
import "golang.org/x/exp/constraints"
|
|
|
|
|
|
2024-10-28 21:51:21 +01:00
|
|
|
// Abs returns the absolute value of x.
|
|
|
|
|
//
|
|
|
|
|
// It is a generic function that can be used on any signed type.
|
2024-06-18 23:38:33 +02:00
|
|
|
func Abs[T constraints.Signed](x T) T {
|
|
|
|
|
if x < 0 {
|
|
|
|
|
return -x
|
|
|
|
|
}
|
2024-10-28 21:51:21 +01:00
|
|
|
|
2024-06-18 23:38:33 +02:00
|
|
|
return x
|
|
|
|
|
}
|