Skip to content

Commit 8e4613b

Browse files
Merge pull request #376 from SouthEndMusic/fix_ConstantInterpolationExtrapolation
Fix `ConstantInterpolation` integer extrapolation
2 parents f37ff2a + 5966a61 commit 8e4613b

File tree

4 files changed

+11
-4
lines changed

4 files changed

+11
-4
lines changed

src/derivatives.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,11 @@ function _derivative(A::ConstantInterpolation, t::Number, iguess)
159159
end
160160

161161
function _derivative(A::ConstantInterpolation{<:AbstractVector}, t::Number, iguess)
162-
return isempty(searchsorted(A.t, t)) ? zero(A.u[1]) : eltype(A.u)(NaN)
162+
return isempty(searchsorted(A.t, t)) ? zero(A.u[1]) : typed_nan(A.u)
163163
end
164164

165165
function _derivative(A::ConstantInterpolation{<:AbstractMatrix}, t::Number, iguess)
166-
return isempty(searchsorted(A.t, t)) ? zero(A.u[:, 1]) : eltype(A.u)(NaN) .* A.u[:, 1]
166+
return isempty(searchsorted(A.t, t)) ? zero(A.u[:, 1]) : typed_nan(A.u) .* A.u[:, 1]
167167
end
168168

169169
# QuadraticSpline Interpolation

src/interpolation_methods.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function _extrapolate_left(A, t)
1414
throw(LeftExtrapolationError())
1515
elseif extrapolation_left == ExtrapolationType.Constant
1616
slope = derivative(A, first(A.t))
17-
first(A.u) + slope * zero(t)
17+
first(A.u) + zero(slope * t)
1818
elseif extrapolation_left == ExtrapolationType.Linear
1919
slope = derivative(A, first(A.t))
2020
first(A.u) + slope * (t - first(A.t))
@@ -36,7 +36,7 @@ function _extrapolate_right(A, t)
3636
throw(RightExtrapolationError())
3737
elseif extrapolation_right == ExtrapolationType.Constant
3838
slope = derivative(A, last(A.t))
39-
last(A.u) + slope * zero(t)
39+
last(A.u) + zero(slope * t)
4040
elseif extrapolation_right == ExtrapolationType.Linear
4141
slope = derivative(A, last(A.t))
4242
last(A.u) + slope * (t - last(A.t))

src/interpolation_utils.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,6 @@ function transformation_reflective(A, t)
327327
(n > 0) && (n -= 1)
328328
t_, n
329329
end
330+
331+
typed_nan(::AbstractArray{T}) where {T <: AbstractFloat} = T(NaN)
332+
typed_nan(::AbstractArray{T}) where {T <: Integer} = zero(T)

test/interpolation_tests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,10 @@ end
520520
A = ConstantInterpolation(u, t; extrapolation = ExtrapolationType.Extension)
521521
@test A(Inf) == last(u)
522522
@test A(-Inf) == first(u)
523+
524+
# Test extrapolation of integer output
525+
itp = ConstantInterpolation([2], [0.0]; extrapolation = ExtrapolationType.Constant)
526+
@test itp(1.0) == 2
523527
end
524528

525529
@testset "QuadraticSpline Interpolation" begin

0 commit comments

Comments
 (0)