Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Roundoff-error in constant (previous-value) interpolation #473

Open
winkmal opened this issue Dec 15, 2021 · 1 comment
Open

Roundoff-error in constant (previous-value) interpolation #473

winkmal opened this issue Dec 15, 2021 · 1 comment

Comments

@winkmal
Copy link

winkmal commented Dec 15, 2021

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?

@mkitti
Copy link
Collaborator

mkitti commented Dec 15, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants