diff --git a/src/FindFirstFunctions.jl b/src/FindFirstFunctions.jl index c324303..1c7fc52 100644 --- a/src/FindFirstFunctions.jl +++ b/src/FindFirstFunctions.jl @@ -240,7 +240,15 @@ function (g::Guesser)(x) f > 0 ? lastindex(v) : firstindex(v) else i_0, i_f = firstindex(v), lastindex(v) - round(typeof(firstindex(v)), f * (i_f - i_0) + i_0) + i_approx = f * (i_f - i_0) + i_0 + target_type = typeof(firstindex(v)) + if i_approx >= typemax(target_type) + lastindex(v) + 1 + elseif i_approx <= typemin(target_type) + firstindex(v) - 1 + else + round(target_type, i_approx) + end end else idx_prev[] diff --git a/test/runtests.jl b/test/runtests.jl index 2d3a88b..4b95bd8 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -37,6 +37,7 @@ using SafeTestsets, Test guesser_prev = Guesser(v, Ref(1), false) @test guesser_linear.linear_lookup @test searchsortedfirstcorrelated(v, 4.0, guesser_linear) == 3 + @test searchsortedfirstcorrelated(v, 1.4234326478e24, guesser_linear) == 5 @test searchsortedlastcorrelated(v, 4.0, guesser_prev) == 2 @test guesser_prev.idx_prev[] == 2