From 9fe94bd995ab13afab7078a708b01f365740d2cd Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Sun, 9 Nov 2014 17:24:14 +1100 Subject: [PATCH] Move abs_sub to FloatMath This removes the need for libcore to depend on libm. `abs_sub` is not as useful for integers. --- src/libcore/num/mod.rs | 22 ---------------------- src/libcoretest/num/int_macros.rs | 8 -------- src/libstd/num/f32.rs | 5 +++++ src/libstd/num/f64.rs | 5 +++++ src/libstd/num/mod.rs | 14 +++++++++++++- 5 files changed, 23 insertions(+), 31 deletions(-) diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 61fabae4b8b..3763fb14b41 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -164,12 +164,6 @@ pub trait Signed: Num + Neg { /// For signed integers, `::MIN` will be returned if the number is `::MIN`. fn abs(self) -> Self; - /// The positive difference of two numbers. - /// - /// Returns `zero` if the number is less than or equal to `other`, otherwise the difference - /// between `self` and `other` is returned. - fn abs_sub(self, other: Self) -> Self; - /// Returns the sign of the number. /// /// For `f32` and `f64`: @@ -200,11 +194,6 @@ macro_rules! signed_impl( if self.is_negative() { -self } else { self } } - #[inline] - fn abs_sub(self, other: $T) -> $T { - if self <= other { 0 } else { self - other } - } - #[inline] fn signum(self) -> $T { match self { @@ -234,15 +223,6 @@ macro_rules! signed_float_impl( unsafe { $fabs(self) } } - /// The positive difference of two numbers. Returns `0.0` if the number is - /// less than or equal to `other`, otherwise the difference between`self` - /// and `other` is returned. - #[inline] - fn abs_sub(self, other: $T) -> $T { - extern { fn $fdim(a: $T, b: $T) -> $T; } - unsafe { $fdim(self, other) } - } - /// # Returns /// /// - `1.0` if the number is positive, `+0.0` or `INFINITY` @@ -1546,5 +1526,3 @@ pub trait Float: Signed + Primitive { #[deprecated = "Use `Signed::abs`"] pub fn abs(value: T) -> T { value.abs() } -#[deprecated = "Use `Signed::abs_sub`"] -pub fn abs_sub(x: T, y: T) -> T { x.abs_sub(y) } diff --git a/src/libcoretest/num/int_macros.rs b/src/libcoretest/num/int_macros.rs index 1b3dd8ffa5e..21b8f31dbf0 100644 --- a/src/libcoretest/num/int_macros.rs +++ b/src/libcoretest/num/int_macros.rs @@ -37,14 +37,6 @@ mod tests { assert!((-1 as $T).abs() == 1 as $T); } - #[test] - fn test_abs_sub() { - assert!((-1 as $T).abs_sub(1 as $T) == 0 as $T); - assert!((1 as $T).abs_sub(1 as $T) == 0 as $T); - assert!((1 as $T).abs_sub(0 as $T) == 1 as $T); - assert!((1 as $T).abs_sub(-1 as $T) == 2 as $T); - } - #[test] fn test_signum() { assert!((1 as $T).signum() == 1 as $T); diff --git a/src/libstd/num/f32.rs b/src/libstd/num/f32.rs index 175b1612b7e..6e2e8b8752f 100644 --- a/src/libstd/num/f32.rs +++ b/src/libstd/num/f32.rs @@ -108,6 +108,11 @@ impl FloatMath for f32 { unsafe { cmath::fminf(self, other) } } + #[inline] + fn abs_sub(self, other: f32) -> f32 { + unsafe { cmath::fdimf(self, other) } + } + #[inline] fn cbrt(self) -> f32 { unsafe { cmath::cbrtf(self) } diff --git a/src/libstd/num/f64.rs b/src/libstd/num/f64.rs index eb8e8db798c..591ca3486d2 100644 --- a/src/libstd/num/f64.rs +++ b/src/libstd/num/f64.rs @@ -116,6 +116,11 @@ impl FloatMath for f64 { unsafe { cmath::fmin(self, other) } } + #[inline] + fn abs_sub(self, other: f64) -> f64 { + unsafe { cmath::fdim(self, other) } + } + #[inline] fn cbrt(self) -> f64 { unsafe { cmath::cbrt(self) } diff --git a/src/libstd/num/mod.rs b/src/libstd/num/mod.rs index ffe162cbc64..3f8504f4553 100644 --- a/src/libstd/num/mod.rs +++ b/src/libstd/num/mod.rs @@ -21,7 +21,7 @@ use option::Option; #[cfg(test)] use fmt::Show; pub use core::num::{Num, div_rem, Zero, zero, One, one}; -pub use core::num::{Signed, abs, abs_sub, signum}; +pub use core::num::{Signed, abs, signum}; pub use core::num::{Unsigned, pow, Bounded}; pub use core::num::{Primitive, Int, Saturating}; pub use core::num::{CheckedAdd, CheckedSub, CheckedMul, CheckedDiv}; @@ -58,6 +58,11 @@ pub trait FloatMath: Float { /// Returns the minimum of the two numbers. fn min(self, other: Self) -> Self; + /// The positive difference of two numbers. Returns `0.0` if the number is + /// less than or equal to `other`, otherwise the difference between`self` + /// and `other` is returned. + fn abs_sub(self, other: Self) -> Self; + /// Take the cubic root of a number. fn cbrt(self) -> Self; /// Calculate the length of the hypotenuse of a right-angle triangle given @@ -122,6 +127,13 @@ pub fn from_str_radix(str: &str, radix: uint) -> Option { FromStrRadix::from_str_radix(str, radix) } +// DEPRECATED + +#[deprecated = "Use `FloatMath::abs_sub`"] +pub fn abs_sub(x: T, y: T) -> T { + x.abs_sub(y) +} + /// Helper function for testing numeric operations #[cfg(test)] pub fn test_num(ten: T, two: T) {