AxisKeys.jl icon indicating copy to clipboard operation
AxisKeys.jl copied to clipboard

Package calling `searchsortedfirst` with non sorted ranges

Open KristofferC opened this issue 4 years ago • 1 comments

In 1.6.1 the following behavior changed:

1.6.0:

julia> searchsortedfirst(1.0:-0.1:0.1, 0.3)
8

julia> searchsortedfirst(collect(1.0:-0.1:0.1), 0.3)
1

1.6.1:

julia> searchsortedfirst(1.0:-0.1:0.1, 0.3)
1

julia> searchsortedfirst(collect(1.0:-0.1:0.1), 0.3)
1

This causes the test here https://github.com/mcabbott/AxisKeys.jl/blob/cddf60f65113796bba7ff6371112e1827fae8468/test/_basic.jl#L106 to fail.

The issue seems to be that the package is using searchsortedfirst but the range it passes in is not sorted: https://github.com/mcabbott/AxisKeys.jl/blob/cddf60f65113796bba7ff6371112e1827fae8468/src/selectors.jl#L67

4|debug> st
In findindex(sel, range) at /Users/kristoffercarlsson/.julia/packages/AxisKeys/G3Okw/src/selectors.jl:66
 66  function findindex(sel::Near, range::AbstractRange)
 67      iplus = searchsortedfirst(range, sel.val)
 68      # "index of the first value in a greater than or equal to x"
>69      if abs(range[iplus]-sel.val) < abs(range[iplus-1]-sel.val)
 70          return iplus
 71      else
 72          return iplus-1
 73      end
 74  end

About to run: (getindex)(1.0:-0.1:0.1, 0)
4|julia> range
1.0:-0.1:0.1

4|julia> issorted(range)
false

Perhaps a rev was forgotten to be passed to searchsortedfirst?

KristofferC avatar Apr 14 '21 10:04 KristofferC

Thanks, that does look wrong.

Looks like it has other bugs, too, when I try to test it more deeply:

julia> AxisKeys.findindex(Near(0.1), 3:0.1:4)
ERROR: BoundsError: attempt to access 11-element StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}} at index [0]

mcabbott avatar Apr 14 '21 14:04 mcabbott