From b282ada39e5437b858b163ef4ade7d4c4065daa0 Mon Sep 17 00:00:00 2001 From: Yusuf Efe <120668197+yuefdev@users.noreply.github.com> Date: Fri, 23 Oct 2026 13:04:43 +0300 Subject: [PATCH] fix(mathutil): stop stepped ranges when values stop increasing (#376) --- mathutil/mathutil.go | 9 +++++++-- mathutil/mathutil_test.go | 4 ++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/mathutil/mathutil.go b/mathutil/mathutil.go index c617360..9966263 100644 --- a/mathutil/mathutil.go +++ b/mathutil/mathutil.go @@ -270,12 +270,17 @@ func Range[T constraints.Integer | constraints.Float](start T, count int) []T { func RangeWithStep[T constraints.Integer | constraints.Float](start, end, step T) []T { result := []T{} - if start >= end || step == 0 { + if start >= end || step <= 0 { return result } - for i := start; i < end; i += step { + for i := start; i < end; { result = append(result, i) + next := i + step + if next <= i { + break + } + i = next } return result diff --git a/mathutil/mathutil_test.go b/mathutil/mathutil_test.go index dd079ce..1bbc993 100644 --- a/mathutil/mathutil_test.go +++ b/mathutil/mathutil_test.go @@ -260,6 +260,10 @@ func TestRangeWithStep(t *testing.T) { t.Parallel() assert := internal.NewAssert(t, "TestRangeWithStep") + assert.Equal([]int8{120}, RangeWithStep[int8](120, 125, 10)) + assert.Equal([]uint8{250}, RangeWithStep[uint8](250, 253, 10)) + assert.Equal([]int{}, RangeWithStep(1, 4, -1)) + assert.Equal([]float64{1e20}, RangeWithStep(1e20, 1e21, 1.0)) result1 := RangeWithStep(1, 4, 1) result2 := RangeWithStep(1, -1, 0)