We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
signum
Ord
1 parent bfffe40 commit fcbc12eCopy full SHA for fcbc12e
library/core/src/num/int_macros.rs
@@ -2574,12 +2574,13 @@ macro_rules! int_impl {
2574
#[must_use = "this returns the result of the operation, \
2575
without modifying the original"]
2576
#[inline(always)]
2577
+ #[rustc_allow_const_fn_unstable(const_cmp)]
2578
pub const fn signum(self) -> Self {
- match self {
2579
- n if n > 0 => 1,
2580
- 0 => 0,
2581
- _ => -1,
2582
- }
+ // Picking the right way to phrase this is complicated
+ // (<https://graphics.stanford.edu/~seander/bithacks.html#CopyIntegerSign>)
+ // so delegate it to `Ord` which is already producing -1/0/+1
+ // exactly like we need and can be the place to deal with the complexity.
2583
+ self.cmp(&0) as _
2584
}
2585
2586
/// Returns `true` if `self` is positive and `false` if the number is zero or
0 commit comments