mirror of
https://github.com/duke-git/lancet.git
synced 2026-11-16 05:12:11 +08:00
fix(mathutil): stop stepped ranges when values stop increasing (#376)
This commit is contained in:
1 parent
b40eea78c3
commit
b282ada39e
2 files changed
+11
-2
No files matched your search
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user