12
12
//! Complex numbers.
13
13
14
14
use std:: fmt;
15
- use std:: num:: { Zero , One , ToStrRadix } ;
15
+ use std:: num:: { Zero , One , ToStrRadix } ;
16
16
17
17
// FIXME #1284: handle complex NaN & infinity etc. This
18
18
// probably doesn't map to C's _Complex correctly.
19
19
20
20
/// A complex number in Cartesian form.
21
- #[ deriving( PartialEq , Clone ) ]
21
+ #[ deriving( PartialEq , Clone , Hash ) ]
22
22
pub struct Complex < T > {
23
23
/// Real portion of the complex number
24
24
pub re : T ,
@@ -36,10 +36,8 @@ impl<T: Clone + Num> Complex<T> {
36
36
Complex { re : re, im : im }
37
37
}
38
38
39
- /**
40
- Returns the square of the norm (since `T` doesn't necessarily
41
- have a sqrt function), i.e. `re^2 + im^2`.
42
- */
39
+ /// Returns the square of the norm (since `T` doesn't necessarily
40
+ /// have a sqrt function), i.e. `re^2 + im^2`.
43
41
#[ inline]
44
42
pub fn norm_sqr ( & self ) -> T {
45
43
self . re * self . re + self . im * self . im
@@ -193,7 +191,8 @@ mod test {
193
191
#![ allow( non_uppercase_statics) ]
194
192
195
193
use super :: { Complex64 , Complex } ;
196
- use std:: num:: { Zero , One , Float } ;
194
+ use std:: num:: { Zero , One , Float } ;
195
+ use std:: hash:: hash;
197
196
198
197
pub static _0_0i : Complex64 = Complex { re : 0.0 , im : 0.0 } ;
199
198
pub static _1_0i : Complex64 = Complex { re : 1.0 , im : 0.0 } ;
@@ -367,4 +366,14 @@ mod test {
367
366
test ( -_neg1_1i, "1-1i" . to_string ( ) ) ;
368
367
test ( _05_05i, "0.5+0.5i" . to_string ( ) ) ;
369
368
}
369
+
370
+ #[ test]
371
+ fn test_hash ( ) {
372
+ let a = Complex :: new ( 0i32 , 0i32 ) ;
373
+ let b = Complex :: new ( 1i32 , 0i32 ) ;
374
+ let c = Complex :: new ( 0i32 , 1i32 ) ;
375
+ assert ! ( hash( & a) != hash( & b) ) ;
376
+ assert ! ( hash( & b) != hash( & c) ) ;
377
+ assert ! ( hash( & c) != hash( & a) ) ;
378
+ }
370
379
}
0 commit comments