Skip to content
Merged
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
27 changes: 27 additions & 0 deletions library/core/src/num/f128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1602,6 +1602,33 @@ impl f128 {
pub const fn algebraic_rem(self, rhs: f128) -> f128 {
intrinsics::frem_algebraic(self, rhs)
}

/// Returns `self` if the value is not NaN, otherwise returns `replacement`
/// if `self` is NaN.
///
/// # Examples
///
/// ```
/// #![feature(f128)]
/// #![feature(float_nan_to)]
/// # #[cfg(target_has_reliable_f128)] {
///
/// let n = f128::NAN;
/// let x = 2.0f128;
/// let y = f128::INFINITY;
///
/// assert_eq!(n.nan_to(0.0f128), 0.0f128);
/// assert_eq!(x.nan_to(0.0f128), 2.0f128);
/// assert_eq!(y.nan_to(0.0f128), f128::INFINITY);
/// # }
/// ```
#[must_use = "method returns a new float and does not mutate the original value"]
#[unstable(feature = "float_nan_to", issue = "161248")]
#[rustc_const_unstable(feature = "float_nan_to", issue = "161248")]
#[inline]
pub const fn nan_to(self, replacement: f128) -> f128 {
if self.is_nan() { replacement } else { self }
}
}

// Functions in this module fall into `core_float_math`
Expand Down
27 changes: 27 additions & 0 deletions library/core/src/num/f16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1588,6 +1588,33 @@ impl f16 {
pub const fn algebraic_rem(self, rhs: f16) -> f16 {
intrinsics::frem_algebraic(self, rhs)
}

/// Returns `self` if the value is not NaN, otherwise returns `replacement`
/// if `self` is NaN.
///
/// # Examples
///
/// ```
/// #![feature(f16)]
/// #![feature(float_nan_to)]
/// # #[cfg(target_has_reliable_f16)] {
///
/// let n = f16::NAN;
/// let x = 2.0f16;
/// let y = f16::INFINITY;
///
/// assert_eq!(n.nan_to(0.0f16), 0.0f16);
/// assert_eq!(x.nan_to(0.0f16), 2.0f16);
/// assert_eq!(y.nan_to(0.0f16), f16::INFINITY);
/// # }
/// ```
#[must_use = "method returns a new float and does not mutate the original value"]
#[unstable(feature = "float_nan_to", issue = "161248")]
#[rustc_const_unstable(feature = "float_nan_to", issue = "161248")]
#[inline]
pub const fn nan_to(self, replacement: f16) -> f16 {
if self.is_nan() { replacement } else { self }
}
}

// Functions in this module fall into `core_float_math`
Expand Down
24 changes: 24 additions & 0 deletions library/core/src/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1783,6 +1783,30 @@ impl f32 {
pub const fn algebraic_rem(self, rhs: f32) -> f32 {
intrinsics::frem_algebraic(self, rhs)
}

/// Returns `self` if the value is not NaN, otherwise returns `replacement`
/// if `self` is NaN.
///
/// # Examples
///
/// ```
/// #![feature(float_nan_to)]
///
/// let n = f32::NAN;
/// let x = 2.0f32;
/// let y = f32::INFINITY;
///
/// assert_eq!(n.nan_to(0.0f32), 0.0f32);
/// assert_eq!(x.nan_to(0.0f32), 2.0f32);
/// assert_eq!(y.nan_to(0.0f32), f32::INFINITY);
/// ```
#[must_use = "method returns a new float and does not mutate the original value"]
#[unstable(feature = "float_nan_to", issue = "161248")]
#[rustc_const_unstable(feature = "float_nan_to", issue = "161248")]
#[inline]
pub const fn nan_to(self, replacement: f32) -> f32 {
if self.is_nan() { replacement } else { self }
}
}

/// Experimental implementations of floating point functions in `core`.
Expand Down
24 changes: 24 additions & 0 deletions library/core/src/num/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1763,6 +1763,30 @@ impl f64 {
pub const fn algebraic_rem(self, rhs: f64) -> f64 {
intrinsics::frem_algebraic(self, rhs)
}

/// Returns `self` if the value is not NaN, otherwise returns `replacement`
/// if `self` is NaN.
///
/// # Examples
///
/// ```
/// #![feature(float_nan_to)]
///
/// let n = f64::NAN;
/// let x = 2.0f64;
/// let y = f64::INFINITY;
///
/// assert_eq!(n.nan_to(0.0f64), 0.0f64);
/// assert_eq!(x.nan_to(0.0f64), 2.0f64);
/// assert_eq!(y.nan_to(0.0f64), f64::INFINITY);
/// ```
#[must_use = "method returns a new float and does not mutate the original value"]
#[unstable(feature = "float_nan_to", issue = "161248")]
#[rustc_const_unstable(feature = "float_nan_to", issue = "161248")]
#[inline]
pub const fn nan_to(self, replacement: f64) -> f64 {
if self.is_nan() { replacement } else { self }
}
}

#[unstable(feature = "core_float_math", issue = "137578")]
Expand Down
Loading