blob: 4e93be47add653f47bcf3bde80507f7669142c47 [file] [log] [blame]
Eric Smith3ab08ca2010-12-04 15:17:38 +00001from test.support import run_unittest, requires_IEEE_754
2from test.test_math import parse_testfile, test_file
Guido van Rossumd8faa362007-04-27 19:54:29 +00003import unittest
Raymond Hettingerb67ad7e2004-06-14 07:40:10 +00004import cmath, math
Christian Heimes53876d92008-04-19 00:31:39 +00005from cmath import phase, polar, rect, pi
Victor Stinnerbe3da382010-11-07 14:14:27 +00006import sysconfig
Christian Heimes53876d92008-04-19 00:31:39 +00007
8INF = float('inf')
9NAN = float('nan')
10
11complex_zeros = [complex(x, y) for x in [0.0, -0.0] for y in [0.0, -0.0]]
12complex_infinities = [complex(x, y) for x, y in [
13 (INF, 0.0), # 1st quadrant
14 (INF, 2.3),
15 (INF, INF),
16 (2.3, INF),
17 (0.0, INF),
18 (-0.0, INF), # 2nd quadrant
19 (-2.3, INF),
20 (-INF, INF),
21 (-INF, 2.3),
22 (-INF, 0.0),
23 (-INF, -0.0), # 3rd quadrant
24 (-INF, -2.3),
25 (-INF, -INF),
26 (-2.3, -INF),
27 (-0.0, -INF),
28 (0.0, -INF), # 4th quadrant
29 (2.3, -INF),
30 (INF, -INF),
31 (INF, -2.3),
32 (INF, -0.0)
33 ]]
34complex_nans = [complex(x, y) for x, y in [
35 (NAN, -INF),
36 (NAN, -2.3),
37 (NAN, -0.0),
38 (NAN, 0.0),
39 (NAN, 2.3),
40 (NAN, INF),
41 (-INF, NAN),
42 (-2.3, NAN),
43 (-0.0, NAN),
44 (0.0, NAN),
45 (2.3, NAN),
46 (INF, NAN)
47 ]]
48
Guido van Rossumd8faa362007-04-27 19:54:29 +000049class CMathTests(unittest.TestCase):
50 # list of all functions in cmath
51 test_functions = [getattr(cmath, fname) for fname in [
52 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atanh',
53 'cos', 'cosh', 'exp', 'log', 'log10', 'sin', 'sinh',
54 'sqrt', 'tan', 'tanh']]
55 # test first and second arguments independently for 2-argument log
56 test_functions.append(lambda x : cmath.log(x, 1729. + 0j))
57 test_functions.append(lambda x : cmath.log(14.-27j, x))
Raymond Hettingerb67ad7e2004-06-14 07:40:10 +000058
Christian Heimes53876d92008-04-19 00:31:39 +000059 def setUp(self):
60 self.test_values = open(test_file)
61
62 def tearDown(self):
63 self.test_values.close()
64
Mark Dickinsona837aa62010-11-07 15:31:41 +000065 def assertFloatIdentical(self, x, y):
66 """Fail unless floats x and y are identical, in the sense that:
67 (1) both x and y are nans, or
68 (2) both x and y are infinities, with the same sign, or
69 (3) both x and y are zeros, with the same sign, or
70 (4) x and y are both finite and nonzero, and x == y
71
72 """
73 msg = 'floats {!r} and {!r} are not identical'
74
75 if math.isnan(x) or math.isnan(y):
76 if math.isnan(x) and math.isnan(y):
77 return
78 elif x == y:
79 if x != 0.0:
80 return
81 # both zero; check that signs match
82 elif math.copysign(1.0, x) == math.copysign(1.0, y):
83 return
84 else:
85 msg += ': zeros have different signs'
86 self.fail(msg.format(x, y))
87
88 def assertComplexIdentical(self, x, y):
89 """Fail unless complex numbers x and y have equal values and signs.
90
91 In particular, if x and y both have real (or imaginary) part
92 zero, but the zeros have different signs, this test will fail.
93
94 """
95 self.assertFloatIdentical(x.real, y.real)
96 self.assertFloatIdentical(x.imag, y.imag)
Victor Stinnerbe3da382010-11-07 14:14:27 +000097
Mark Dickinson4d1e50d2009-12-20 20:37:56 +000098 def rAssertAlmostEqual(self, a, b, rel_err = 2e-15, abs_err = 5e-323,
99 msg=None):
100 """Fail if the two floating-point numbers are not almost equal.
101
102 Determine whether floating-point values a and b are equal to within
103 a (small) rounding error. The default values for rel_err and
104 abs_err are chosen to be suitable for platforms where a float is
105 represented by an IEEE 754 double. They allow an error of between
106 9 and 19 ulps.
107 """
Christian Heimes53876d92008-04-19 00:31:39 +0000108
109 # special values testing
110 if math.isnan(a):
111 if math.isnan(b):
112 return
Mark Dickinson4d1e50d2009-12-20 20:37:56 +0000113 self.fail(msg or '{!r} should be nan'.format(b))
Christian Heimes53876d92008-04-19 00:31:39 +0000114
115 if math.isinf(a):
116 if a == b:
117 return
Mark Dickinson4d1e50d2009-12-20 20:37:56 +0000118 self.fail(msg or 'finite result where infinity expected: '
119 'expected {!r}, got {!r}'.format(a, b))
Christian Heimes53876d92008-04-19 00:31:39 +0000120
Mark Dickinson4d1e50d2009-12-20 20:37:56 +0000121 # if both a and b are zero, check whether they have the same sign
122 # (in theory there are examples where it would be legitimate for a
123 # and b to have opposite signs; in practice these hardly ever
124 # occur).
Christian Heimes53876d92008-04-19 00:31:39 +0000125 if not a and not b:
Mark Dickinson4d1e50d2009-12-20 20:37:56 +0000126 if math.copysign(1., a) != math.copysign(1., b):
127 self.fail(msg or 'zero has wrong sign: expected {!r}, '
128 'got {!r}'.format(a, b))
Christian Heimes53876d92008-04-19 00:31:39 +0000129
Mark Dickinson4d1e50d2009-12-20 20:37:56 +0000130 # if a-b overflows, or b is infinite, return False. Again, in
131 # theory there are examples where a is within a few ulps of the
132 # max representable float, and then b could legitimately be
133 # infinite. In practice these examples are rare.
Christian Heimes53876d92008-04-19 00:31:39 +0000134 try:
135 absolute_error = abs(b-a)
136 except OverflowError:
137 pass
138 else:
Mark Dickinson4d1e50d2009-12-20 20:37:56 +0000139 # test passes if either the absolute error or the relative
140 # error is sufficiently small. The defaults amount to an
141 # error of between 9 ulps and 19 ulps on an IEEE-754 compliant
142 # machine.
Christian Heimes53876d92008-04-19 00:31:39 +0000143 if absolute_error <= max(abs_err, rel_err * abs(a)):
144 return
Mark Dickinson4d1e50d2009-12-20 20:37:56 +0000145 self.fail(msg or
146 '{!r} and {!r} are not sufficiently close'.format(a, b))
Raymond Hettingerb67ad7e2004-06-14 07:40:10 +0000147
Guido van Rossumd8faa362007-04-27 19:54:29 +0000148 def test_constants(self):
149 e_expected = 2.71828182845904523536
150 pi_expected = 3.14159265358979323846
Mark Dickinsonda892452009-12-20 19:56:09 +0000151 self.assertAlmostEqual(cmath.pi, pi_expected, places=9,
Mark Dickinson4d1e50d2009-12-20 20:37:56 +0000152 msg="cmath.pi is {}; should be {}".format(cmath.pi, pi_expected))
Mark Dickinsonda892452009-12-20 19:56:09 +0000153 self.assertAlmostEqual(cmath.e, e_expected, places=9,
Mark Dickinson4d1e50d2009-12-20 20:37:56 +0000154 msg="cmath.e is {}; should be {}".format(cmath.e, e_expected))
Roger E. Masse3daddda1996-12-09 22:59:15 +0000155
Guido van Rossumd8faa362007-04-27 19:54:29 +0000156 def test_user_object(self):
157 # Test automatic calling of __complex__ and __float__ by cmath
158 # functions
Roger E. Massefab8ab81996-12-20 22:36:52 +0000159
Guido van Rossumd8faa362007-04-27 19:54:29 +0000160 # some random values to use as test values; we avoid values
161 # for which any of the functions in cmath is undefined
162 # (i.e. 0., 1., -1., 1j, -1j) or would cause overflow
163 cx_arg = 4.419414439 + 1.497100113j
164 flt_arg = -6.131677725
Roger E. Massefab8ab81996-12-20 22:36:52 +0000165
Guido van Rossumd8faa362007-04-27 19:54:29 +0000166 # a variety of non-complex numbers, used to check that
167 # non-complex return values from __complex__ give an error
168 non_complexes = ["not complex", 1, 5, 2., None,
169 object(), NotImplemented]
170
171 # Now we introduce a variety of classes whose instances might
172 # end up being passed to the cmath functions
173
174 # usual case: new-style class implementing __complex__
175 class MyComplex(object):
176 def __init__(self, value):
177 self.value = value
178 def __complex__(self):
179 return self.value
180
181 # old-style class implementing __complex__
182 class MyComplexOS:
183 def __init__(self, value):
184 self.value = value
185 def __complex__(self):
186 return self.value
187
188 # classes for which __complex__ raises an exception
189 class SomeException(Exception):
190 pass
191 class MyComplexException(object):
192 def __complex__(self):
193 raise SomeException
194 class MyComplexExceptionOS:
195 def __complex__(self):
196 raise SomeException
197
198 # some classes not providing __float__ or __complex__
199 class NeitherComplexNorFloat(object):
200 pass
201 class NeitherComplexNorFloatOS:
202 pass
203 class MyInt(object):
204 def __int__(self): return 2
Guido van Rossumd8faa362007-04-27 19:54:29 +0000205 def __index__(self): return 2
206 class MyIntOS:
207 def __int__(self): return 2
Guido van Rossumd8faa362007-04-27 19:54:29 +0000208 def __index__(self): return 2
209
210 # other possible combinations of __float__ and __complex__
211 # that should work
212 class FloatAndComplex(object):
213 def __float__(self):
214 return flt_arg
215 def __complex__(self):
216 return cx_arg
217 class FloatAndComplexOS:
218 def __float__(self):
219 return flt_arg
220 def __complex__(self):
221 return cx_arg
222 class JustFloat(object):
223 def __float__(self):
224 return flt_arg
225 class JustFloatOS:
226 def __float__(self):
227 return flt_arg
228
229 for f in self.test_functions:
230 # usual usage
Christian Heimes53876d92008-04-19 00:31:39 +0000231 self.assertEqual(f(MyComplex(cx_arg)), f(cx_arg))
232 self.assertEqual(f(MyComplexOS(cx_arg)), f(cx_arg))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000233 # other combinations of __float__ and __complex__
Christian Heimes53876d92008-04-19 00:31:39 +0000234 self.assertEqual(f(FloatAndComplex()), f(cx_arg))
235 self.assertEqual(f(FloatAndComplexOS()), f(cx_arg))
236 self.assertEqual(f(JustFloat()), f(flt_arg))
237 self.assertEqual(f(JustFloatOS()), f(flt_arg))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000238 # TypeError should be raised for classes not providing
239 # either __complex__ or __float__, even if they provide
Mark Dickinsoncce2f212009-01-15 19:32:23 +0000240 # __int__ or __index__. An old-style class
Guido van Rossumd8faa362007-04-27 19:54:29 +0000241 # currently raises AttributeError instead of a TypeError;
242 # this could be considered a bug.
243 self.assertRaises(TypeError, f, NeitherComplexNorFloat())
244 self.assertRaises(TypeError, f, MyInt())
245 self.assertRaises(Exception, f, NeitherComplexNorFloatOS())
246 self.assertRaises(Exception, f, MyIntOS())
247 # non-complex return value from __complex__ -> TypeError
248 for bad_complex in non_complexes:
249 self.assertRaises(TypeError, f, MyComplex(bad_complex))
250 self.assertRaises(TypeError, f, MyComplexOS(bad_complex))
251 # exceptions in __complex__ should be propagated correctly
252 self.assertRaises(SomeException, f, MyComplexException())
253 self.assertRaises(SomeException, f, MyComplexExceptionOS())
254
255 def test_input_type(self):
256 # ints and longs should be acceptable inputs to all cmath
257 # functions, by virtue of providing a __float__ method
258 for f in self.test_functions:
259 for arg in [2, 2.]:
Christian Heimes53876d92008-04-19 00:31:39 +0000260 self.assertEqual(f(arg), f(arg.__float__()))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000261
262 # but strings should give a TypeError
263 for f in self.test_functions:
264 for arg in ["a", "long_string", "0", "1j", ""]:
265 self.assertRaises(TypeError, f, arg)
266
267 def test_cmath_matches_math(self):
268 # check that corresponding cmath and math functions are equal
269 # for floats in the appropriate range
270
271 # test_values in (0, 1)
272 test_values = [0.01, 0.1, 0.2, 0.5, 0.9, 0.99]
273
274 # test_values for functions defined on [-1., 1.]
275 unit_interval = test_values + [-x for x in test_values] + \
276 [0., 1., -1.]
277
278 # test_values for log, log10, sqrt
279 positive = test_values + [1.] + [1./x for x in test_values]
280 nonnegative = [0.] + positive
281
282 # test_values for functions defined on the whole real line
283 real_line = [0.] + positive + [-x for x in positive]
284
285 test_functions = {
286 'acos' : unit_interval,
287 'asin' : unit_interval,
288 'atan' : real_line,
289 'cos' : real_line,
290 'cosh' : real_line,
291 'exp' : real_line,
292 'log' : positive,
293 'log10' : positive,
294 'sin' : real_line,
295 'sinh' : real_line,
296 'sqrt' : nonnegative,
297 'tan' : real_line,
298 'tanh' : real_line}
299
300 for fn, values in test_functions.items():
301 float_fn = getattr(math, fn)
302 complex_fn = getattr(cmath, fn)
303 for v in values:
Christian Heimes53876d92008-04-19 00:31:39 +0000304 z = complex_fn(v)
305 self.rAssertAlmostEqual(float_fn(v), z.real)
306 self.assertEqual(0., z.imag)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000307
308 # test two-argument version of log with various bases
309 for base in [0.5, 2., 10.]:
310 for v in positive:
Christian Heimes53876d92008-04-19 00:31:39 +0000311 z = cmath.log(v, base)
312 self.rAssertAlmostEqual(math.log(v, base), z.real)
313 self.assertEqual(0., z.imag)
314
Eric Smith3ab08ca2010-12-04 15:17:38 +0000315 @requires_IEEE_754
Christian Heimes53876d92008-04-19 00:31:39 +0000316 def test_specific_values(self):
Christian Heimes53876d92008-04-19 00:31:39 +0000317 def rect_complex(z):
318 """Wrapped version of rect that accepts a complex number instead of
319 two float arguments."""
320 return cmath.rect(z.real, z.imag)
321
322 def polar_complex(z):
323 """Wrapped version of polar that returns a complex number instead of
324 two floats."""
325 return complex(*polar(z))
326
327 for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file):
328 arg = complex(ar, ai)
329 expected = complex(er, ei)
330 if fn == 'rect':
331 function = rect_complex
332 elif fn == 'polar':
333 function = polar_complex
334 else:
335 function = getattr(cmath, fn)
336 if 'divide-by-zero' in flags or 'invalid' in flags:
337 try:
338 actual = function(arg)
339 except ValueError:
340 continue
341 else:
Mark Dickinson4d1e50d2009-12-20 20:37:56 +0000342 self.fail('ValueError not raised in test '
343 '{}: {}(complex({!r}, {!r}))'.format(id, fn, ar, ai))
Christian Heimes53876d92008-04-19 00:31:39 +0000344
345 if 'overflow' in flags:
346 try:
347 actual = function(arg)
348 except OverflowError:
349 continue
350 else:
Mark Dickinson4d1e50d2009-12-20 20:37:56 +0000351 self.fail('OverflowError not raised in test '
352 '{}: {}(complex({!r}, {!r}))'.format(id, fn, ar, ai))
Christian Heimes53876d92008-04-19 00:31:39 +0000353
354 actual = function(arg)
355
356 if 'ignore-real-sign' in flags:
357 actual = complex(abs(actual.real), actual.imag)
358 expected = complex(abs(expected.real), expected.imag)
359 if 'ignore-imag-sign' in flags:
360 actual = complex(actual.real, abs(actual.imag))
361 expected = complex(expected.real, abs(expected.imag))
362
363 # for the real part of the log function, we allow an
364 # absolute error of up to 2e-15.
365 if fn in ('log', 'log10'):
366 real_abs_err = 2e-15
367 else:
368 real_abs_err = 5e-323
369
Mark Dickinson4d1e50d2009-12-20 20:37:56 +0000370 error_message = (
371 '{}: {}(complex({!r}, {!r}))\n'
372 'Expected: complex({!r}, {!r})\n'
373 'Received: complex({!r}, {!r})\n'
374 'Received value insufficiently close to expected value.'
375 ).format(id, fn, ar, ai,
376 expected.real, expected.imag,
377 actual.real, actual.imag)
378 self.rAssertAlmostEqual(expected.real, actual.real,
379 abs_err=real_abs_err,
380 msg=error_message)
381 self.rAssertAlmostEqual(expected.imag, actual.imag,
382 msg=error_message)
Christian Heimes53876d92008-04-19 00:31:39 +0000383
384 def assertCISEqual(self, a, b):
385 eps = 1E-7
386 if abs(a[0] - b[0]) > eps or abs(a[1] - b[1]) > eps:
387 self.fail((a ,b))
388
389 def test_polar(self):
390 self.assertCISEqual(polar(0), (0., 0.))
391 self.assertCISEqual(polar(1.), (1., 0.))
392 self.assertCISEqual(polar(-1.), (1., pi))
393 self.assertCISEqual(polar(1j), (1., pi/2))
394 self.assertCISEqual(polar(-1j), (1., -pi/2))
395
396 def test_phase(self):
397 self.assertAlmostEqual(phase(0), 0.)
398 self.assertAlmostEqual(phase(1.), 0.)
399 self.assertAlmostEqual(phase(-1.), pi)
400 self.assertAlmostEqual(phase(-1.+1E-300j), pi)
401 self.assertAlmostEqual(phase(-1.-1E-300j), -pi)
402 self.assertAlmostEqual(phase(1j), pi/2)
403 self.assertAlmostEqual(phase(-1j), -pi/2)
404
405 # zeros
406 self.assertEqual(phase(complex(0.0, 0.0)), 0.0)
407 self.assertEqual(phase(complex(0.0, -0.0)), -0.0)
408 self.assertEqual(phase(complex(-0.0, 0.0)), pi)
409 self.assertEqual(phase(complex(-0.0, -0.0)), -pi)
410
411 # infinities
412 self.assertAlmostEqual(phase(complex(-INF, -0.0)), -pi)
413 self.assertAlmostEqual(phase(complex(-INF, -2.3)), -pi)
414 self.assertAlmostEqual(phase(complex(-INF, -INF)), -0.75*pi)
415 self.assertAlmostEqual(phase(complex(-2.3, -INF)), -pi/2)
416 self.assertAlmostEqual(phase(complex(-0.0, -INF)), -pi/2)
417 self.assertAlmostEqual(phase(complex(0.0, -INF)), -pi/2)
418 self.assertAlmostEqual(phase(complex(2.3, -INF)), -pi/2)
419 self.assertAlmostEqual(phase(complex(INF, -INF)), -pi/4)
420 self.assertEqual(phase(complex(INF, -2.3)), -0.0)
421 self.assertEqual(phase(complex(INF, -0.0)), -0.0)
422 self.assertEqual(phase(complex(INF, 0.0)), 0.0)
423 self.assertEqual(phase(complex(INF, 2.3)), 0.0)
424 self.assertAlmostEqual(phase(complex(INF, INF)), pi/4)
425 self.assertAlmostEqual(phase(complex(2.3, INF)), pi/2)
426 self.assertAlmostEqual(phase(complex(0.0, INF)), pi/2)
427 self.assertAlmostEqual(phase(complex(-0.0, INF)), pi/2)
428 self.assertAlmostEqual(phase(complex(-2.3, INF)), pi/2)
429 self.assertAlmostEqual(phase(complex(-INF, INF)), 0.75*pi)
430 self.assertAlmostEqual(phase(complex(-INF, 2.3)), pi)
431 self.assertAlmostEqual(phase(complex(-INF, 0.0)), pi)
432
433 # real or imaginary part NaN
434 for z in complex_nans:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000435 self.assertTrue(math.isnan(phase(z)))
Christian Heimes53876d92008-04-19 00:31:39 +0000436
437 def test_abs(self):
438 # zeros
439 for z in complex_zeros:
440 self.assertEqual(abs(z), 0.0)
441
442 # infinities
443 for z in complex_infinities:
444 self.assertEqual(abs(z), INF)
445
446 # real or imaginary part NaN
447 self.assertEqual(abs(complex(NAN, -INF)), INF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000448 self.assertTrue(math.isnan(abs(complex(NAN, -2.3))))
449 self.assertTrue(math.isnan(abs(complex(NAN, -0.0))))
450 self.assertTrue(math.isnan(abs(complex(NAN, 0.0))))
451 self.assertTrue(math.isnan(abs(complex(NAN, 2.3))))
Christian Heimes53876d92008-04-19 00:31:39 +0000452 self.assertEqual(abs(complex(NAN, INF)), INF)
453 self.assertEqual(abs(complex(-INF, NAN)), INF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000454 self.assertTrue(math.isnan(abs(complex(-2.3, NAN))))
455 self.assertTrue(math.isnan(abs(complex(-0.0, NAN))))
456 self.assertTrue(math.isnan(abs(complex(0.0, NAN))))
457 self.assertTrue(math.isnan(abs(complex(2.3, NAN))))
Christian Heimes53876d92008-04-19 00:31:39 +0000458 self.assertEqual(abs(complex(INF, NAN)), INF)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000459 self.assertTrue(math.isnan(abs(complex(NAN, NAN))))
Christian Heimes53876d92008-04-19 00:31:39 +0000460
Eric Smith3ab08ca2010-12-04 15:17:38 +0000461
462 @requires_IEEE_754
463 def test_abs_overflows(self):
Christian Heimes53876d92008-04-19 00:31:39 +0000464 # result overflows
Eric Smith3ab08ca2010-12-04 15:17:38 +0000465 self.assertRaises(OverflowError, abs, complex(1.4e308, 1.4e308))
Christian Heimes53876d92008-04-19 00:31:39 +0000466
467 def assertCEqual(self, a, b):
468 eps = 1E-7
469 if abs(a.real - b[0]) > eps or abs(a.imag - b[1]) > eps:
470 self.fail((a ,b))
471
472 def test_rect(self):
473 self.assertCEqual(rect(0, 0), (0, 0))
474 self.assertCEqual(rect(1, 0), (1., 0))
475 self.assertCEqual(rect(1, -pi), (-1., 0))
476 self.assertCEqual(rect(1, pi/2), (0, 1.))
477 self.assertCEqual(rect(1, -pi/2), (0, -1.))
478
Mark Dickinson8e0c9962010-07-11 17:38:24 +0000479 def test_isfinite(self):
480 real_vals = [float('-inf'), -2.3, -0.0,
481 0.0, 2.3, float('inf'), float('nan')]
482 for x in real_vals:
483 for y in real_vals:
484 z = complex(x, y)
Mark Dickinson68c5de62010-07-11 19:12:10 +0000485 self.assertEqual(cmath.isfinite(z),
Mark Dickinson8e0c9962010-07-11 17:38:24 +0000486 math.isfinite(x) and math.isfinite(y))
487
Christian Heimes53876d92008-04-19 00:31:39 +0000488 def test_isnan(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000489 self.assertFalse(cmath.isnan(1))
490 self.assertFalse(cmath.isnan(1j))
491 self.assertFalse(cmath.isnan(INF))
492 self.assertTrue(cmath.isnan(NAN))
493 self.assertTrue(cmath.isnan(complex(NAN, 0)))
494 self.assertTrue(cmath.isnan(complex(0, NAN)))
495 self.assertTrue(cmath.isnan(complex(NAN, NAN)))
496 self.assertTrue(cmath.isnan(complex(NAN, INF)))
497 self.assertTrue(cmath.isnan(complex(INF, NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +0000498
499 def test_isinf(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000500 self.assertFalse(cmath.isinf(1))
501 self.assertFalse(cmath.isinf(1j))
502 self.assertFalse(cmath.isinf(NAN))
503 self.assertTrue(cmath.isinf(INF))
504 self.assertTrue(cmath.isinf(complex(INF, 0)))
505 self.assertTrue(cmath.isinf(complex(0, INF)))
506 self.assertTrue(cmath.isinf(complex(INF, INF)))
507 self.assertTrue(cmath.isinf(complex(NAN, INF)))
508 self.assertTrue(cmath.isinf(complex(INF, NAN)))
Christian Heimes53876d92008-04-19 00:31:39 +0000509
Victor Stinnerbe3da382010-11-07 14:14:27 +0000510 @requires_IEEE_754
511 @unittest.skipIf(sysconfig.get_config_var('TANH_PRESERVES_ZERO_SIGN') == 0,
512 "system tanh() function doesn't copy the sign")
513 def testTanhSign(self):
Mark Dickinson5ccafba2010-11-13 10:43:40 +0000514 for z in complex_zeros:
515 self.assertComplexIdentical(cmath.tanh(z), z)
Victor Stinnerbe3da382010-11-07 14:14:27 +0000516
Mark Dickinson4ccc1372010-11-20 11:08:27 +0000517 # The algorithm used for atan and atanh makes use of the system
518 # log1p function; If that system function doesn't respect the sign
519 # of zero, then atan and atanh will also have difficulties with
520 # the sign of complex zeros.
521 @requires_IEEE_754
522 @unittest.skipIf(sysconfig.get_config_var('LOG1P_DROPS_ZERO_SIGN'),
523 "system log1p() function doesn't preserve the sign")
524 def testAtanSign(self):
525 for z in complex_zeros:
526 self.assertComplexIdentical(cmath.atan(z), z)
527
528 @requires_IEEE_754
529 @unittest.skipIf(sysconfig.get_config_var('LOG1P_DROPS_ZERO_SIGN'),
530 "system log1p() function doesn't preserve the sign")
531 def testAtanhSign(self):
532 for z in complex_zeros:
533 self.assertComplexIdentical(cmath.atanh(z), z)
534
Guido van Rossumd8faa362007-04-27 19:54:29 +0000535
536def test_main():
537 run_unittest(CMathTests)
538
539if __name__ == "__main__":
540 test_main()