ยปCore Development>Code coverage>Lib/test/test_cmath.py

Python code coverage for Lib/test/test_cmath.py

#countcontent
1n/afrom test.support import requires_IEEE_754, cpython_only
2n/afrom test.test_math import parse_testfile, test_file
3n/aimport test.test_math as test_math
4n/aimport unittest
5n/aimport cmath, math
6n/afrom cmath import phase, polar, rect, pi
7n/aimport platform
8n/aimport sys
9n/aimport sysconfig
10n/a
11n/aINF = float('inf')
12n/aNAN = float('nan')
13n/a
14n/acomplex_zeros = [complex(x, y) for x in [0.0, -0.0] for y in [0.0, -0.0]]
15n/acomplex_infinities = [complex(x, y) for x, y in [
16n/a (INF, 0.0), # 1st quadrant
17n/a (INF, 2.3),
18n/a (INF, INF),
19n/a (2.3, INF),
20n/a (0.0, INF),
21n/a (-0.0, INF), # 2nd quadrant
22n/a (-2.3, INF),
23n/a (-INF, INF),
24n/a (-INF, 2.3),
25n/a (-INF, 0.0),
26n/a (-INF, -0.0), # 3rd quadrant
27n/a (-INF, -2.3),
28n/a (-INF, -INF),
29n/a (-2.3, -INF),
30n/a (-0.0, -INF),
31n/a (0.0, -INF), # 4th quadrant
32n/a (2.3, -INF),
33n/a (INF, -INF),
34n/a (INF, -2.3),
35n/a (INF, -0.0)
36n/a ]]
37n/acomplex_nans = [complex(x, y) for x, y in [
38n/a (NAN, -INF),
39n/a (NAN, -2.3),
40n/a (NAN, -0.0),
41n/a (NAN, 0.0),
42n/a (NAN, 2.3),
43n/a (NAN, INF),
44n/a (-INF, NAN),
45n/a (-2.3, NAN),
46n/a (-0.0, NAN),
47n/a (0.0, NAN),
48n/a (2.3, NAN),
49n/a (INF, NAN)
50n/a ]]
51n/a
52n/aclass CMathTests(unittest.TestCase):
53n/a # list of all functions in cmath
54n/a test_functions = [getattr(cmath, fname) for fname in [
55n/a 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atanh',
56n/a 'cos', 'cosh', 'exp', 'log', 'log10', 'sin', 'sinh',
57n/a 'sqrt', 'tan', 'tanh']]
58n/a # test first and second arguments independently for 2-argument log
59n/a test_functions.append(lambda x : cmath.log(x, 1729. + 0j))
60n/a test_functions.append(lambda x : cmath.log(14.-27j, x))
61n/a
62n/a def setUp(self):
63n/a self.test_values = open(test_file)
64n/a
65n/a def tearDown(self):
66n/a self.test_values.close()
67n/a
68n/a def assertFloatIdentical(self, x, y):
69n/a """Fail unless floats x and y are identical, in the sense that:
70n/a (1) both x and y are nans, or
71n/a (2) both x and y are infinities, with the same sign, or
72n/a (3) both x and y are zeros, with the same sign, or
73n/a (4) x and y are both finite and nonzero, and x == y
74n/a
75n/a """
76n/a msg = 'floats {!r} and {!r} are not identical'
77n/a
78n/a if math.isnan(x) or math.isnan(y):
79n/a if math.isnan(x) and math.isnan(y):
80n/a return
81n/a elif x == y:
82n/a if x != 0.0:
83n/a return
84n/a # both zero; check that signs match
85n/a elif math.copysign(1.0, x) == math.copysign(1.0, y):
86n/a return
87n/a else:
88n/a msg += ': zeros have different signs'
89n/a self.fail(msg.format(x, y))
90n/a
91n/a def assertComplexIdentical(self, x, y):
92n/a """Fail unless complex numbers x and y have equal values and signs.
93n/a
94n/a In particular, if x and y both have real (or imaginary) part
95n/a zero, but the zeros have different signs, this test will fail.
96n/a
97n/a """
98n/a self.assertFloatIdentical(x.real, y.real)
99n/a self.assertFloatIdentical(x.imag, y.imag)
100n/a
101n/a def rAssertAlmostEqual(self, a, b, rel_err = 2e-15, abs_err = 5e-323,
102n/a msg=None):
103n/a """Fail if the two floating-point numbers are not almost equal.
104n/a
105n/a Determine whether floating-point values a and b are equal to within
106n/a a (small) rounding error. The default values for rel_err and
107n/a abs_err are chosen to be suitable for platforms where a float is
108n/a represented by an IEEE 754 double. They allow an error of between
109n/a 9 and 19 ulps.
110n/a """
111n/a
112n/a # special values testing
113n/a if math.isnan(a):
114n/a if math.isnan(b):
115n/a return
116n/a self.fail(msg or '{!r} should be nan'.format(b))
117n/a
118n/a if math.isinf(a):
119n/a if a == b:
120n/a return
121n/a self.fail(msg or 'finite result where infinity expected: '
122n/a 'expected {!r}, got {!r}'.format(a, b))
123n/a
124n/a # if both a and b are zero, check whether they have the same sign
125n/a # (in theory there are examples where it would be legitimate for a
126n/a # and b to have opposite signs; in practice these hardly ever
127n/a # occur).
128n/a if not a and not b:
129n/a if math.copysign(1., a) != math.copysign(1., b):
130n/a self.fail(msg or 'zero has wrong sign: expected {!r}, '
131n/a 'got {!r}'.format(a, b))
132n/a
133n/a # if a-b overflows, or b is infinite, return False. Again, in
134n/a # theory there are examples where a is within a few ulps of the
135n/a # max representable float, and then b could legitimately be
136n/a # infinite. In practice these examples are rare.
137n/a try:
138n/a absolute_error = abs(b-a)
139n/a except OverflowError:
140n/a pass
141n/a else:
142n/a # test passes if either the absolute error or the relative
143n/a # error is sufficiently small. The defaults amount to an
144n/a # error of between 9 ulps and 19 ulps on an IEEE-754 compliant
145n/a # machine.
146n/a if absolute_error <= max(abs_err, rel_err * abs(a)):
147n/a return
148n/a self.fail(msg or
149n/a '{!r} and {!r} are not sufficiently close'.format(a, b))
150n/a
151n/a def test_constants(self):
152n/a e_expected = 2.71828182845904523536
153n/a pi_expected = 3.14159265358979323846
154n/a self.assertAlmostEqual(cmath.pi, pi_expected, places=9,
155n/a msg="cmath.pi is {}; should be {}".format(cmath.pi, pi_expected))
156n/a self.assertAlmostEqual(cmath.e, e_expected, places=9,
157n/a msg="cmath.e is {}; should be {}".format(cmath.e, e_expected))
158n/a
159n/a def test_infinity_and_nan_constants(self):
160n/a self.assertEqual(cmath.inf.real, math.inf)
161n/a self.assertEqual(cmath.inf.imag, 0.0)
162n/a self.assertEqual(cmath.infj.real, 0.0)
163n/a self.assertEqual(cmath.infj.imag, math.inf)
164n/a
165n/a self.assertTrue(math.isnan(cmath.nan.real))
166n/a self.assertEqual(cmath.nan.imag, 0.0)
167n/a self.assertEqual(cmath.nanj.real, 0.0)
168n/a self.assertTrue(math.isnan(cmath.nanj.imag))
169n/a
170n/a # Check consistency with reprs.
171n/a self.assertEqual(repr(cmath.inf), "inf")
172n/a self.assertEqual(repr(cmath.infj), "infj")
173n/a self.assertEqual(repr(cmath.nan), "nan")
174n/a self.assertEqual(repr(cmath.nanj), "nanj")
175n/a
176n/a def test_user_object(self):
177n/a # Test automatic calling of __complex__ and __float__ by cmath
178n/a # functions
179n/a
180n/a # some random values to use as test values; we avoid values
181n/a # for which any of the functions in cmath is undefined
182n/a # (i.e. 0., 1., -1., 1j, -1j) or would cause overflow
183n/a cx_arg = 4.419414439 + 1.497100113j
184n/a flt_arg = -6.131677725
185n/a
186n/a # a variety of non-complex numbers, used to check that
187n/a # non-complex return values from __complex__ give an error
188n/a non_complexes = ["not complex", 1, 5, 2., None,
189n/a object(), NotImplemented]
190n/a
191n/a # Now we introduce a variety of classes whose instances might
192n/a # end up being passed to the cmath functions
193n/a
194n/a # usual case: new-style class implementing __complex__
195n/a class MyComplex(object):
196n/a def __init__(self, value):
197n/a self.value = value
198n/a def __complex__(self):
199n/a return self.value
200n/a
201n/a # old-style class implementing __complex__
202n/a class MyComplexOS:
203n/a def __init__(self, value):
204n/a self.value = value
205n/a def __complex__(self):
206n/a return self.value
207n/a
208n/a # classes for which __complex__ raises an exception
209n/a class SomeException(Exception):
210n/a pass
211n/a class MyComplexException(object):
212n/a def __complex__(self):
213n/a raise SomeException
214n/a class MyComplexExceptionOS:
215n/a def __complex__(self):
216n/a raise SomeException
217n/a
218n/a # some classes not providing __float__ or __complex__
219n/a class NeitherComplexNorFloat(object):
220n/a pass
221n/a class NeitherComplexNorFloatOS:
222n/a pass
223n/a class MyInt(object):
224n/a def __int__(self): return 2
225n/a def __index__(self): return 2
226n/a class MyIntOS:
227n/a def __int__(self): return 2
228n/a def __index__(self): return 2
229n/a
230n/a # other possible combinations of __float__ and __complex__
231n/a # that should work
232n/a class FloatAndComplex(object):
233n/a def __float__(self):
234n/a return flt_arg
235n/a def __complex__(self):
236n/a return cx_arg
237n/a class FloatAndComplexOS:
238n/a def __float__(self):
239n/a return flt_arg
240n/a def __complex__(self):
241n/a return cx_arg
242n/a class JustFloat(object):
243n/a def __float__(self):
244n/a return flt_arg
245n/a class JustFloatOS:
246n/a def __float__(self):
247n/a return flt_arg
248n/a
249n/a for f in self.test_functions:
250n/a # usual usage
251n/a self.assertEqual(f(MyComplex(cx_arg)), f(cx_arg))
252n/a self.assertEqual(f(MyComplexOS(cx_arg)), f(cx_arg))
253n/a # other combinations of __float__ and __complex__
254n/a self.assertEqual(f(FloatAndComplex()), f(cx_arg))
255n/a self.assertEqual(f(FloatAndComplexOS()), f(cx_arg))
256n/a self.assertEqual(f(JustFloat()), f(flt_arg))
257n/a self.assertEqual(f(JustFloatOS()), f(flt_arg))
258n/a # TypeError should be raised for classes not providing
259n/a # either __complex__ or __float__, even if they provide
260n/a # __int__ or __index__. An old-style class
261n/a # currently raises AttributeError instead of a TypeError;
262n/a # this could be considered a bug.
263n/a self.assertRaises(TypeError, f, NeitherComplexNorFloat())
264n/a self.assertRaises(TypeError, f, MyInt())
265n/a self.assertRaises(Exception, f, NeitherComplexNorFloatOS())
266n/a self.assertRaises(Exception, f, MyIntOS())
267n/a # non-complex return value from __complex__ -> TypeError
268n/a for bad_complex in non_complexes:
269n/a self.assertRaises(TypeError, f, MyComplex(bad_complex))
270n/a self.assertRaises(TypeError, f, MyComplexOS(bad_complex))
271n/a # exceptions in __complex__ should be propagated correctly
272n/a self.assertRaises(SomeException, f, MyComplexException())
273n/a self.assertRaises(SomeException, f, MyComplexExceptionOS())
274n/a
275n/a def test_input_type(self):
276n/a # ints should be acceptable inputs to all cmath
277n/a # functions, by virtue of providing a __float__ method
278n/a for f in self.test_functions:
279n/a for arg in [2, 2.]:
280n/a self.assertEqual(f(arg), f(arg.__float__()))
281n/a
282n/a # but strings should give a TypeError
283n/a for f in self.test_functions:
284n/a for arg in ["a", "long_string", "0", "1j", ""]:
285n/a self.assertRaises(TypeError, f, arg)
286n/a
287n/a def test_cmath_matches_math(self):
288n/a # check that corresponding cmath and math functions are equal
289n/a # for floats in the appropriate range
290n/a
291n/a # test_values in (0, 1)
292n/a test_values = [0.01, 0.1, 0.2, 0.5, 0.9, 0.99]
293n/a
294n/a # test_values for functions defined on [-1., 1.]
295n/a unit_interval = test_values + [-x for x in test_values] + \
296n/a [0., 1., -1.]
297n/a
298n/a # test_values for log, log10, sqrt
299n/a positive = test_values + [1.] + [1./x for x in test_values]
300n/a nonnegative = [0.] + positive
301n/a
302n/a # test_values for functions defined on the whole real line
303n/a real_line = [0.] + positive + [-x for x in positive]
304n/a
305n/a test_functions = {
306n/a 'acos' : unit_interval,
307n/a 'asin' : unit_interval,
308n/a 'atan' : real_line,
309n/a 'cos' : real_line,
310n/a 'cosh' : real_line,
311n/a 'exp' : real_line,
312n/a 'log' : positive,
313n/a 'log10' : positive,
314n/a 'sin' : real_line,
315n/a 'sinh' : real_line,
316n/a 'sqrt' : nonnegative,
317n/a 'tan' : real_line,
318n/a 'tanh' : real_line}
319n/a
320n/a for fn, values in test_functions.items():
321n/a float_fn = getattr(math, fn)
322n/a complex_fn = getattr(cmath, fn)
323n/a for v in values:
324n/a z = complex_fn(v)
325n/a self.rAssertAlmostEqual(float_fn(v), z.real)
326n/a self.assertEqual(0., z.imag)
327n/a
328n/a # test two-argument version of log with various bases
329n/a for base in [0.5, 2., 10.]:
330n/a for v in positive:
331n/a z = cmath.log(v, base)
332n/a self.rAssertAlmostEqual(math.log(v, base), z.real)
333n/a self.assertEqual(0., z.imag)
334n/a
335n/a @requires_IEEE_754
336n/a def test_specific_values(self):
337n/a # Some tests need to be skipped on ancient OS X versions.
338n/a # See issue #27953.
339n/a SKIP_ON_TIGER = {'tan0064'}
340n/a
341n/a osx_version = None
342n/a if sys.platform == 'darwin':
343n/a version_txt = platform.mac_ver()[0]
344n/a try:
345n/a osx_version = tuple(map(int, version_txt.split('.')))
346n/a except ValueError:
347n/a pass
348n/a
349n/a def rect_complex(z):
350n/a """Wrapped version of rect that accepts a complex number instead of
351n/a two float arguments."""
352n/a return cmath.rect(z.real, z.imag)
353n/a
354n/a def polar_complex(z):
355n/a """Wrapped version of polar that returns a complex number instead of
356n/a two floats."""
357n/a return complex(*polar(z))
358n/a
359n/a for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file):
360n/a arg = complex(ar, ai)
361n/a expected = complex(er, ei)
362n/a
363n/a # Skip certain tests on OS X 10.4.
364n/a if osx_version is not None and osx_version < (10, 5):
365n/a if id in SKIP_ON_TIGER:
366n/a continue
367n/a
368n/a if fn == 'rect':
369n/a function = rect_complex
370n/a elif fn == 'polar':
371n/a function = polar_complex
372n/a else:
373n/a function = getattr(cmath, fn)
374n/a if 'divide-by-zero' in flags or 'invalid' in flags:
375n/a try:
376n/a actual = function(arg)
377n/a except ValueError:
378n/a continue
379n/a else:
380n/a self.fail('ValueError not raised in test '
381n/a '{}: {}(complex({!r}, {!r}))'.format(id, fn, ar, ai))
382n/a
383n/a if 'overflow' in flags:
384n/a try:
385n/a actual = function(arg)
386n/a except OverflowError:
387n/a continue
388n/a else:
389n/a self.fail('OverflowError not raised in test '
390n/a '{}: {}(complex({!r}, {!r}))'.format(id, fn, ar, ai))
391n/a
392n/a actual = function(arg)
393n/a
394n/a if 'ignore-real-sign' in flags:
395n/a actual = complex(abs(actual.real), actual.imag)
396n/a expected = complex(abs(expected.real), expected.imag)
397n/a if 'ignore-imag-sign' in flags:
398n/a actual = complex(actual.real, abs(actual.imag))
399n/a expected = complex(expected.real, abs(expected.imag))
400n/a
401n/a # for the real part of the log function, we allow an
402n/a # absolute error of up to 2e-15.
403n/a if fn in ('log', 'log10'):
404n/a real_abs_err = 2e-15
405n/a else:
406n/a real_abs_err = 5e-323
407n/a
408n/a error_message = (
409n/a '{}: {}(complex({!r}, {!r}))\n'
410n/a 'Expected: complex({!r}, {!r})\n'
411n/a 'Received: complex({!r}, {!r})\n'
412n/a 'Received value insufficiently close to expected value.'
413n/a ).format(id, fn, ar, ai,
414n/a expected.real, expected.imag,
415n/a actual.real, actual.imag)
416n/a self.rAssertAlmostEqual(expected.real, actual.real,
417n/a abs_err=real_abs_err,
418n/a msg=error_message)
419n/a self.rAssertAlmostEqual(expected.imag, actual.imag,
420n/a msg=error_message)
421n/a
422n/a def check_polar(self, func):
423n/a def check(arg, expected):
424n/a got = func(arg)
425n/a for e, g in zip(expected, got):
426n/a self.rAssertAlmostEqual(e, g)
427n/a check(0, (0., 0.))
428n/a check(1, (1., 0.))
429n/a check(-1, (1., pi))
430n/a check(1j, (1., pi / 2))
431n/a check(-3j, (3., -pi / 2))
432n/a inf = float('inf')
433n/a check(complex(inf, 0), (inf, 0.))
434n/a check(complex(-inf, 0), (inf, pi))
435n/a check(complex(3, inf), (inf, pi / 2))
436n/a check(complex(5, -inf), (inf, -pi / 2))
437n/a check(complex(inf, inf), (inf, pi / 4))
438n/a check(complex(inf, -inf), (inf, -pi / 4))
439n/a check(complex(-inf, inf), (inf, 3 * pi / 4))
440n/a check(complex(-inf, -inf), (inf, -3 * pi / 4))
441n/a nan = float('nan')
442n/a check(complex(nan, 0), (nan, nan))
443n/a check(complex(0, nan), (nan, nan))
444n/a check(complex(nan, nan), (nan, nan))
445n/a check(complex(inf, nan), (inf, nan))
446n/a check(complex(-inf, nan), (inf, nan))
447n/a check(complex(nan, inf), (inf, nan))
448n/a check(complex(nan, -inf), (inf, nan))
449n/a
450n/a def test_polar(self):
451n/a self.check_polar(polar)
452n/a
453n/a @cpython_only
454n/a def test_polar_errno(self):
455n/a # Issue #24489: check a previously set C errno doesn't disturb polar()
456n/a from _testcapi import set_errno
457n/a def polar_with_errno_set(z):
458n/a set_errno(11)
459n/a try:
460n/a return polar(z)
461n/a finally:
462n/a set_errno(0)
463n/a self.check_polar(polar_with_errno_set)
464n/a
465n/a def test_phase(self):
466n/a self.assertAlmostEqual(phase(0), 0.)
467n/a self.assertAlmostEqual(phase(1.), 0.)
468n/a self.assertAlmostEqual(phase(-1.), pi)
469n/a self.assertAlmostEqual(phase(-1.+1E-300j), pi)
470n/a self.assertAlmostEqual(phase(-1.-1E-300j), -pi)
471n/a self.assertAlmostEqual(phase(1j), pi/2)
472n/a self.assertAlmostEqual(phase(-1j), -pi/2)
473n/a
474n/a # zeros
475n/a self.assertEqual(phase(complex(0.0, 0.0)), 0.0)
476n/a self.assertEqual(phase(complex(0.0, -0.0)), -0.0)
477n/a self.assertEqual(phase(complex(-0.0, 0.0)), pi)
478n/a self.assertEqual(phase(complex(-0.0, -0.0)), -pi)
479n/a
480n/a # infinities
481n/a self.assertAlmostEqual(phase(complex(-INF, -0.0)), -pi)
482n/a self.assertAlmostEqual(phase(complex(-INF, -2.3)), -pi)
483n/a self.assertAlmostEqual(phase(complex(-INF, -INF)), -0.75*pi)
484n/a self.assertAlmostEqual(phase(complex(-2.3, -INF)), -pi/2)
485n/a self.assertAlmostEqual(phase(complex(-0.0, -INF)), -pi/2)
486n/a self.assertAlmostEqual(phase(complex(0.0, -INF)), -pi/2)
487n/a self.assertAlmostEqual(phase(complex(2.3, -INF)), -pi/2)
488n/a self.assertAlmostEqual(phase(complex(INF, -INF)), -pi/4)
489n/a self.assertEqual(phase(complex(INF, -2.3)), -0.0)
490n/a self.assertEqual(phase(complex(INF, -0.0)), -0.0)
491n/a self.assertEqual(phase(complex(INF, 0.0)), 0.0)
492n/a self.assertEqual(phase(complex(INF, 2.3)), 0.0)
493n/a self.assertAlmostEqual(phase(complex(INF, INF)), pi/4)
494n/a self.assertAlmostEqual(phase(complex(2.3, INF)), pi/2)
495n/a self.assertAlmostEqual(phase(complex(0.0, INF)), pi/2)
496n/a self.assertAlmostEqual(phase(complex(-0.0, INF)), pi/2)
497n/a self.assertAlmostEqual(phase(complex(-2.3, INF)), pi/2)
498n/a self.assertAlmostEqual(phase(complex(-INF, INF)), 0.75*pi)
499n/a self.assertAlmostEqual(phase(complex(-INF, 2.3)), pi)
500n/a self.assertAlmostEqual(phase(complex(-INF, 0.0)), pi)
501n/a
502n/a # real or imaginary part NaN
503n/a for z in complex_nans:
504n/a self.assertTrue(math.isnan(phase(z)))
505n/a
506n/a def test_abs(self):
507n/a # zeros
508n/a for z in complex_zeros:
509n/a self.assertEqual(abs(z), 0.0)
510n/a
511n/a # infinities
512n/a for z in complex_infinities:
513n/a self.assertEqual(abs(z), INF)
514n/a
515n/a # real or imaginary part NaN
516n/a self.assertEqual(abs(complex(NAN, -INF)), INF)
517n/a self.assertTrue(math.isnan(abs(complex(NAN, -2.3))))
518n/a self.assertTrue(math.isnan(abs(complex(NAN, -0.0))))
519n/a self.assertTrue(math.isnan(abs(complex(NAN, 0.0))))
520n/a self.assertTrue(math.isnan(abs(complex(NAN, 2.3))))
521n/a self.assertEqual(abs(complex(NAN, INF)), INF)
522n/a self.assertEqual(abs(complex(-INF, NAN)), INF)
523n/a self.assertTrue(math.isnan(abs(complex(-2.3, NAN))))
524n/a self.assertTrue(math.isnan(abs(complex(-0.0, NAN))))
525n/a self.assertTrue(math.isnan(abs(complex(0.0, NAN))))
526n/a self.assertTrue(math.isnan(abs(complex(2.3, NAN))))
527n/a self.assertEqual(abs(complex(INF, NAN)), INF)
528n/a self.assertTrue(math.isnan(abs(complex(NAN, NAN))))
529n/a
530n/a
531n/a @requires_IEEE_754
532n/a def test_abs_overflows(self):
533n/a # result overflows
534n/a self.assertRaises(OverflowError, abs, complex(1.4e308, 1.4e308))
535n/a
536n/a def assertCEqual(self, a, b):
537n/a eps = 1E-7
538n/a if abs(a.real - b[0]) > eps or abs(a.imag - b[1]) > eps:
539n/a self.fail((a ,b))
540n/a
541n/a def test_rect(self):
542n/a self.assertCEqual(rect(0, 0), (0, 0))
543n/a self.assertCEqual(rect(1, 0), (1., 0))
544n/a self.assertCEqual(rect(1, -pi), (-1., 0))
545n/a self.assertCEqual(rect(1, pi/2), (0, 1.))
546n/a self.assertCEqual(rect(1, -pi/2), (0, -1.))
547n/a
548n/a def test_isfinite(self):
549n/a real_vals = [float('-inf'), -2.3, -0.0,
550n/a 0.0, 2.3, float('inf'), float('nan')]
551n/a for x in real_vals:
552n/a for y in real_vals:
553n/a z = complex(x, y)
554n/a self.assertEqual(cmath.isfinite(z),
555n/a math.isfinite(x) and math.isfinite(y))
556n/a
557n/a def test_isnan(self):
558n/a self.assertFalse(cmath.isnan(1))
559n/a self.assertFalse(cmath.isnan(1j))
560n/a self.assertFalse(cmath.isnan(INF))
561n/a self.assertTrue(cmath.isnan(NAN))
562n/a self.assertTrue(cmath.isnan(complex(NAN, 0)))
563n/a self.assertTrue(cmath.isnan(complex(0, NAN)))
564n/a self.assertTrue(cmath.isnan(complex(NAN, NAN)))
565n/a self.assertTrue(cmath.isnan(complex(NAN, INF)))
566n/a self.assertTrue(cmath.isnan(complex(INF, NAN)))
567n/a
568n/a def test_isinf(self):
569n/a self.assertFalse(cmath.isinf(1))
570n/a self.assertFalse(cmath.isinf(1j))
571n/a self.assertFalse(cmath.isinf(NAN))
572n/a self.assertTrue(cmath.isinf(INF))
573n/a self.assertTrue(cmath.isinf(complex(INF, 0)))
574n/a self.assertTrue(cmath.isinf(complex(0, INF)))
575n/a self.assertTrue(cmath.isinf(complex(INF, INF)))
576n/a self.assertTrue(cmath.isinf(complex(NAN, INF)))
577n/a self.assertTrue(cmath.isinf(complex(INF, NAN)))
578n/a
579n/a @requires_IEEE_754
580n/a @unittest.skipIf(sysconfig.get_config_var('TANH_PRESERVES_ZERO_SIGN') == 0,
581n/a "system tanh() function doesn't copy the sign")
582n/a def testTanhSign(self):
583n/a for z in complex_zeros:
584n/a self.assertComplexIdentical(cmath.tanh(z), z)
585n/a
586n/a # The algorithm used for atan and atanh makes use of the system
587n/a # log1p function; If that system function doesn't respect the sign
588n/a # of zero, then atan and atanh will also have difficulties with
589n/a # the sign of complex zeros.
590n/a @requires_IEEE_754
591n/a def testAtanSign(self):
592n/a for z in complex_zeros:
593n/a self.assertComplexIdentical(cmath.atan(z), z)
594n/a
595n/a @requires_IEEE_754
596n/a def testAtanhSign(self):
597n/a for z in complex_zeros:
598n/a self.assertComplexIdentical(cmath.atanh(z), z)
599n/a
600n/a
601n/aclass IsCloseTests(test_math.IsCloseTests):
602n/a isclose = cmath.isclose
603n/a
604n/a def test_reject_complex_tolerances(self):
605n/a with self.assertRaises(TypeError):
606n/a self.isclose(1j, 1j, rel_tol=1j)
607n/a
608n/a with self.assertRaises(TypeError):
609n/a self.isclose(1j, 1j, abs_tol=1j)
610n/a
611n/a with self.assertRaises(TypeError):
612n/a self.isclose(1j, 1j, rel_tol=1j, abs_tol=1j)
613n/a
614n/a def test_complex_values(self):
615n/a # test complex values that are close to within 12 decimal places
616n/a complex_examples = [(1.0+1.0j, 1.000000000001+1.0j),
617n/a (1.0+1.0j, 1.0+1.000000000001j),
618n/a (-1.0+1.0j, -1.000000000001+1.0j),
619n/a (1.0-1.0j, 1.0-0.999999999999j),
620n/a ]
621n/a
622n/a self.assertAllClose(complex_examples, rel_tol=1e-12)
623n/a self.assertAllNotClose(complex_examples, rel_tol=1e-13)
624n/a
625n/a def test_complex_near_zero(self):
626n/a # test values near zero that are near to within three decimal places
627n/a near_zero_examples = [(0.001j, 0),
628n/a (0.001, 0),
629n/a (0.001+0.001j, 0),
630n/a (-0.001+0.001j, 0),
631n/a (0.001-0.001j, 0),
632n/a (-0.001-0.001j, 0),
633n/a ]
634n/a
635n/a self.assertAllClose(near_zero_examples, abs_tol=1.5e-03)
636n/a self.assertAllNotClose(near_zero_examples, abs_tol=0.5e-03)
637n/a
638n/a self.assertIsClose(0.001-0.001j, 0.001+0.001j, abs_tol=2e-03)
639n/a self.assertIsNotClose(0.001-0.001j, 0.001+0.001j, abs_tol=1e-03)
640n/a
641n/a
642n/aif __name__ == "__main__":
643n/a unittest.main()