Skip to content

Commit

Permalink
Merge pull request #26 from SouthEndMusic/catch_inexact_rounding_error
Browse files Browse the repository at this point in the history
Catch inexact rounding error
  • Loading branch information
ChrisRackauckas authored Sep 4, 2024
2 parents 9f69a2d + 06c8904 commit f337f84
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/FindFirstFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit f337f84

Please sign in to comment.