diff options
| author | Daniel Micay <danielmicay@gmail.com> | 2014-03-04 19:54:35 -0500 |
|---|---|---|
| committer | Daniel Micay <danielmicay@gmail.com> | 2014-03-05 11:20:50 -0500 |
| commit | 2760974ec05966c811f1ee6f85bf59b13f28b009 (patch) | |
| tree | 68d84ba69516afbecb642ad7cb9074292df3dbbd | |
| parent | 6e7f170fedd3c526a643c0b2d13863acd982be02 (diff) | |
add correct floating point `min` and `max` methods.
The `std::cmp` functions are not correct for floating point types. `min(NaN, 2.0)` and `min(2.0, NaN)` return different values, because these functions assume a total order. Floating point types need special `min`, `max` and `clamp` functions.
| -rw-r--r-- | src/libstd/num/f32.rs | 11 | ||||
| -rw-r--r-- | src/libstd/num/f64.rs | 12 | ||||
| -rw-r--r-- | src/libstd/num/mod.rs | 3 | ||||
| -rw-r--r-- | src/libtest/lib.rs | 2 |
4 files changed, 25 insertions, 3 deletions
diff --git a/src/libstd/num/f32.rs b/src/libstd/num/f32.rs index a686edef99e..fb56444d265 100644 --- a/src/libstd/num/f32.rs +++ b/src/libstd/num/f32.rs @@ -9,6 +9,7 @@ // except according to those terms. //! Operations and constants for 32-bits floats (`f32` type) + #[allow(missing_doc)]; use prelude::*; @@ -312,6 +313,16 @@ impl Primitive for f32 {} impl Float for f32 { #[inline] + fn max(self, other: f32) -> f32 { + unsafe { cmath::c_float::fmax(self, other) } + } + + #[inline] + fn min(self, other: f32) -> f32 { + unsafe { cmath::c_float::fmin(self, other) } + } + + #[inline] fn nan() -> f32 { 0.0 / 0.0 } #[inline] diff --git a/src/libstd/num/f64.rs b/src/libstd/num/f64.rs index 026bd651e0e..7889041659a 100644 --- a/src/libstd/num/f64.rs +++ b/src/libstd/num/f64.rs @@ -23,8 +23,6 @@ use num::{Zero, One, Bounded, strconv}; use num; use intrinsics; -pub use cmp::{min, max}; - macro_rules! delegate( ( $( @@ -314,6 +312,16 @@ impl Primitive for f64 {} impl Float for f64 { #[inline] + fn max(self, other: f64) -> f64 { + unsafe { cmath::c_double::fmax(self, other) } + } + + #[inline] + fn min(self, other: f64) -> f64 { + unsafe { cmath::c_double::fmin(self, other) } + } + + #[inline] fn nan() -> f64 { 0.0 / 0.0 } #[inline] diff --git a/src/libstd/num/mod.rs b/src/libstd/num/mod.rs index 2051eeef60c..2f377a52ef8 100644 --- a/src/libstd/num/mod.rs +++ b/src/libstd/num/mod.rs @@ -313,6 +313,9 @@ pub enum FPCategory { pub trait Float: Signed + Round + Primitive { + fn max(self, other: Self) -> Self; + fn min(self, other: Self) -> Self; + // FIXME (#5527): These should be associated constants fn nan() -> Self; fn infinity() -> Self; diff --git a/src/libtest/lib.rs b/src/libtest/lib.rs index 0e062003053..9ebd7d72c91 100644 --- a/src/libtest/lib.rs +++ b/src/libtest/lib.rs @@ -1063,7 +1063,7 @@ impl MetricMap { Some(v) => { let delta = v.value - vold.value; let noise = match noise_pct { - None => f64::max(vold.noise.abs(), v.noise.abs()), + None => vold.noise.abs().max(v.noise.abs()), Some(pct) => vold.value * pct / 100.0 }; if delta.abs() <= noise { |
