1212//! Complex numbers.
1313
1414use std:: fmt;
15- use std:: num:: { Zero , One , ToStrRadix } ;
15+ use std:: num:: { Zero , One , ToStrRadix } ;
1616
1717// FIXME #1284: handle complex NaN & infinity etc. This
1818// probably doesn't map to C's _Complex correctly.
1919
2020/// A complex number in Cartesian form.
21- #[ deriving( PartialEq , Clone ) ]
21+ #[ deriving( PartialEq , Clone , Hash ) ]
2222pub struct Complex < T > {
2323 /// Real portion of the complex number
2424 pub re : T ,
@@ -36,10 +36,8 @@ impl<T: Clone + Num> Complex<T> {
3636 Complex { re : re, im : im }
3737 }
3838
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`.
4341 #[ inline]
4442 pub fn norm_sqr ( & self ) -> T {
4543 self . re * self . re + self . im * self . im
@@ -193,7 +191,8 @@ mod test {
193191 #![ allow( non_uppercase_statics) ]
194192
195193 use super :: { Complex64 , Complex } ;
196- use std:: num:: { Zero , One , Float } ;
194+ use std:: num:: { Zero , One , Float } ;
195+ use std:: hash:: hash;
197196
198197 pub static _0_0i : Complex64 = Complex { re : 0.0 , im : 0.0 } ;
199198 pub static _1_0i : Complex64 = Complex { re : 1.0 , im : 0.0 } ;
@@ -367,4 +366,14 @@ mod test {
367366 test ( -_neg1_1i, "1-1i" . to_string ( ) ) ;
368367 test ( _05_05i, "0.5+0.5i" . to_string ( ) ) ;
369368 }
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+ }
370379}
0 commit comments