Skip to content

Commit 847dca1

Browse files
authored
Fix findfirst for InfStepRange for negative step (#192)
* Fix findfirst for InfStepRange for negative step * Use parantheses in bounds check
1 parent d9f862f commit 847dca1

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/infrange.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ MemoryLayout(::Type{<:AbstractInfUnitRange}) = LazyLayout()
571571

572572
# from array.jl
573573
function _step_findfirst(p, r::InfStepRange{T,S}) where {T,S}
574-
first(r) <= p.x || return nothing
574+
(first(r) <= p.x && step(r) > zero(step(r))) || (first(r) >= p.x && step(r) < zero(step(r))) || return nothing
575575
d = convert(S, p.x - first(r))
576576
iszero(d % step(r)) || return nothing
577577
return d ÷ step(r) + 1

test/runtests.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,16 @@ end
12291229
@test v[1:∞] == v
12301230
end
12311231

1232+
@testset "issue #180" begin
1233+
@test isnothing(findfirst(==(21), 10:-1:-∞))
1234+
@test isnothing(findfirst(==(11), 10:-1:-∞))
1235+
@test findfirst(==(9), 10:-1:-∞) == 2
1236+
r = 10:-1:-
1237+
v = -20
1238+
ind = findfirst(==(v), r)
1239+
@test r[ind] == v
1240+
end
1241+
12321242
include("test_infconv.jl")
12331243
include("test_block.jl")
12341244

0 commit comments

Comments
 (0)