Interpolations.jl
Interpolations.jl copied to clipboard
Roundoff-error in constant (previous-value) interpolation
Since the previous-value interpolation feature was merged, I wanted to use it as a lookup function inside another function.
But the community helped me discover that there was an issue with precision/rounding. Consider this simple table, read in from a CSV file called Inputarray_Odm2prod_2h_3d.csv
:
3300, 430, 32.64, 32.64
3521, 0, 32.64, 32.64
3660, 60, 249.83539555, 58.001892497
3828, 0, 249.83539555, 58.001892497
10860, 60, 249.83539555, 58.001892497
11028, 0, 249.83539555, 58.001892497
18060, 60, 249.83539555, 58.001892497
18228, 0, 249.83539555, 58.001892497
The following code constructs said lookup function and calls it:
using DelimitedFiles
using Interpolations
f_tScal = 24 # Use hours instead of days
# %% Read input from CSV file
inputArray = readdlm("Inputarray_Odm2prod_2h_3d.csv", ',', Float64)
inputArray[:,1] /= (86400/f_tScal) # Scale input from per day to per second (or whatever)
# does nearest-neighbor interpolation! Only 2D
itp_q_in1(t) = extrapolate(interpolate((inputArray[:,1],), inputArray[:,2], Gridded(Constant{Previous}())), 0.0)(t)
trange = inputArray[1:8,1]
# Showing precision issue by adding a very small number to each element of the t vector
[itp_q_in1(trange), itp_q_in1(trange.+1e-8)]
gives
2-element Vector{Vector{Float64}}:
[430.0, 430.0, 0.0, 60.0, 0.0, 60.0, 0.0, 60.0]
[430.0, 0.0, 60.0, 0.0, 60.0, 0.0, 60.0, 0.0]
As you can see, except for the first element, all values are different. Actually, the second output line would be the correct one I expect, but I only get it by adding this small number to the trange
vector.
Is this the expected behavior? Is there some glitch in my code? Or is it just a bug?
Using Gridded
for this is definitely not ideal because we end up having to scale the input and run into floating point issues. Let me investigate this further.