Skip to content

Avoid overflowing intermediate differences in floating-point lerp #556

Closed

Description

@LunaStev

lerp_f32/f64 uses a + (b-a)*amount. Opposite-sign large finite endpoints can overflow b-a even when the interpolation result is finite. With a=1e308, b=-1e308 and amount=0.5, the midpoint is zero but the formula propagates infinity. At amount=0, the same overflow is multiplied by zero and can yield NaN instead of returning a.

This affects interpolation within [0,1], not only extrapolation outside the endpoints.

Code evidence:

Acceptance:

  • Use a numerically stable interpolation path for finite endpoints, preserving exact endpoint results at amounts 0 and 1.
  • Cover opposite-sign extreme values and finite midpoints for both float widths.
  • Test monotonicity and ordinary close-endpoint inputs so fixing overflow does not introduce unnecessary cancellation regressions.
  • Document exceptional-input behavior briefly and keep the implementation independent of libm/libc.

Audit status: static source inspection against canonical master ea74c2dafc31da876e5561f2d176ba719f5a0458 on 2026-09-10. No compiler build, test execution, implementation change or local reproduction was performed for this report. The examples describe the source-derived behavior to verify when implementing the fix.

Activity

  1. added
    bug
    help wanted
    on Sep 10, 2026
  2. added a commit that references this issue on Sep 16, 2026
    451cc7a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bughelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions