Skip to content

Commit 4f5563d

Browse files
committed
Added abs_diff for integer types.
1 parent 497ee32 commit 4f5563d

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

library/core/src/num/int_macros.rs

+40
Original file line numberDiff line numberDiff line change
@@ -2227,6 +2227,46 @@ macro_rules! int_impl {
22272227
}
22282228
}
22292229

2230+
/// Computes the absolute difference between `self` and `other`.
2231+
///
2232+
/// This function always returns the correct answer without overflow or
2233+
/// panics by returning an unsigned integer.
2234+
///
2235+
/// # Examples
2236+
///
2237+
/// Basic usage:
2238+
///
2239+
/// ```
2240+
/// #![feature(int_abs_diff)]
2241+
#[doc = concat!("assert_eq!(100", stringify!($SelfT), ".abs_diff(80), 20", stringify!($UnsignedT), ");")]
2242+
#[doc = concat!("assert_eq!(100", stringify!($SelfT), ".abs_diff(110), 10", stringify!($UnsignedT), ");")]
2243+
#[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").abs_diff(80), 180", stringify!($UnsignedT), ");")]
2244+
#[doc = concat!("assert_eq!((-100", stringify!($SelfT), ").abs_diff(-120), 20", stringify!($UnsignedT), ");")]
2245+
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.abs_diff(", stringify!($SelfT), "::MAX), ", stringify!($UnsignedT), "::MAX);")]
2246+
/// ```
2247+
#[unstable(feature = "int_abs_diff", issue = "none")]
2248+
#[inline]
2249+
pub const fn abs_diff(self, other: Self) -> $UnsignedT {
2250+
if self < other {
2251+
// Converting a non-negative x from signed to unsigned by using
2252+
// `x as U` is left unchanged, but a negative x is converted
2253+
// to value x + 2^N. Thus if `s` and `o` are binary variables
2254+
// respectively indicating whether `self` and `other` are
2255+
// negative, we are computing the mathematical value:
2256+
//
2257+
// (other + o*2^N) - (self + s*2^N) mod 2^N
2258+
// other - self + (o-s)*2^N mod 2^N
2259+
// other - self mod 2^N
2260+
//
2261+
// Finally, taking the mod 2^N of the mathematical value of
2262+
// `other - self` does not change it as it already is
2263+
// in the range [0, 2^N).
2264+
(other as $UnsignedT).wrapping_sub(self as $UnsignedT)
2265+
} else {
2266+
(self as $UnsignedT).wrapping_sub(other as $UnsignedT)
2267+
}
2268+
}
2269+
22302270
/// Returns a number representing sign of `self`.
22312271
///
22322272
/// - `0` if the number is zero

library/core/src/num/uint_macros.rs

+21
Original file line numberDiff line numberDiff line change
@@ -1490,6 +1490,27 @@ macro_rules! uint_impl {
14901490
(c, b | d)
14911491
}
14921492

1493+
/// Computes the absolute difference between `self` and `other`.
1494+
///
1495+
/// # Examples
1496+
///
1497+
/// Basic usage:
1498+
///
1499+
/// ```
1500+
/// #![feature(int_abs_diff)]
1501+
#[doc = concat!("assert_eq!(100", stringify!($SelfT), ".abs_diff(80), 20", stringify!($SelfT), ");")]
1502+
#[doc = concat!("assert_eq!(100", stringify!($SelfT), ".abs_diff(110), 10", stringify!($SelfT), ");")]
1503+
/// ```
1504+
#[unstable(feature = "int_abs_diff", issue = "none")]
1505+
#[inline]
1506+
pub const fn abs_diff(self, other: Self) -> Self {
1507+
if self < other {
1508+
other - self
1509+
} else {
1510+
self - other
1511+
}
1512+
}
1513+
14931514
/// Calculates the multiplication of `self` and `rhs`.
14941515
///
14951516
/// Returns a tuple of the multiplication along with a boolean

0 commit comments

Comments
 (0)