]> git.proxmox.com Git - rustc.git/blob - src/vendor/num-traits/src/sign.rs
New upstream version 1.26.0+dfsg1
[rustc.git] / src / vendor / num-traits / src / sign.rs
1 use core::ops::Neg;
2 use core::num::Wrapping;
3
4 use Num;
5 use float::FloatCore;
6
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.
10 ///
11 /// For `f32` and `f64`, `NaN` will be returned if the number is `NaN`.
12 ///
13 /// For signed integers, `::MIN` will be returned if the number is `::MIN`.
14 fn abs(&self) -> Self;
15
16 /// The positive difference of two numbers.
17 ///
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;
21
22 /// Returns the sign of the number.
23 ///
24 /// For `f32` and `f64`:
25 ///
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`
29 ///
30 /// For signed integers:
31 ///
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;
36
37 /// Returns true if the number is positive and false if the number is zero or negative.
38 fn is_positive(&self) -> bool;
39
40 /// Returns true if the number is negative and false if the number is zero or positive.
41 fn is_negative(&self) -> bool;
42 }
43
44 macro_rules! signed_impl {
45 ($($t:ty)*) => ($(
46 impl Signed for $t {
47 #[inline]
48 fn abs(&self) -> $t {
49 if self.is_negative() { -*self } else { *self }
50 }
51
52 #[inline]
53 fn abs_sub(&self, other: &$t) -> $t {
54 if *self <= *other { 0 } else { *self - *other }
55 }
56
57 #[inline]
58 fn signum(&self) -> $t {
59 match *self {
60 n if n > 0 => 1,
61 0 => 0,
62 _ => -1,
63 }
64 }
65
66 #[inline]
67 fn is_positive(&self) -> bool { *self > 0 }
68
69 #[inline]
70 fn is_negative(&self) -> bool { *self < 0 }
71 }
72 )*)
73 }
74
75 signed_impl!(isize i8 i16 i32 i64);
76
77 impl<T: Signed> Signed for Wrapping<T> where Wrapping<T>: Num + Neg<Output=Wrapping<T>>
78 {
79 #[inline]
80 fn abs(&self) -> Self {
81 Wrapping(self.0.abs())
82 }
83
84 #[inline]
85 fn abs_sub(&self, other: &Self) -> Self {
86 Wrapping(self.0.abs_sub(&other.0))
87 }
88
89 #[inline]
90 fn signum(&self) -> Self {
91 Wrapping(self.0.signum())
92 }
93
94 #[inline]
95 fn is_positive(&self) -> bool { self.0.is_positive() }
96
97 #[inline]
98 fn is_negative(&self) -> bool { self.0.is_negative() }
99 }
100
101 macro_rules! signed_float_impl {
102 ($t:ty) => {
103 impl Signed for $t {
104 /// Computes the absolute value. Returns `NAN` if the number is `NAN`.
105 #[inline]
106 fn abs(&self) -> $t {
107 FloatCore::abs(*self)
108 }
109
110 /// The positive difference of two numbers. Returns `0.0` if the number is
111 /// less than or equal to `other`, otherwise the difference between`self`
112 /// and `other` is returned.
113 #[inline]
114 fn abs_sub(&self, other: &$t) -> $t {
115 if *self <= *other { 0. } else { *self - *other }
116 }
117
118 /// # Returns
119 ///
120 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
121 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
122 /// - `NAN` if the number is NaN
123 #[inline]
124 fn signum(&self) -> $t {
125 FloatCore::signum(*self)
126 }
127
128 /// Returns `true` if the number is positive, including `+0.0` and `INFINITY`
129 #[inline]
130 fn is_positive(&self) -> bool { FloatCore::is_sign_positive(*self) }
131
132 /// Returns `true` if the number is negative, including `-0.0` and `NEG_INFINITY`
133 #[inline]
134 fn is_negative(&self) -> bool { FloatCore::is_sign_negative(*self) }
135 }
136 }
137 }
138
139 signed_float_impl!(f32);
140 signed_float_impl!(f64);
141
142 /// Computes the absolute value.
143 ///
144 /// For `f32` and `f64`, `NaN` will be returned if the number is `NaN`
145 ///
146 /// For signed integers, `::MIN` will be returned if the number is `::MIN`.
147 #[inline(always)]
148 pub fn abs<T: Signed>(value: T) -> T {
149 value.abs()
150 }
151
152 /// The positive difference of two numbers.
153 ///
154 /// Returns zero if `x` is less than or equal to `y`, otherwise the difference
155 /// between `x` and `y` is returned.
156 #[inline(always)]
157 pub fn abs_sub<T: Signed>(x: T, y: T) -> T {
158 x.abs_sub(&y)
159 }
160
161 /// Returns the sign of the number.
162 ///
163 /// For `f32` and `f64`:
164 ///
165 /// * `1.0` if the number is positive, `+0.0` or `INFINITY`
166 /// * `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
167 /// * `NaN` if the number is `NaN`
168 ///
169 /// For signed integers:
170 ///
171 /// * `0` if the number is zero
172 /// * `1` if the number is positive
173 /// * `-1` if the number is negative
174 #[inline(always)] pub fn signum<T: Signed>(value: T) -> T { value.signum() }
175
176 /// A trait for values which cannot be negative
177 pub trait Unsigned: Num {}
178
179 macro_rules! empty_trait_impl {
180 ($name:ident for $($t:ty)*) => ($(
181 impl $name for $t {}
182 )*)
183 }
184
185 empty_trait_impl!(Unsigned for usize u8 u16 u32 u64);
186
187 impl<T: Unsigned> Unsigned for Wrapping<T> where Wrapping<T>: Num {}
188
189 #[test]
190 fn unsigned_wrapping_is_unsigned() {
191 fn require_unsigned<T: Unsigned>(_: &T) {}
192 require_unsigned(&Wrapping(42_u32));
193 }
194 /*
195 // Commenting this out since it doesn't compile on Rust 1.8,
196 // because on this version Wrapping doesn't implement Neg and therefore can't
197 // implement Signed.
198 #[test]
199 fn signed_wrapping_is_signed() {
200 fn require_signed<T: Signed>(_: &T) {}
201 require_signed(&Wrapping(-42));
202 }
203 */