]>
git.proxmox.com Git - rustc.git/blob - vendor/num-traits/src/sign.rs
1 use core
::num
::Wrapping
;
4 use crate::float
::FloatCore
;
7 /// Useful functions for signed numbers (i.e. numbers that can be negative).
8 pub trait Signed
: Sized
+ Num
+ Neg
<Output
= Self> {
9 /// Computes the absolute value.
11 /// For `f32` and `f64`, `NaN` will be returned if the number is `NaN`.
13 /// For signed integers, `::MIN` will be returned if the number is `::MIN`.
14 fn abs(&self) -> Self;
16 /// The positive difference of two numbers.
18 /// Returns `zero` if the number is less than or equal to `other`, otherwise the difference
19 /// between `self` and `other` is returned.
20 fn abs_sub(&self, other
: &Self) -> Self;
22 /// Returns the sign of the number.
24 /// For `f32` and `f64`:
26 /// * `1.0` if the number is positive, `+0.0` or `INFINITY`
27 /// * `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
28 /// * `NaN` if the number is `NaN`
30 /// For signed integers:
32 /// * `0` if the number is zero
33 /// * `1` if the number is positive
34 /// * `-1` if the number is negative
35 fn signum(&self) -> Self;
37 /// Returns true if the number is positive and false if the number is zero or negative.
38 fn is_positive(&self) -> bool
;
40 /// Returns true if the number is negative and false if the number is zero or positive.
41 fn is_negative(&self) -> bool
;
44 macro_rules
! signed_impl
{
49 if self.is_negative() { -*self }
else { *self }
53 fn abs_sub(&self, other
: &$t
) -> $t
{
54 if *self <= *other { 0 }
else { *self - *other }
58 fn signum(&self) -> $t
{
67 fn is_positive(&self) -> bool { *self > 0 }
70 fn is_negative(&self) -> bool { *self < 0 }
75 signed_impl
!(isize i8 i16 i32 i64 i128
);
77 impl<T
: Signed
> Signed
for Wrapping
<T
>
79 Wrapping
<T
>: Num
+ Neg
<Output
= Wrapping
<T
>>,
82 fn abs(&self) -> Self {
83 Wrapping(self.0.abs())
87 fn abs_sub(&self, other
: &Self) -> Self {
88 Wrapping(self.0.abs_sub(&other
.0))
92 fn signum(&self) -> Self {
93 Wrapping(self.0.signum())
97 fn is_positive(&self) -> bool
{
102 fn is_negative(&self) -> bool
{
107 macro_rules
! signed_float_impl
{
110 /// Computes the absolute value. Returns `NAN` if the number is `NAN`.
112 fn abs(&self) -> $t
{
113 FloatCore
::abs(*self)
116 /// The positive difference of two numbers. Returns `0.0` if the number is
117 /// less than or equal to `other`, otherwise the difference between`self`
118 /// and `other` is returned.
120 fn abs_sub(&self, other
: &$t
) -> $t
{
130 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
131 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
132 /// - `NAN` if the number is NaN
134 fn signum(&self) -> $t
{
135 FloatCore
::signum(*self)
138 /// Returns `true` if the number is positive, including `+0.0` and `INFINITY`
140 fn is_positive(&self) -> bool
{
141 FloatCore
::is_sign_positive(*self)
144 /// Returns `true` if the number is negative, including `-0.0` and `NEG_INFINITY`
146 fn is_negative(&self) -> bool
{
147 FloatCore
::is_sign_negative(*self)
153 signed_float_impl
!(f32);
154 signed_float_impl
!(f64);
156 /// Computes the absolute value.
158 /// For `f32` and `f64`, `NaN` will be returned if the number is `NaN`
160 /// For signed integers, `::MIN` will be returned if the number is `::MIN`.
162 pub fn abs
<T
: Signed
>(value
: T
) -> T
{
166 /// The positive difference of two numbers.
168 /// Returns zero if `x` is less than or equal to `y`, otherwise the difference
169 /// between `x` and `y` is returned.
171 pub fn abs_sub
<T
: Signed
>(x
: T
, y
: T
) -> T
{
175 /// Returns the sign of the number.
177 /// For `f32` and `f64`:
179 /// * `1.0` if the number is positive, `+0.0` or `INFINITY`
180 /// * `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
181 /// * `NaN` if the number is `NaN`
183 /// For signed integers:
185 /// * `0` if the number is zero
186 /// * `1` if the number is positive
187 /// * `-1` if the number is negative
189 pub fn signum
<T
: Signed
>(value
: T
) -> T
{
193 /// A trait for values which cannot be negative
194 pub trait Unsigned
: Num {}
196 macro_rules
! empty_trait_impl
{
197 ($name
:ident
for $
($t
:ty
)*) => ($
(
202 empty_trait_impl
!(Unsigned
for usize u8 u16 u32 u64 u128
);
204 impl<T
: Unsigned
> Unsigned
for Wrapping
<T
> where Wrapping
<T
>: Num {}
207 fn unsigned_wrapping_is_unsigned() {
208 fn require_unsigned
<T
: Unsigned
>(_
: &T
) {}
209 require_unsigned(&Wrapping(42_u32));
213 fn signed_wrapping_is_signed() {
214 fn require_signed
<T
: Signed
>(_
: &T
) {}
215 require_signed(&Wrapping(-42));