-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Linear interpolation #85925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Linear interpolation #85925
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -876,4 +876,32 @@ impl f32 { | |
pub fn atanh(self) -> f32 { | ||
0.5 * ((2.0 * self) / (1.0 - self)).ln_1p() | ||
} | ||
|
||
/// Linear interpolation between `start` and `end`. | ||
/// | ||
/// This enables the calculation of a "smooth" transition between `start` and `end`, | ||
/// where start is represented by `self == 0.0` and `end` is represented by `self == 1.0`. | ||
/// | ||
/// Values below 0.0 or above 1.0 are allowed, and in general this function closely | ||
/// resembles the value of `start + self * (end - start)`, plus additional guarantees. | ||
clarfonthey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// | ||
/// Those guarantees are, assuming that all values are [`finite`]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we give any guarantees about non-finite values? E.g. that any nan wil result in a nan result, and/or that if only one of the values is infinite, the result will be that infinite value. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, I was considering doing the full list of guarantees that C++ provides, which are:
But I figured that it might potentially limit what kinds of implementations we allow. I'll try adding tests for these (and comment them out if they don't work) and maybe we can hash it out in the tracking issue. |
||
/// | ||
/// * The value at 0.0 is always `start` and the value at 1.0 is always `end` (exactness) | ||
/// * If `start == end`, the value at any point will always be `start == end` (consistency) | ||
/// * The values will always move in the direction from `start` to `end` (monotonicity) | ||
/// | ||
/// [`finite`]: #method.is_finite | ||
#[must_use = "method returns a new number and does not mutate the original value"] | ||
#[unstable(feature = "float_interpolation", issue = "71015")] | ||
pub fn lerp(self, start: f32, end: f32) -> f32 { | ||
// consistent | ||
if start == end { | ||
start | ||
|
||
// exact/monotonic | ||
} else { | ||
self.mul_add(end, (-self).mul_add(start, start)) | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.