Implement signum
with Ord
This commit is contained in:
parent
bfffe406fb
commit
fcbc12eae3
1 changed files with 6 additions and 5 deletions
|
@ -2574,12 +2574,13 @@ macro_rules! int_impl {
|
||||||
#[must_use = "this returns the result of the operation, \
|
#[must_use = "this returns the result of the operation, \
|
||||||
without modifying the original"]
|
without modifying the original"]
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
|
#[rustc_allow_const_fn_unstable(const_cmp)]
|
||||||
pub const fn signum(self) -> Self {
|
pub const fn signum(self) -> Self {
|
||||||
match self {
|
// Picking the right way to phrase this is complicated
|
||||||
n if n > 0 => 1,
|
// (<https://graphics.stanford.edu/~seander/bithacks.html#CopyIntegerSign>)
|
||||||
0 => 0,
|
// so delegate it to `Ord` which is already producing -1/0/+1
|
||||||
_ => -1,
|
// exactly like we need and can be the place to deal with the complexity.
|
||||||
}
|
self.cmp(&0) as _
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if `self` is positive and `false` if the number is zero or
|
/// Returns `true` if `self` is positive and `false` if the number is zero or
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue