Skip to content

Commit f337f84

Browse files
Merge pull request #26 from SouthEndMusic/catch_inexact_rounding_error
Catch inexact rounding error
2 parents 9f69a2d + 06c8904 commit f337f84

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/FindFirstFunctions.jl

+9-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,15 @@ function (g::Guesser)(x)
240240
f > 0 ? lastindex(v) : firstindex(v)
241241
else
242242
i_0, i_f = firstindex(v), lastindex(v)
243-
round(typeof(firstindex(v)), f * (i_f - i_0) + i_0)
243+
i_approx = f * (i_f - i_0) + i_0
244+
target_type = typeof(firstindex(v))
245+
if i_approx >= typemax(target_type)
246+
lastindex(v) + 1
247+
elseif i_approx <= typemin(target_type)
248+
firstindex(v) - 1
249+
else
250+
round(target_type, i_approx)
251+
end
244252
end
245253
else
246254
idx_prev[]

test/runtests.jl

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ using SafeTestsets, Test
3737
guesser_prev = Guesser(v, Ref(1), false)
3838
@test guesser_linear.linear_lookup
3939
@test searchsortedfirstcorrelated(v, 4.0, guesser_linear) == 3
40+
@test searchsortedfirstcorrelated(v, 1.4234326478e24, guesser_linear) == 5
4041
@test searchsortedlastcorrelated(v, 4.0, guesser_prev) == 2
4142
@test guesser_prev.idx_prev[] == 2
4243

0 commit comments

Comments
 (0)