diff --git a/src/libstd/f32.rs b/src/libstd/f32.rs index 65273275a4006..0e81f723245d0 100644 --- a/src/libstd/f32.rs +++ b/src/libstd/f32.rs @@ -912,6 +912,30 @@ impl f32 { } 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_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)] diff --git a/src/libstd/f64.rs b/src/libstd/f64.rs index ff222fc8539d0..3da01f7192c02 100644 --- a/src/libstd/f64.rs +++ b/src/libstd/f64.rs @@ -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). diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index a9a519f0a3a71..5d7a91335cbe7 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -247,6 +247,7 @@ #![feature(cfg_target_thread_local)] #![feature(char_error_internals)] #![feature(clamp)] +#![feature(lerp)] #![feature(concat_idents)] #![feature(const_cstr_unchecked)] #![feature(const_raw_ptr_deref)]