From 169105bb84267182d9bbf2385a30e0530fba69f1 Mon Sep 17 00:00:00 2001 From: Vinzent Steinberg Date: Fri, 2 Jun 2017 11:40:30 +0200 Subject: [PATCH] Implement `Float::{min, max}` for `no_std` --- traits/src/float.rs | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/traits/src/float.rs b/traits/src/float.rs index 4fa7fc6..9fbe635 100644 --- a/traits/src/float.rs +++ b/traits/src/float.rs @@ -682,6 +682,8 @@ pub trait Float /// Returns the maximum of the two numbers. /// + /// If one of the arguments is NaN, then the other argument is returned. + /// /// ``` /// use num_traits::Float; /// @@ -690,11 +692,21 @@ pub trait Float /// /// assert_eq!(x.max(y), y); /// ``` - #[cfg(feature = "std")] - fn max(self, other: Self) -> Self; + #[inline] + 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. /// + /// If one of the arguments is NaN, then the other argument is returned. + /// /// ``` /// use num_traits::Float; /// @@ -703,8 +715,16 @@ pub trait Float /// /// assert_eq!(x.min(y), x); /// ``` - #[cfg(feature = "std")] - fn min(self, other: Self) -> Self; + #[inline] + 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. ///