Skip to content

Commit 5de71ac

Browse files
committed
gh-144173: fix flaky test_complex.test_truediv()
Previously, component-wise relative error bound was tested. However, such bound can't exist already for complex multiplication as one can be used to perform subtraction of floating-point numbers, e.g. x and y for z0=1+1j and z1=x+yj. ```pycon >>> x, y = 1e-9+1j, 1+1j >>> a = x*y*y.conjugate()/2;a (1.0000000272292198e-09+1j) >>> b = x*(y*y.conjugate()/2);b (1e-09+1j) >>> b == x True >>> (a.real-b.real)/math.ulp(b.real) 131672427.0 ```
1 parent 96e4cd6 commit 5de71ac

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

Lib/test/test_complex.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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
def test_truediv(self):
10796
simple_real = [float(i) for i in range(-5, 6)]
@@ -115,10 +104,20 @@ def test_truediv(self):
115104
self.check_div(complex(1e200, 1e200), 1+0j)
116105
self.check_div(complex(1e-200, 1e-200), 1+0j)
117106

107+
# Smith's algorithm has several sources of inaccuracy
108+
# for components of the result. In examples below,
109+
# it's cancellation of digits in computation of sum.
110+
self.check_div(1e-09+1j, 1+1j)
111+
self.check_div(8.289760544677449e-09+0.13257307440728516j,
112+
0.9059966714925808+0.5054864708672686j)
113+
118114
# Just for fun.
119115
for i in range(100):
120-
self.check_div(complex(random(), random()),
121-
complex(random(), random()))
116+
x = complex(random(), random())
117+
y = complex(random(), random())
118+
self.check_div(x, y)
119+
y = complex(1e10*y.real, y.imag)
120+
self.check_div(x, y)
122121

123122
self.assertAlmostEqual(complex.__truediv__(2+0j, 1+1j), 1-1j)
124123
self.assertRaises(TypeError, operator.truediv, 1j, None)
@@ -454,7 +453,7 @@ def test_boolcontext(self):
454453
self.assertTrue(1j)
455454

456455
def test_conjugate(self):
457-
self.assertClose(complex(5.3, 9.8).conjugate(), 5.3-9.8j)
456+
self.assertEqual(complex(5.3, 9.8).conjugate(), 5.3-9.8j)
458457

459458
def test_constructor(self):
460459
def check(z, x, y):

0 commit comments

Comments
 (0)