@@ -64,8 +64,8 @@ pub trait Signed: Neg<Self> {
64
64
fn is_negative ( self ) -> bool ;
65
65
}
66
66
67
- macro_rules! signed_impl (
68
- ( $( $ T: ty) * ) => ( $ (
67
+ macro_rules! signed_int_impl {
68
+ ( $T: ty) => {
69
69
impl Signed for $T {
70
70
#[ inline]
71
71
fn abs( self ) -> $T {
@@ -75,9 +75,9 @@ macro_rules! signed_impl(
75
75
#[ inline]
76
76
fn signum( self ) -> $T {
77
77
match self {
78
- n if n > 0 => 1 ,
79
- 0 => 0 ,
80
- _ => -1 ,
78
+ n if n > 0 => 1 ,
79
+ 0 => 0 ,
80
+ _ => -1 ,
81
81
}
82
82
}
83
83
@@ -87,13 +87,17 @@ macro_rules! signed_impl(
87
87
#[ inline]
88
88
fn is_negative( self ) -> bool { self < 0 }
89
89
}
90
- ) * )
91
- )
90
+ }
91
+ }
92
92
93
- signed_impl ! ( int i8 i16 i32 i64 )
93
+ signed_int_impl ! ( i8 )
94
+ signed_int_impl ! ( i16 )
95
+ signed_int_impl ! ( i32 )
96
+ signed_int_impl ! ( i64 )
97
+ signed_int_impl ! ( int)
94
98
95
- macro_rules! signed_float_impl(
96
- ( $T: ty, $nan : expr , $inf : expr , $neg_inf : expr , $ fabs: path, $fcopysign: path, $fdim : ident ) => {
99
+ macro_rules! signed_float_impl {
100
+ ( $T: ty, $fabs: path, $fcopysign: path) => {
97
101
impl Signed for $T {
98
102
/// Computes the absolute value. Returns `NAN` if the number is `NAN`.
99
103
#[ inline]
@@ -103,46 +107,42 @@ macro_rules! signed_float_impl(
103
107
104
108
/// # Returns
105
109
///
106
- /// - `1.0` if the number is positive, `+0.0` or `INFINITY `
107
- /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY `
108
- /// - `NAN ` if the number is NaN
110
+ /// - `1.0` if the number is positive, `+0.0` or `Float::infinity() `
111
+ /// - `-1.0` if the number is negative, `-0.0` or `Float::neg_infinity() `
112
+ /// - `Float::nan() ` if the number is `Float::nan()`
109
113
#[ inline]
110
114
fn signum( self ) -> $T {
111
- if self != self { $nan } else {
115
+ if self . is_nan( ) {
116
+ Float :: nan( )
117
+ } else {
112
118
unsafe { $fcopysign( 1.0 , self ) }
113
119
}
114
120
}
115
121
116
- /// Returns `true` if the number is positive, including `+0.0` and `INFINITY`
122
+ /// Returns `true` if the number is positive, including `+0.0` and
123
+ /// `Float::infinity()`.
117
124
#[ inline]
118
- fn is_positive( self ) -> bool { self > 0.0 || ( 1.0 / self ) == $inf }
125
+ fn is_positive( self ) -> bool {
126
+ self > 0.0 || ( 1.0 / self ) == Float :: infinity( )
127
+ }
119
128
120
- /// Returns `true` if the number is negative, including `-0.0` and `NEG_INFINITY`
129
+ /// Returns `true` if the number is negative, including `-0.0` and
130
+ /// `Float::neg_infinity()`.
121
131
#[ inline]
122
- fn is_negative( self ) -> bool { self < 0.0 || ( 1.0 / self ) == $neg_inf }
132
+ fn is_negative( self ) -> bool {
133
+ self < 0.0 || ( 1.0 / self ) == Float :: neg_infinity( )
134
+ }
123
135
}
124
- }
125
- )
136
+ } ;
137
+ }
126
138
127
- signed_float_impl ! ( f32 , f32 :: NAN , f32 :: INFINITY , f32 :: NEG_INFINITY ,
128
- intrinsics:: fabsf32, intrinsics:: copysignf32, fdimf)
129
- signed_float_impl ! ( f64 , f64 :: NAN , f64 :: INFINITY , f64 :: NEG_INFINITY ,
130
- intrinsics:: fabsf64, intrinsics:: copysignf64, fdim)
139
+ signed_float_impl ! ( f32 ,
140
+ intrinsics:: fabsf32,
141
+ intrinsics:: copysignf32)
131
142
132
- /// Returns the sign of the number.
133
- ///
134
- /// For `f32` and `f64`:
135
- ///
136
- /// * `1.0` if the number is positive, `+0.0` or `INFINITY`
137
- /// * `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
138
- /// * `NaN` if the number is `NaN`
139
- ///
140
- /// For signed integers:
141
- ///
142
- /// * `0` if the number is zero
143
- /// * `1` if the number is positive
144
- /// * `-1` if the number is negative
145
- #[ inline( always) ] pub fn signum < T : Signed > ( value : T ) -> T { value. signum ( ) }
143
+ signed_float_impl ! ( f64 ,
144
+ intrinsics:: fabsf64,
145
+ intrinsics:: copysignf64)
146
146
147
147
/// Raises a value to the power of exp, using exponentiation by squaring.
148
148
///
@@ -1481,6 +1481,10 @@ one_impl!(f64, 1.0f64)
1481
1481
pub fn abs < T : Signed > ( value : T ) -> T {
1482
1482
value. abs ( )
1483
1483
}
1484
+ #[ deprecated = "Use `Signed::signum`" ]
1485
+ pub fn signum < T : Signed > ( value : T ) -> T {
1486
+ value. signum ( )
1487
+ }
1484
1488
#[ deprecated = "Use `UnsignedInt::next_power_of_two`" ]
1485
1489
pub fn next_power_of_two < T : UnsignedInt > ( n : T ) -> T {
1486
1490
n. next_power_of_two ( )
0 commit comments