Skip to content

Commit 9fe94bd

Browse files
committed
Move abs_sub to FloatMath
This removes the need for libcore to depend on libm. `abs_sub` is not as useful for integers.
1 parent e6db701 commit 9fe94bd

File tree

5 files changed

+23
-31
lines changed

5 files changed

+23
-31
lines changed

src/libcore/num/mod.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,6 @@ pub trait Signed: Num + Neg<Self> {
164164
/// For signed integers, `::MIN` will be returned if the number is `::MIN`.
165165
fn abs(self) -> Self;
166166

167-
/// The positive difference of two numbers.
168-
///
169-
/// Returns `zero` if the number is less than or equal to `other`, otherwise the difference
170-
/// between `self` and `other` is returned.
171-
fn abs_sub(self, other: Self) -> Self;
172-
173167
/// Returns the sign of the number.
174168
///
175169
/// For `f32` and `f64`:
@@ -200,11 +194,6 @@ macro_rules! signed_impl(
200194
if self.is_negative() { -self } else { self }
201195
}
202196

203-
#[inline]
204-
fn abs_sub(self, other: $T) -> $T {
205-
if self <= other { 0 } else { self - other }
206-
}
207-
208197
#[inline]
209198
fn signum(self) -> $T {
210199
match self {
@@ -234,15 +223,6 @@ macro_rules! signed_float_impl(
234223
unsafe { $fabs(self) }
235224
}
236225

237-
/// The positive difference of two numbers. Returns `0.0` if the number is
238-
/// less than or equal to `other`, otherwise the difference between`self`
239-
/// and `other` is returned.
240-
#[inline]
241-
fn abs_sub(self, other: $T) -> $T {
242-
extern { fn $fdim(a: $T, b: $T) -> $T; }
243-
unsafe { $fdim(self, other) }
244-
}
245-
246226
/// # Returns
247227
///
248228
/// - `1.0` if the number is positive, `+0.0` or `INFINITY`
@@ -1546,5 +1526,3 @@ pub trait Float: Signed + Primitive {
15461526

15471527
#[deprecated = "Use `Signed::abs`"]
15481528
pub fn abs<T: Signed>(value: T) -> T { value.abs() }
1549-
#[deprecated = "Use `Signed::abs_sub`"]
1550-
pub fn abs_sub<T: Signed>(x: T, y: T) -> T { x.abs_sub(y) }

src/libcoretest/num/int_macros.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,6 @@ mod tests {
3737
assert!((-1 as $T).abs() == 1 as $T);
3838
}
3939

40-
#[test]
41-
fn test_abs_sub() {
42-
assert!((-1 as $T).abs_sub(1 as $T) == 0 as $T);
43-
assert!((1 as $T).abs_sub(1 as $T) == 0 as $T);
44-
assert!((1 as $T).abs_sub(0 as $T) == 1 as $T);
45-
assert!((1 as $T).abs_sub(-1 as $T) == 2 as $T);
46-
}
47-
4840
#[test]
4941
fn test_signum() {
5042
assert!((1 as $T).signum() == 1 as $T);

src/libstd/num/f32.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ impl FloatMath for f32 {
108108
unsafe { cmath::fminf(self, other) }
109109
}
110110

111+
#[inline]
112+
fn abs_sub(self, other: f32) -> f32 {
113+
unsafe { cmath::fdimf(self, other) }
114+
}
115+
111116
#[inline]
112117
fn cbrt(self) -> f32 {
113118
unsafe { cmath::cbrtf(self) }

src/libstd/num/f64.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ impl FloatMath for f64 {
116116
unsafe { cmath::fmin(self, other) }
117117
}
118118

119+
#[inline]
120+
fn abs_sub(self, other: f64) -> f64 {
121+
unsafe { cmath::fdim(self, other) }
122+
}
123+
119124
#[inline]
120125
fn cbrt(self) -> f64 {
121126
unsafe { cmath::cbrt(self) }

src/libstd/num/mod.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use option::Option;
2121
#[cfg(test)] use fmt::Show;
2222

2323
pub use core::num::{Num, div_rem, Zero, zero, One, one};
24-
pub use core::num::{Signed, abs, abs_sub, signum};
24+
pub use core::num::{Signed, abs, signum};
2525
pub use core::num::{Unsigned, pow, Bounded};
2626
pub use core::num::{Primitive, Int, Saturating};
2727
pub use core::num::{CheckedAdd, CheckedSub, CheckedMul, CheckedDiv};
@@ -58,6 +58,11 @@ pub trait FloatMath: Float {
5858
/// Returns the minimum of the two numbers.
5959
fn min(self, other: Self) -> Self;
6060

61+
/// The positive difference of two numbers. Returns `0.0` if the number is
62+
/// less than or equal to `other`, otherwise the difference between`self`
63+
/// and `other` is returned.
64+
fn abs_sub(self, other: Self) -> Self;
65+
6166
/// Take the cubic root of a number.
6267
fn cbrt(self) -> Self;
6368
/// Calculate the length of the hypotenuse of a right-angle triangle given
@@ -122,6 +127,13 @@ pub fn from_str_radix<T: FromStrRadix>(str: &str, radix: uint) -> Option<T> {
122127
FromStrRadix::from_str_radix(str, radix)
123128
}
124129

130+
// DEPRECATED
131+
132+
#[deprecated = "Use `FloatMath::abs_sub`"]
133+
pub fn abs_sub<T: FloatMath>(x: T, y: T) -> T {
134+
x.abs_sub(y)
135+
}
136+
125137
/// Helper function for testing numeric operations
126138
#[cfg(test)]
127139
pub fn test_num<T:Num + NumCast + Show>(ten: T, two: T) {

0 commit comments

Comments
 (0)