@@ -79,6 +79,66 @@ fn canonicalize_signed_zero<T: FloatCore>(x: T) -> T {
79
79
#[ repr( transparent) ]
80
80
pub struct OrderedFloat < T > ( pub T ) ;
81
81
82
+ #[ cfg( feature = "num-cmp" ) ]
83
+ mod impl_num_cmp {
84
+ use super :: OrderedFloat ;
85
+ use core:: cmp:: Ordering ;
86
+ use num_cmp:: NumCmp ;
87
+ use num_traits:: float:: FloatCore ;
88
+
89
+ impl < T , U > NumCmp < U > for OrderedFloat < T >
90
+ where
91
+ T : FloatCore + NumCmp < U > ,
92
+ U : Copy ,
93
+ {
94
+ fn num_cmp ( self , other : U ) -> Option < Ordering > {
95
+ NumCmp :: num_cmp ( self . 0 , other)
96
+ }
97
+
98
+ fn num_eq ( self , other : U ) -> bool {
99
+ NumCmp :: num_eq ( self . 0 , other)
100
+ }
101
+
102
+ fn num_ne ( self , other : U ) -> bool {
103
+ NumCmp :: num_ne ( self . 0 , other)
104
+ }
105
+
106
+ fn num_lt ( self , other : U ) -> bool {
107
+ NumCmp :: num_lt ( self . 0 , other)
108
+ }
109
+
110
+ fn num_gt ( self , other : U ) -> bool {
111
+ NumCmp :: num_gt ( self . 0 , other)
112
+ }
113
+
114
+ fn num_le ( self , other : U ) -> bool {
115
+ NumCmp :: num_le ( self . 0 , other)
116
+ }
117
+
118
+ fn num_ge ( self , other : U ) -> bool {
119
+ NumCmp :: num_ge ( self . 0 , other)
120
+ }
121
+ }
122
+
123
+ #[ test]
124
+ pub fn test_num_cmp ( ) {
125
+ let f = OrderedFloat ( 1.0 ) ;
126
+
127
+ assert_eq ! ( NumCmp :: num_cmp( f, 1.0 ) , Some ( Ordering :: Equal ) ) ;
128
+ assert_eq ! ( NumCmp :: num_cmp( f, -1.0 ) , Some ( Ordering :: Greater ) ) ;
129
+ assert_eq ! ( NumCmp :: num_cmp( f, 2.0 ) , Some ( Ordering :: Less ) ) ;
130
+
131
+ assert ! ( NumCmp :: num_eq( f, 1 ) ) ;
132
+ assert ! ( NumCmp :: num_ne( f, -1 ) ) ;
133
+ assert ! ( NumCmp :: num_lt( f, 100 ) ) ;
134
+ assert ! ( NumCmp :: num_gt( f, 0 ) ) ;
135
+ assert ! ( NumCmp :: num_le( f, 1 ) ) ;
136
+ assert ! ( NumCmp :: num_le( f, 2 ) ) ;
137
+ assert ! ( NumCmp :: num_ge( f, 1 ) ) ;
138
+ assert ! ( NumCmp :: num_ge( f, -1 ) ) ;
139
+ }
140
+ }
141
+
82
142
impl < T : FloatCore > OrderedFloat < T > {
83
143
/// Get the value out.
84
144
#[ inline]
0 commit comments