@@ -72,8 +72,8 @@ def assertAlmostEqual(self, a, b):
7272 else :
7373 unittest .TestCase .assertAlmostEqual (self , a , b )
7474
75- def assertCloseAbs (self , x , y , eps = 1e-9 ):
76- """Return true iff floats x and y "are close"."""
75+ def assertClose (self , x , y , eps = 1e-9 ):
76+ """Return true iff complexes x and y "are close"."""
7777 # put the one with larger magnitude second
7878 if abs (x ) > abs (y ):
7979 x , y = y , x
@@ -82,26 +82,15 @@ def assertCloseAbs(self, x, y, eps=1e-9):
8282 if x == 0 :
8383 return abs (y ) < eps
8484 # check that relative difference < eps
85- self .assertTrue (abs ((x - y )/ y ) < eps )
86-
87- def assertClose (self , x , y , eps = 1e-9 ):
88- """Return true iff complexes x and y "are close"."""
89- self .assertCloseAbs (x .real , y .real , eps )
90- self .assertCloseAbs (x .imag , y .imag , eps )
85+ self .assertTrue (abs (x - y )/ abs (y ) < eps )
9186
9287 def check_div (self , x , y ):
9388 """Compute complex z=x*y, and check that z/x==y and z/y==x."""
9489 z = x * y
95- if x != 0 :
96- q = z / x
97- self .assertClose (q , y )
98- q = z .__truediv__ (x )
99- self .assertClose (q , y )
100- if y != 0 :
101- q = z / y
102- self .assertClose (q , x )
103- q = z .__truediv__ (y )
104- self .assertClose (q , x )
90+ if x :
91+ self .assertClose (z / x , y )
92+ if y :
93+ self .assertClose (z / y , x )
10594
10695 @unittest .expectedFailure # TODO: RUSTPYTHON; AssertionError: floats nan and inf are not identical
10796 def test_truediv (self ):
@@ -116,10 +105,20 @@ def test_truediv(self):
116105 self .check_div (complex (1e200 , 1e200 ), 1 + 0j )
117106 self .check_div (complex (1e-200 , 1e-200 ), 1 + 0j )
118107
108+ # Smith's algorithm has several sources of inaccuracy
109+ # for components of the result. In examples below,
110+ # it's cancellation of digits in computation of sum.
111+ self .check_div (1e-09 + 1j , 1 + 1j )
112+ self .check_div (8.289760544677449e-09 + 0.13257307440728516j ,
113+ 0.9059966714925808 + 0.5054864708672686j )
114+
119115 # Just for fun.
120116 for i in range (100 ):
121- self .check_div (complex (random (), random ()),
122- complex (random (), random ()))
117+ x = complex (random (), random ())
118+ y = complex (random (), random ())
119+ self .check_div (x , y )
120+ y = complex (1e10 * y .real , y .imag )
121+ self .check_div (x , y )
123122
124123 self .assertAlmostEqual (complex .__truediv__ (2 + 0j , 1 + 1j ), 1 - 1j )
125124 self .assertRaises (TypeError , operator .truediv , 1j , None )
@@ -457,7 +456,7 @@ def test_boolcontext(self):
457456 self .assertTrue (1j )
458457
459458 def test_conjugate (self ):
460- self .assertClose (complex (5.3 , 9.8 ).conjugate (), 5.3 - 9.8j )
459+ self .assertEqual (complex (5.3 , 9.8 ).conjugate (), 5.3 - 9.8j )
461460
462461 @unittest .expectedFailure # TODO: RUSTPYTHON; AssertionError: DeprecationWarning not triggered
463462 def test_constructor (self ):
0 commit comments