summaryrefslogtreecommitdiff
path: root/internal/util/math.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/util/math.go')
-rw-r--r--internal/util/math.go9
1 files changed, 9 insertions, 0 deletions
diff --git a/internal/util/math.go b/internal/util/math.go
index e1850f772..f6e78fa1c 100644
--- a/internal/util/math.go
+++ b/internal/util/math.go
@@ -23,6 +23,15 @@ type Number interface {
~uintptr | ~float32 | ~float64
}
+// Decr performs a safe decrement of
+// n, clamping minimum value at zero.
+func Decr[N Number](n N) N {
+ if n <= 0 {
+ return 0
+ }
+ return n - 1
+}
+
// Div performs a safe division of
// n1 and n2, checking for zero n2. In the
// case of zero n2, zero is returned.