1- use core:: ops;
1+ use core:: { fmt , ops} ;
22
33use super :: int_traits:: { Int , MinInt } ;
44
55/// Trait for some basic operations on floats
66#[ allow( dead_code) ]
77pub trait Float :
88 Copy
9- + core:: fmt:: Debug
9+ + fmt:: Debug
10+ + fmt:: Display
1011 + PartialEq
1112 + PartialOrd
1213 + ops:: AddAssign
@@ -17,16 +18,17 @@ pub trait Float:
1718 + ops:: Rem < Output = Self >
1819{
1920 /// A uint of the same width as the float
20- type Int : Int < OtherSign = Self :: SignedInt , UnsignedInt = Self :: Int > ;
21+ type Int : Int < OtherSign = Self :: SignedInt , Unsigned = Self :: Int > ;
2122
2223 /// A int of the same width as the float
23- type SignedInt : Int + MinInt < OtherSign = Self :: Int , UnsignedInt = Self :: Int > ;
24+ type SignedInt : Int + MinInt < OtherSign = Self :: Int , Unsigned = Self :: Int > ;
2425
2526 /// An int capable of containing the exponent bits plus a sign bit. This is signed.
2627 type ExpInt : Int ;
2728
2829 const ZERO : Self ;
2930 const ONE : Self ;
31+ const NEG_ONE : Self ;
3032 const INFINITY : Self ;
3133 const NEG_INFINITY : Self ;
3234 const NAN : Self ;
@@ -69,9 +71,18 @@ pub trait Float:
6971 /// compared.
7072 fn eq_repr ( self , rhs : Self ) -> bool ;
7173
72- /// Returns true if the sign is negative
74+ /// Returns true if the value is NaN.
75+ fn is_nan ( self ) -> bool ;
76+
77+ /// Returns true if the value is +inf or -inf.
78+ fn is_infinite ( self ) -> bool ;
79+
80+ /// Returns true if the sign is negative.
7381 fn is_sign_negative ( self ) -> bool ;
7482
83+ /// Returns if `self` is subnormal
84+ fn is_subnormal ( self ) -> bool ;
85+
7586 /// Returns the exponent, not adjusting for bias.
7687 fn exp ( self ) -> Self :: ExpInt ;
7788
@@ -95,8 +106,11 @@ pub trait Float:
95106 /// Returns (normalized exponent, normalized significand)
96107 fn normalize ( significand : Self :: Int ) -> ( i32 , Self :: Int ) ;
97108
98- /// Returns if `self` is subnormal
99- fn is_subnormal ( self ) -> bool ;
109+ /// Returns a number composed of the magnitude of self and the sign of sign.
110+ fn copysign ( self , other : Self ) -> Self ;
111+
112+ /// Returns a number that represents the sign of self.
113+ fn signum ( self ) -> Self ;
100114}
101115
102116macro_rules! float_impl {
@@ -108,6 +122,7 @@ macro_rules! float_impl {
108122
109123 const ZERO : Self = 0.0 ;
110124 const ONE : Self = 1.0 ;
125+ const NEG_ONE : Self = -1.0 ;
111126 const INFINITY : Self = Self :: INFINITY ;
112127 const NEG_INFINITY : Self = Self :: NEG_INFINITY ;
113128 const NAN : Self = Self :: NAN ;
@@ -136,9 +151,18 @@ macro_rules! float_impl {
136151 }
137152 if is_nan( self ) && is_nan( rhs) { true } else { self . to_bits( ) == rhs. to_bits( ) }
138153 }
154+ fn is_nan( self ) -> bool {
155+ self . is_nan( )
156+ }
157+ fn is_infinite( self ) -> bool {
158+ self . is_infinite( )
159+ }
139160 fn is_sign_negative( self ) -> bool {
140161 self . is_sign_negative( )
141162 }
163+ fn is_subnormal( self ) -> bool {
164+ ( self . to_bits( ) & Self :: EXP_MASK ) == Self :: Int :: ZERO
165+ }
142166 fn exp( self ) -> Self :: ExpInt {
143167 ( ( self . to_bits( ) & Self :: EXP_MASK ) >> Self :: SIG_BITS ) as Self :: ExpInt
144168 }
@@ -162,8 +186,16 @@ macro_rules! float_impl {
162186 let shift = significand. leading_zeros( ) . wrapping_sub( Self :: EXP_BITS ) ;
163187 ( 1i32 . wrapping_sub( shift as i32 ) , significand << shift as Self :: Int )
164188 }
165- fn is_subnormal( self ) -> bool {
166- ( self . to_bits( ) & Self :: EXP_MASK ) == Self :: Int :: ZERO
189+ fn copysign( self , other: Self ) -> Self {
190+ let mut x = self . to_bits( ) ;
191+ let y = other. to_bits( ) ;
192+ x &= !Self :: SIGN_MASK ;
193+ x |= y & Self :: SIGN_MASK ;
194+ Self :: from_bits( x)
195+ }
196+
197+ fn signum( self ) -> Self {
198+ if self . is_nan( ) { self } else { Self :: ONE . copysign( self ) }
167199 }
168200 }
169201 } ;
0 commit comments