Skip to content

Commit 2ee6d9a

Browse files
committed
Linear interpolation (lerp) implementation
Enable feature flag for the use of it
1 parent 1406186 commit 2ee6d9a

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

src/libstd/f32.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,30 @@ impl f32 {
912912
}
913913
x
914914
}
915+
916+
/// Computes `self + step(upper - pol)` the linear interpolation between
917+
/// `self` and `upper` for the parameter `pol` (or extrapolation,
918+
/// when `pol` is outside the range [0,1]).
919+
///
920+
/// # Examples
921+
///
922+
/// ```
923+
/// #![feature(lerp)]
924+
/// let x = 10.0_f32;
925+
/// let y = 20.0_f32;
926+
/// let pol = 0.5_f32;
927+
///
928+
/// // lerp(x, y, pol)
929+
/// let lerp = x.lerp(y, pol);
930+
///
931+
/// assert!(lerp == 15.0_f32);
932+
/// ```
933+
#[must_use = "method returns a new number and does not mutate the original value"]
934+
#[unstable(feature = "lerp", issue = "71015")]
935+
#[inline]
936+
pub fn lerp(self, upper: f32, pol: f32) -> f32 {
937+
self * (1_f32 - pol) + upper * pol
938+
}
915939
}
916940

917941
#[cfg(test)]

src/libstd/f64.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,30 @@ impl f64 {
915915
x
916916
}
917917

918+
/// Computes `self + step(upper - pol)` the linear interpolation between
919+
/// `self` and `upper` for the parameter `pol` (or extrapolation,
920+
/// when `pol` is outside the range [0,1]).
921+
///
922+
/// # Examples
923+
///
924+
/// ```
925+
/// #![feature(lerp)]
926+
/// let x = 10.0_f64;
927+
/// let y = 20.0_f64;
928+
/// let pol = 0.5_f64;
929+
///
930+
/// // lerp(x, y, pol)
931+
/// let lerp = x.lerp(y, pol);
932+
///
933+
/// assert!(lerp == 15.0_f64);
934+
/// ```
935+
#[must_use = "method returns a new number and does not mutate the original value"]
936+
#[unstable(feature = "lerp", issue = "71015")]
937+
#[inline]
938+
pub fn lerp(self, upper: f64, pol: f64) -> f64 {
939+
self * (1_f64 - pol) + upper * pol
940+
}
941+
918942
// Solaris/Illumos requires a wrapper around log, log2, and log10 functions
919943
// because of their non-standard behavior (e.g., log(-n) returns -Inf instead
920944
// of expected NaN).

src/libstd/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@
247247
#![feature(cfg_target_thread_local)]
248248
#![feature(char_error_internals)]
249249
#![feature(clamp)]
250+
#![feature(lerp)]
250251
#![feature(concat_idents)]
251252
#![feature(const_cstr_unchecked)]
252253
#![feature(const_raw_ptr_deref)]

0 commit comments

Comments
 (0)