Skip to content

Linear interpolation (lerp) implementation #71016

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/libstd/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,30 @@ impl f32 {
}
x
}

/// Computes `self + step(upper - pol)` the linear interpolation between
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what step refers to but this description doesn't look like it matches the implementation. In particular pol is dimensionless and upper is not so upper - pol is not an expression that would make sense.

/// `self` and `upper` for the parameter `pol` (or extrapolation,
/// when `pol` is outside the range [0,1]).
///
/// # Examples
///
/// ```
/// #![feature(lerp)]
/// let x = 10.0_f32;
/// let y = 20.0_f32;
/// let pol = 0.5_f32;
///
/// // lerp(x, y, pol)
/// let lerp = x.lerp(y, pol);
///
/// assert!(lerp == 15.0_f32);
/// ```
#[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "lerp", issue = "71015")]
#[inline]
pub fn lerp(self, upper: f32, pol: f32) -> f32 {
unsafe { intrinsics::fmaf32(self, 1_f32 - pol, upper * pol) }
}
}

#[cfg(test)]
Expand Down
24 changes: 24 additions & 0 deletions src/libstd/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,30 @@ impl f64 {
x
}

/// Computes `self + step(upper - pol)` the linear interpolation between
/// `self` and `upper` for the parameter `pol` (or extrapolation,
/// when `pol` is outside the range [0,1]).
///
/// # Examples
///
/// ```
/// #![feature(lerp)]
/// let x = 10.0_f64;
/// let y = 20.0_f64;
/// let pol = 0.5_f64;
///
/// // lerp(x, y, pol)
/// let lerp = x.lerp(y, pol);
///
/// assert!(lerp == 15.0_f64);
/// ```
#[must_use = "method returns a new number and does not mutate the original value"]
#[unstable(feature = "lerp", issue = "71015")]
#[inline]
pub fn lerp(self, upper: f64, pol: f64) -> f64 {
unsafe { intrinsics::fmaf64(self, 1_f64 - pol, upper * pol) }
}

// Solaris/Illumos requires a wrapper around log, log2, and log10 functions
// because of their non-standard behavior (e.g., log(-n) returns -Inf instead
// of expected NaN).
Expand Down
1 change: 1 addition & 0 deletions src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@
#![feature(cfg_target_thread_local)]
#![feature(char_error_internals)]
#![feature(clamp)]
#![feature(lerp)]
#![feature(concat_idents)]
Comment on lines 249 to 251
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be put in sorted order per the NB at the top and bottom of this feature list.
abcdefghijklmnopqrstuvwxyz

#![feature(const_cstr_unchecked)]
#![feature(const_raw_ptr_deref)]
Expand Down