File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -558,4 +558,15 @@ mod tests {
558
558
val & !( 0xff << ( byte * 8 ) )
559
559
}
560
560
}
561
+
562
+ #[ test]
563
+ fn test_float_hashes_differ ( ) {
564
+ assert ! ( 0.0 . hash( ) != 1.0 . hash( ) ) ;
565
+ assert ! ( 1.0 . hash( ) != ( -1.0 ) . hash( ) ) ;
566
+ }
567
+
568
+ #[ test]
569
+ fn test_float_hashes_of_zero ( ) {
570
+ assert_eq ! ( 0.0 . hash( ) , ( -0.0 ) . hash( ) ) ;
571
+ }
561
572
}
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ The `ToBytes` and `IterBytes` traits
14
14
15
15
*/
16
16
17
+ use cast;
17
18
use io;
18
19
use io:: Writer ;
19
20
use option:: { None , Option , Some } ;
@@ -190,6 +191,35 @@ impl IterBytes for int {
190
191
}
191
192
}
192
193
194
+ impl IterBytes for float {
195
+ #[ inline( always) ]
196
+ fn iter_bytes ( & self , lsb0 : bool , f : Cb ) -> bool {
197
+ ( * self as f64 ) . iter_bytes ( lsb0, f)
198
+ }
199
+ }
200
+
201
+ impl IterBytes for f32 {
202
+ #[ inline( always) ]
203
+ fn iter_bytes ( & self , lsb0 : bool , f : Cb ) -> bool {
204
+ let i: u32 = unsafe {
205
+ // 0.0 == -0.0 so they should also have the same hashcode
206
+ cast:: transmute ( if * self == -0.0 { 0.0 } else { * self } )
207
+ } ;
208
+ i. iter_bytes ( lsb0, f)
209
+ }
210
+ }
211
+
212
+ impl IterBytes for f64 {
213
+ #[ inline( always) ]
214
+ fn iter_bytes ( & self , lsb0 : bool , f : Cb ) -> bool {
215
+ let i: u64 = unsafe {
216
+ // 0.0 == -0.0 so they should also have the same hashcode
217
+ cast:: transmute ( if * self == -0.0 { 0.0 } else { * self } )
218
+ } ;
219
+ i. iter_bytes ( lsb0, f)
220
+ }
221
+ }
222
+
193
223
impl < ' self , A : IterBytes > IterBytes for & ' self [ A ] {
194
224
#[ inline( always) ]
195
225
fn iter_bytes ( & self , lsb0 : bool , f : Cb ) -> bool {
You can’t perform that action at this time.
0 commit comments