LagrangeInterpolation returns NaN when the first knot is at x = 0.0
Summary
LagrangeInterpolation returns NaN when the first knot is at x = 0.0 and the interpolation is evaluated exactly at that knot. The exact-knot shortcut degenerates instead of firing.
Repro (QuantLib-Python 1.44, Python 3.14; same code path at current master):
import QuantLib as ql
f = ql.LagrangeInterpolation([0.0, 1.0, 2.0, 3.0], [0.30, 0.28, 0.29, 0.27])
print(f(0.0)) # -> nan, expected 0.30
Mechanism
In lagrangeinterpolation.hpp, the exact-knot test uses an absolute epsilon scaled by the node magnitude, eps = 10 * QL_EPSILON * |x|. At x = 0.0 this is exactly 0, so the shortcut test *iter - x < eps evaluates 0 < 0 and falls through to the barycentric loop, where the first weight computes lambda[0] / (0 - 0) = inf/inf = NaN.
Controls: with the first knot at 0.5 or -2.0 the evaluation is exact; derivative(0.0) is unaffected because it uses close_enough.
Impact
Any interpolation grid whose first abscissa is 0.0 (a time-to-maturity or moneyness grid is the natural case) silently returns NaN for in-range queries at the first knot, instead of the data value.
Suggested fix
Treat the exact-knot shortcut with a test that does not degenerate at 0, e.g. use close_enough(*iter, x) (as the derivative path already does), or a relative epsilon against |*iter - x| with an absolute floor.
Environment
QuantLib-Python 1.44; verified in source at master e429e9f1 (ql/math/interpolations/lagrangeinterpolation.hpp).
Found by a formal-verification audit of historical QuantLib PRs; AI-assisted (ZCode/GLM-5.3-flash).
Thanks for posting! It might take a while before we look at your issue, so don't worry if there seems to be no feedback. We'll get to it.
Hi! I would like to work on this issue. I plan to implement the suggested fix by updating the exact-knot test to prevent the zero-epsilon degeneration when x=0.0. I will also add a corresponding unit test to verify this edge case.