gh-154573: Add math.lerp() for linear interpolation#154574
Open
PhysicistJohn wants to merge 2 commits into
Open
gh-154573: Add math.lerp() for linear interpolation#154574PhysicistJohn wants to merge 2 commits into
PhysicistJohn wants to merge 2 commits into
Conversation
Adds math.lerp(a, b, t), computing linear interpolation between a and b using the algorithm standardized for C++20's std::lerp ([c.math.lerp], WG21 P0811R3 "Well-behaved interpolation for numbers and pointers" by S. Davis Herring). Unlike the naive "a + t * (b - a)" formula, this guarantees exact endpoints (lerp(a, b, 0.0) == a, lerp(a, b, 1.0) == b), monotonicity in t, and lerp(a, a, t) == a for any finite t. Includes tests (LerpTests in test_math.py), docs, a What's New entry, and a NEWS entry.
Documentation build overview
|
Missed this in the initial commit -- the detailed function docs were there but the table at the top of the page (which lists fma, fmax, fmin, etc.) didn't have an entry.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
math.lerp(a, b, t), computing linear interpolation betweenaand
busing the algorithm standardized for C++20'sstd::lerp([c.math.lerp], WG21 P0811R3 "Well-behaved interpolation for numbers
and pointers" by S. Davis Herring). Fixes gh-154573.
Unlike the naive
a + t * (b - a)formula, this guarantees:lerp(a, b, 0.0) == a,lerp(a, b, 1.0) == b)tlerp(a, a, t) == afor any finitetI verified this port against the real algorithm several independent
ways before opening this:
std::lerpanddiffed bit patterns across ~25 test vectors, including inf/NaN edge
cases: bit-for-bit identical.
libcxx/include/cmathsource: the algorithm is line-for-line the same, modulo variable
names.
(a, b, t)triples for endpointexactness, monotonicity, and the
a == binvariant: zero failures.test_math.py/test_cmath/test_capiregression run: nofailures, no new compiler warnings.
Includes tests (
LerpTestsintest_math.py), docs(
Doc/library/math.rst), a What's New entry, and a NEWS entry.