Implement `Float::{min, max}` for `no_std`

This commit is contained in:
Vinzent Steinberg 2017-06-02 11:40:30 +02:00
parent 6253669ef4
commit 169105bb84
1 changed files with 24 additions and 4 deletions

View File

@ -682,6 +682,8 @@ pub trait Float
/// Returns the maximum of the two numbers. /// Returns the maximum of the two numbers.
/// ///
/// If one of the arguments is NaN, then the other argument is returned.
///
/// ``` /// ```
/// use num_traits::Float; /// use num_traits::Float;
/// ///
@ -690,11 +692,21 @@ pub trait Float
/// ///
/// assert_eq!(x.max(y), y); /// assert_eq!(x.max(y), y);
/// ``` /// ```
#[cfg(feature = "std")] #[inline]
fn max(self, other: Self) -> Self; fn max(self, other: Self) -> Self {
if self.is_nan() {
return other;
}
if other.is_nan() {
return self;
}
if self > other { self } else { other }
}
/// Returns the minimum of the two numbers. /// Returns the minimum of the two numbers.
/// ///
/// If one of the arguments is NaN, then the other argument is returned.
///
/// ``` /// ```
/// use num_traits::Float; /// use num_traits::Float;
/// ///
@ -703,8 +715,16 @@ pub trait Float
/// ///
/// assert_eq!(x.min(y), x); /// assert_eq!(x.min(y), x);
/// ``` /// ```
#[cfg(feature = "std")] #[inline]
fn min(self, other: Self) -> Self; fn min(self, other: Self) -> Self {
if self.is_nan() {
return other;
}
if other.is_nan() {
return self;
}
if self < other { self } else { other }
}
/// The positive difference of two numbers. /// The positive difference of two numbers.
/// ///