6 /// Useful functions for signed numbers (i.e. numbers that can be negative).
7 pub trait Signed
: Sized
+ Num
+ Neg
<Output
= Self> {
8 /// Computes the absolute value.
10 /// For `f32` and `f64`, `NaN` will be returned if the number is `NaN`.
12 /// For signed integers, `::MIN` will be returned if the number is `::MIN`.
13 fn abs(&self) -> Self;
15 /// The positive difference of two numbers.
17 /// Returns `zero` if the number is less than or equal to `other`, otherwise the difference
18 /// between `self` and `other` is returned.
19 fn abs_sub(&self, other
: &Self) -> Self;
21 /// Returns the sign of the number.
23 /// For `f32` and `f64`:
25 /// * `1.0` if the number is positive, `+0.0` or `INFINITY`
26 /// * `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
27 /// * `NaN` if the number is `NaN`
29 /// For signed integers:
31 /// * `0` if the number is zero
32 /// * `1` if the number is positive
33 /// * `-1` if the number is negative
34 fn signum(&self) -> Self;
36 /// Returns true if the number is positive and false if the number is zero or negative.
37 fn is_positive(&self) -> bool
;
39 /// Returns true if the number is negative and false if the number is zero or positive.
40 fn is_negative(&self) -> bool
;
43 macro_rules
! signed_impl
{
48 if self.is_negative() { -*self }
else { *self }
52 fn abs_sub(&self, other
: &$t
) -> $t
{
53 if *self <= *other { 0 }
else { *self - *other }
57 fn signum(&self) -> $t
{
66 fn is_positive(&self) -> bool { *self > 0 }
69 fn is_negative(&self) -> bool { *self < 0 }
74 signed_impl
!(isize i8 i16 i32 i64);
76 macro_rules
! signed_float_impl
{
77 ($t
:ty
, $nan
:expr
, $inf
:expr
, $neg_inf
:expr
) => {
79 /// Computes the absolute value. Returns `NAN` if the number is `NAN`.
85 /// The positive difference of two numbers. Returns `0.0` if the number is
86 /// less than or equal to `other`, otherwise the difference between`self`
87 /// and `other` is returned.
90 fn abs_sub(&self, other
: &$t
) -> $t
{
91 <$t
>::abs_sub(*self, *other
)
96 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
97 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
98 /// - `NAN` if the number is NaN
100 fn signum(&self) -> $t
{
104 /// Returns `true` if the number is positive, including `+0.0` and `INFINITY`
106 fn is_positive(&self) -> bool { *self > 0.0 || (1.0 / *self) == $inf }
108 /// Returns `true` if the number is negative, including `-0.0` and `NEG_INFINITY`
110 fn is_negative(&self) -> bool { *self < 0.0 || (1.0 / *self) == $neg_inf }
115 signed_float_impl
!(f32, f32::NAN
, f32::INFINITY
, f32::NEG_INFINITY
);
116 signed_float_impl
!(f64, f64::NAN
, f64::INFINITY
, f64::NEG_INFINITY
);
118 /// Computes the absolute value.
120 /// For `f32` and `f64`, `NaN` will be returned if the number is `NaN`
122 /// For signed integers, `::MIN` will be returned if the number is `::MIN`.
124 pub fn abs
<T
: Signed
>(value
: T
) -> T
{
128 /// The positive difference of two numbers.
130 /// Returns zero if `x` is less than or equal to `y`, otherwise the difference
131 /// between `x` and `y` is returned.
133 pub fn abs_sub
<T
: Signed
>(x
: T
, y
: T
) -> T
{
137 /// Returns the sign of the number.
139 /// For `f32` and `f64`:
141 /// * `1.0` if the number is positive, `+0.0` or `INFINITY`
142 /// * `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
143 /// * `NaN` if the number is `NaN`
145 /// For signed integers:
147 /// * `0` if the number is zero
148 /// * `1` if the number is positive
149 /// * `-1` if the number is negative
150 #[inline(always)] pub fn signum<T: Signed>(value: T) -> T { value.signum() }
152 /// A trait for values which cannot be negative
153 pub trait Unsigned
: Num {}
155 macro_rules
! empty_trait_impl
{
156 ($name
:ident
for $
($t
:ty
)*) => ($
(
161 empty_trait_impl
!(Unsigned
for usize u8 u16 u32 u64);