Skip to content

Commit 81a044f

Browse files
authored
fix #35272, searchsorted(3:-1:1, 2.5, rev=true) (#35274)
1 parent 5ecc17f commit 81a044f

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

base/sort.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,11 @@ function searchsortedlast(a::AbstractRange{<:Integer}, x::Real, o::DirectOrderin
262262
elseif h < 0 && x <= last(a)
263263
lastindex(a)
264264
else
265-
fld(floor(Integer, x) - first(a), h) + 1
265+
if o isa ForwardOrdering
266+
fld(floor(Integer, x) - first(a), h) + 1
267+
else
268+
fld(ceil(Integer, x) - first(a), h) + 1
269+
end
266270
end
267271
end
268272

@@ -280,7 +284,11 @@ function searchsortedfirst(a::AbstractRange{<:Integer}, x::Real, o::DirectOrderi
280284
elseif h < 0 && x < last(a)
281285
lastindex(a) + 1
282286
else
283-
-fld(floor(Integer, -x) + first(a), h) + 1
287+
if o isa ForwardOrdering
288+
-fld(floor(Integer, -x) + first(a), h) + 1
289+
else
290+
-fld(ceil(Integer, -x) + first(a), h) + 1
291+
end
284292
end
285293
end
286294

test/sorting.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,17 @@ end
177177
end
178178
end
179179
end
180+
@testset "issue #35272" begin
181+
for v0 = (3:-1:1, 3.0:-1.0:1.0), v = (v0, collect(v0))
182+
@test searchsorted(v, 3, rev=true) == 1:1
183+
@test searchsorted(v, 3.0, rev=true) == 1:1
184+
@test searchsorted(v, 2.5, rev=true) == 2:1
185+
@test searchsorted(v, 2, rev=true) == 2:2
186+
@test searchsorted(v, 1.2, rev=true) == 3:2
187+
@test searchsorted(v, 1, rev=true) == 3:3
188+
@test searchsorted(v, 0.1, rev=true) == 4:3
189+
end
190+
end
180191
end
181192
# exercise the codepath in searchsorted* methods for ranges that check for zero step range
182193
struct ConstantRange{T} <: AbstractRange{T}

0 commit comments

Comments
 (0)