From e8aa16622b71139d7a5f8b3d27c18978c5a4f0c5 Mon Sep 17 00:00:00 2001 From: mstar Date: Thu, 10 Apr 2025 14:36:51 +0200 Subject: [PATCH] Add generic math abs --- math/math.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 math/math.go diff --git a/math/math.go b/math/math.go new file mode 100644 index 0000000..8f04ebc --- /dev/null +++ b/math/math.go @@ -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 + } +}