Skip to content

Allow returning quiet_NaN() when out of bounds - #36

Open
renggl0j wants to merge 2 commits into
CD3:masterfrom
renggl0j:proposal/nan
Open

renggl0j wants to merge 2 commits into
CD3:masterfrom
renggl0j:proposal/nan

Conversation

@renggl0j

@renggl0j renggl0j commented Oct 8, 2026

Copy link
Copy Markdown

Hi,

I'm using your interpolators in my code. Unfortunately when x is out of bounds they return 0, which is not easy to discriminate if a valid interpolations may also return 0.

I'm therefore proposing this change so the caller may chose a different invalid value to return (typically std::numeric_limits<float>::quiet_NaN()), whilst guaranteeing backward-compatilibity.

@CD3

CD3 commented Oct 13, 2026

Copy link
Copy Markdown
Owner

Thank you for the pull request.

There was another request to allow extrapolation for out of bounds values. I've pushed a branch that allows configuring the out-of-bounds value when extrapolation is disabled and does a "reasonable" extrapolation when enabled. The interface looks like this

TEST_CASE("Extrapolation settings") {
    _1D::CubicSplineInterpolator<double> interp;

    int N = 100;
    double xmin = 0, xmax = 2 * M_PI;
    double dx = (xmax - xmin) / (N - 1);

    _1D::CubicSplineInterpolator<double>::VectorType xx(N), yy(N);

    CHECK_THROWS_AS(interp(1), std::logic_error);

    for (int i = 0; i < N; i++) {
        xx(i) = dx * i;
        yy(i) = sin(xx(i));
    }
    interp.setData(xx, yy);

    CHECK(interp(-1) == 0);
    CHECK(interp(8) == 0);
    CHECK(!std::isnan(interp(-1)));
    CHECK(!std::isnan(interp(-4)));

    interp.setOutOfBoundsValue(1);

    CHECK(interp(-1) == 1);
    CHECK(interp(8) == 1);
    CHECK(!std::isnan(interp(-1)));
    CHECK(!std::isnan(interp(-4)));

    interp.setOutOfBoundsNaN();

    CHECK(interp(-1) != std::numeric_limits<double>::quiet_NaN());
    CHECK(interp(8) != std::numeric_limits<double>::quiet_NaN());
    CHECK(std::isnan(interp(-1)));
    CHECK(std::isnan(interp(-4)));

    interp.enableExtrapolation();

    CHECK(interp(-1) == Approx(-1));
    CHECK(interp(8) == Approx(8 - 2 * M_PI));
}

Does this do what you are looking for?

@CD3

CD3 commented Oct 13, 2026

Copy link
Copy Markdown
Owner

#35 would be resolved with feat-extrapolation-configuration branch too.

@renggl0j

Copy link
Copy Markdown
Author

Yes, that would be an even better interface than my quick proposal, thanks.

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

Successfully merging this pull request may close these issues.

2 participants