From 9e9be0b9fcfaa3a48b9ce0937b5e5b15139059b2 Mon Sep 17 00:00:00 2001 From: Bart de Koning Date: Tue, 3 Sep 2024 16:34:14 +0200 Subject: [PATCH 1/6] Catch inexact rounding error --- src/FindFirstFunctions.jl | 6 +++++- test/runtests.jl | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/FindFirstFunctions.jl b/src/FindFirstFunctions.jl index 6322da8..93bf103 100644 --- a/src/FindFirstFunctions.jl +++ b/src/FindFirstFunctions.jl @@ -238,7 +238,11 @@ 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) + try + round(typeof(firstindex(v)), f * (i_f - i_0) + i_0) + catch + idx_prev[] + end end else idx_prev[] diff --git a/test/runtests.jl b/test/runtests.jl index 1e49042..f99c5e8 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 end From e2a8addf872e7d003cac648154077da84531b569 Mon Sep 17 00:00:00 2001 From: Bart de Koning Date: Tue, 3 Sep 2024 17:42:11 +0200 Subject: [PATCH 2/6] Avoid try/catch --- src/FindFirstFunctions.jl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/FindFirstFunctions.jl b/src/FindFirstFunctions.jl index 93bf103..1f22b45 100644 --- a/src/FindFirstFunctions.jl +++ b/src/FindFirstFunctions.jl @@ -238,9 +238,10 @@ function (g::Guesser)(x) f > 0 ? lastindex(v) : firstindex(v) else i_0, i_f = firstindex(v), lastindex(v) - try - round(typeof(firstindex(v)), f * (i_f - i_0) + i_0) - catch + i_appr = f * (i_f - i_0) + i_0 + if typemin(Int64) <= x <= typemax(Int64) + round(typeof(firstindex(v)), i_appr) + else idx_prev[] end end From a30dbcc2897b99012d1e7fa5afa0e00f1e1f9c50 Mon Sep 17 00:00:00 2001 From: Bart de Koning Date: Tue, 3 Sep 2024 17:43:19 +0200 Subject: [PATCH 3/6] fix --- src/FindFirstFunctions.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/FindFirstFunctions.jl b/src/FindFirstFunctions.jl index 1f22b45..d6956d8 100644 --- a/src/FindFirstFunctions.jl +++ b/src/FindFirstFunctions.jl @@ -239,7 +239,7 @@ function (g::Guesser)(x) else i_0, i_f = firstindex(v), lastindex(v) i_appr = f * (i_f - i_0) + i_0 - if typemin(Int64) <= x <= typemax(Int64) + if typemin(Int64) <= i_appr <= typemax(Int64) round(typeof(firstindex(v)), i_appr) else idx_prev[] From 82492794fa269bfe8b6fccb8f8070c403c487621 Mon Sep 17 00:00:00 2001 From: Bart de Koning Date: Tue, 3 Sep 2024 17:44:41 +0200 Subject: [PATCH 4/6] another fix --- src/FindFirstFunctions.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/FindFirstFunctions.jl b/src/FindFirstFunctions.jl index d6956d8..08e5c2c 100644 --- a/src/FindFirstFunctions.jl +++ b/src/FindFirstFunctions.jl @@ -239,8 +239,9 @@ function (g::Guesser)(x) else i_0, i_f = firstindex(v), lastindex(v) i_appr = f * (i_f - i_0) + i_0 - if typemin(Int64) <= i_appr <= typemax(Int64) - round(typeof(firstindex(v)), i_appr) + target_type = typeof(firstindex(v)) + if typemin(target_type) <= i_appr <= typemax(target_type) + round(target_type, i_appr) else idx_prev[] end From 3da9474cc8ad27cd9a3f747cc8e039deec1ed633 Mon Sep 17 00:00:00 2001 From: Bart de Koning Date: Tue, 3 Sep 2024 18:17:19 +0200 Subject: [PATCH 5/6] Apply suggestion --- src/FindFirstFunctions.jl | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/FindFirstFunctions.jl b/src/FindFirstFunctions.jl index 08e5c2c..6a13b31 100644 --- a/src/FindFirstFunctions.jl +++ b/src/FindFirstFunctions.jl @@ -238,12 +238,14 @@ function (g::Guesser)(x) f > 0 ? lastindex(v) : firstindex(v) else i_0, i_f = firstindex(v), lastindex(v) - i_appr = f * (i_f - i_0) + i_0 - target_type = typeof(firstindex(v)) - if typemin(target_type) <= i_appr <= typemax(target_type) - round(target_type, i_appr) + i_approx = f * (i_f - i_0) + i_0 + target_type = eltype(v) + if i_approx <= typemin(target_type) + firstindex(v) - 1 + elseif i >= typemax(target_type) + lastindex(v) + 1 else - idx_prev[] + round(target_type, i_appr) end end else From 06c8904c568ac268d36174885cd0825836f5cfcc Mon Sep 17 00:00:00 2001 From: Bart de Koning Date: Tue, 3 Sep 2024 18:31:37 +0200 Subject: [PATCH 6/6] Apply suggestion --- src/FindFirstFunctions.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/FindFirstFunctions.jl b/src/FindFirstFunctions.jl index 6a13b31..996556d 100644 --- a/src/FindFirstFunctions.jl +++ b/src/FindFirstFunctions.jl @@ -239,13 +239,13 @@ function (g::Guesser)(x) else i_0, i_f = firstindex(v), lastindex(v) i_approx = f * (i_f - i_0) + i_0 - target_type = eltype(v) - if i_approx <= typemin(target_type) - firstindex(v) - 1 - elseif i >= typemax(target_type) + 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_appr) + round(target_type, i_approx) end end else