JuliaMath / JuliaMath/Interpolations.jl

Roundoff-error in constant (previous-value) interpolation

Open
#473 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Julia
Stars
575
Forks
117
PR merge metrics
No merged PRs in 30d

Description

Since the [previous-value interpolation feature was merged](https://github.com/JuliaMath/Interpolations.jl/pull/338), I wanted to use it as a lookup function [inside another function](https://github.com/SciML/DifferentialEquations.jl/issues/805).
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?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.