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