MCPcopy Index your code
hub / github.com/python/cpython / test_truediv

Method test_truediv

Lib/test/test_complex.py:95–195  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

93 self.assertClose(z / y, x)
94
95 def test_truediv(self):
96 simple_real = [float(i) for i in range(-5, 6)]
97 simple_complex = [complex(x, y) for x in simple_real for y in simple_real]
98 for x in simple_complex:
99 for y in simple_complex:
100 self.check_div(x, y)
101
102 # A naive complex division algorithm (such as in 2.0) is very prone to
103 # nonsense errors for these (overflows and underflows).
104 self.check_div(complex(1e200, 1e200), 1+0j)
105 self.check_div(complex(1e-200, 1e-200), 1+0j)
106
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
114 # Just for fun.
115 for i in range(100):
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)
121
122 self.assertAlmostEqual(complex.__truediv__(2+0j, 1+1j), 1-1j)
123 self.assertRaises(TypeError, operator.truediv, 1j, None)
124 self.assertRaises(TypeError, operator.truediv, None, 1j)
125
126 for denom_real, denom_imag in [(0, NAN), (NAN, 0), (NAN, NAN)]:
127 z = complex(0, 0) / complex(denom_real, denom_imag)
128 self.assertTrue(isnan(z.real))
129 self.assertTrue(isnan(z.imag))
130 z = float(0) / complex(denom_real, denom_imag)
131 self.assertTrue(isnan(z.real))
132 self.assertTrue(isnan(z.imag))
133
134 self.assertComplexesAreIdentical(complex(INF, NAN) / 2,
135 complex(INF, NAN))
136
137 self.assertComplexesAreIdentical(complex(INF, 1)/(0.0+1j),
138 complex(NAN, -INF))
139
140 # test recover of infs if numerator has infs and denominator is finite
141 self.assertComplexesAreIdentical(complex(INF, -INF)/(1+0j),
142 complex(INF, -INF))
143 self.assertComplexesAreIdentical(complex(INF, INF)/(0.0+1j),
144 complex(INF, -INF))
145 self.assertComplexesAreIdentical(complex(NAN, INF)/complex(2**1000, 2**-1000),
146 complex(INF, INF))
147 self.assertComplexesAreIdentical(complex(INF, NAN)/complex(2**1000, 2**-1000),
148 complex(INF, -INF))
149
150 # test recover of zeros if denominator is infinite
151 self.assertComplexesAreIdentical((1+1j)/complex(INF, INF), (0.0+0j))
152 self.assertComplexesAreIdentical((1+1j)/complex(INF, -INF), (0.0+0j))

Callers

nothing calls this directly

Calls 7

check_divMethod · 0.95
assertAlmostEqualMethod · 0.95
assertTrueMethod · 0.80
__truediv__Method · 0.45
assertRaisesMethod · 0.45
assertEqualMethod · 0.45

Tested by

no test coverage detected