You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Evaluation on grid points does not match the input exactly #207
First, thanks for this fabulous work, which is indeed fast and contains very useful functionalities, like PCHIP and periodic BCs.
The issue I ran into is like this:
x =-1:0.1:1;
y =rand(length(x));
itp =pchip_interp(x, y);
itp.(x) - y
The difference contains several small (~1e-16) but nonzero numbers.
I also check cubic_interp and the result is worse. itp.(x) and y are different in general.
This should not be an issue in practice, but I would expect a polynomial interpolator to generate exactly the same result as the input data. Am I wrong or there is some subtle issue in the code?
Hi @linwaytin,
Thanks for the kind words and the report!
In short, this is expected and intended behavior.
The node values may differ only in the last few digits, which is a fair (and largely unavoidable) trade-off for performance.
Other packages like Interpolations.jl and DataInterpolations.jl have the same issues.
In particular, Range grids (e.g., -1:0.1:1) are fundamentally harder.
Node positions aren't stored; they're computed at everytime we request value, and it may differ depending on the internal definition/implementaion of Range.
That computation can introduce small round-off, so a node position value may differ slightly.
(FastInterpolations internally redefines the Range in a simpler way for the performance)
Nevertheless, if your grid is uniform, I recommend keeping it as a range, since it's much faster.
If you really need exact node values, consider a Vector grid (e.g., x = collect(-1:0.1:1)).
Keep in mind that Vector grids are generally slower, and some methods may still not match exactly even there, due to floating-point rounding in the evaluation.
One general tip:
if you have many query points (xq), try the batch query form of itp(xq) instead of broadcasting form of itp.(xq).
FastInterpolations has optimizations specific to batch queries, so itp(xq) is usually faster.
I just noticed that, however, in the case of (pchip + range grid), the batch query form is slightly slower than broadcasting form.
This is probably because pchip is not fully optimized yet.
I will look into it and make it faster.
Thanks again for the report!
As you said, this is not a real issue, but just for my curiosity, I did some experiments. Indeed using Vector largely improves the consistency (and again pchip_interp is much better than cubic_interp), but it can still happen, especially at the end points.
Just a quick update: PR #211 has resolved the inconsistency in cubic interpolation.
Now, Vector grid gives the exact nodal values (except for the last node; last nodal value may still have round-off errors)
The cubic kernel improvements address the accuracy issue and, as a bonus, also improve performance :)
Activity
mgyoo86 commentedon Sep 30, 2026
Hi @linwaytin,
Thanks for the kind words and the report!
In short, this is expected and intended behavior.
The node values may differ only in the last few digits, which is a fair (and largely unavoidable) trade-off for performance.
Other packages like
Interpolations.jlandDataInterpolations.jlhave the same issues.In particular,
Rangegrids (e.g.,-1:0.1:1) are fundamentally harder.Node positions aren't stored; they're computed at everytime we request value, and it may differ depending on the internal definition/implementaion of
Range.That computation can introduce small round-off, so a node position value may differ slightly.
(
FastInterpolationsinternally redefines theRangein a simpler way for the performance)Nevertheless, if your grid is uniform, I recommend keeping it as a range, since it's much faster.
If you really need exact node values, consider a
Vectorgrid (e.g.,x = collect(-1:0.1:1)).Keep in mind that
Vectorgrids are generally slower, and some methods may still not match exactly even there, due to floating-point rounding in the evaluation.One general tip:
if you have many query points (
xq), try the batch query form ofitp(xq)instead of broadcasting form ofitp.(xq).FastInterpolations has optimizations specific to batch queries, so
itp(xq)is usually faster.I just noticed that, however, in the case of (pchip + range grid), the batch query form is slightly slower than broadcasting form.
This is probably because pchip is not fully optimized yet.
I will look into it and make it faster.
Thanks again for the report!
linwaytin commentedon Sep 30, 2026
Thank you so much for the detailed explanation!
As you said, this is not a real issue, but just for my curiosity, I did some experiments. Indeed using
Vectorlargely improves the consistency (and againpchip_interpis much better thancubic_interp), but it can still happen, especially at the end points.Thanks for your tip on the performance!
mgyoo86 commentedon Sep 30, 2026
Yes, thanks to this isssue, I noticed some edge cases and room for improvements.
I'll try to fix issues and improve performance in the near future!
Please let me know if you find any other issues or have new ideas/suggestions.
I'll be happy to take a look :)
fix(cubic): exact node values from the cubic value kernel (#211)
mgyoo86 commentedon Oct 7, 2026
Hi @linwaytin,
Just a quick update: PR #211 has resolved the inconsistency in cubic interpolation.
Now,
Vectorgrid gives the exact nodal values (except for the last node; last nodal value may still have round-off errors)The cubic kernel improvements address the accuracy issue and, as a bonus, also improve performance :)
linwaytin commentedon Oct 8, 2026
That is awesome! Thanks for the update.
mgyoo86 commentedon Oct 14, 2026
@linwaytin
FYI, the new version (v0.4.20) has just been released.
Please give it a try and let me know if you find any further issues :)