Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_cmath.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | b36cb4602e21 |
| children |
| rev | line source |
|---|---|
|
41219
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
1 from test.test_support import run_unittest |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
2 from test.test_math import parse_testfile, test_file |
|
41219
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
3 import unittest |
|
32350
11c31a98dfd6
Patch #826074: cmath.log optional base argument, fixes #823209
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
4 import cmath, math |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
5 from cmath import phase, polar, rect, pi |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
6 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
7 INF = float('inf') |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
8 NAN = float('nan') |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
9 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
10 complex_zeros = [complex(x, y) for x in [0.0, -0.0] for y in [0.0, -0.0]] |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
11 complex_infinities = [complex(x, y) for x, y in [ |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
12 (INF, 0.0), # 1st quadrant |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
13 (INF, 2.3), |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
14 (INF, INF), |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
15 (2.3, INF), |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
16 (0.0, INF), |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
17 (-0.0, INF), # 2nd quadrant |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
18 (-2.3, INF), |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
19 (-INF, INF), |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
20 (-INF, 2.3), |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
21 (-INF, 0.0), |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
22 (-INF, -0.0), # 3rd quadrant |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
23 (-INF, -2.3), |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
24 (-INF, -INF), |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
25 (-2.3, -INF), |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
26 (-0.0, -INF), |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
27 (0.0, -INF), # 4th quadrant |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
28 (2.3, -INF), |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
29 (INF, -INF), |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
30 (INF, -2.3), |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
31 (INF, -0.0) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
32 ]] |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
33 complex_nans = [complex(x, y) for x, y in [ |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
34 (NAN, -INF), |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
35 (NAN, -2.3), |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
36 (NAN, -0.0), |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
37 (NAN, 0.0), |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
38 (NAN, 2.3), |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
39 (NAN, INF), |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
40 (-INF, NAN), |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
41 (-2.3, NAN), |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
42 (-0.0, NAN), |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
43 (0.0, NAN), |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
44 (2.3, NAN), |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
45 (INF, NAN) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
46 ]] |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
47 |
|
41219
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
48 class CMathTests(unittest.TestCase): |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
49 # list of all functions in cmath |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
50 test_functions = [getattr(cmath, fname) for fname in [ |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
51 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atanh', |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
52 'cos', 'cosh', 'exp', 'log', 'log10', 'sin', 'sinh', |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
53 'sqrt', 'tan', 'tanh']] |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
54 # test first and second arguments independently for 2-argument log |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
55 test_functions.append(lambda x : cmath.log(x, 1729. + 0j)) |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
56 test_functions.append(lambda x : cmath.log(14.-27j, x)) |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
57 |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
58 def setUp(self): |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
59 self.test_values = open(test_file) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
60 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
61 def tearDown(self): |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
62 self.test_values.close() |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
63 |
|
57839
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
64 def rAssertAlmostEqual(self, a, b, rel_err = 2e-15, abs_err = 5e-323, |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
65 msg=None): |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
66 """Fail if the two floating-point numbers are not almost equal. |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
67 |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
68 Determine whether floating-point values a and b are equal to within |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
69 a (small) rounding error. The default values for rel_err and |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
70 abs_err are chosen to be suitable for platforms where a float is |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
71 represented by an IEEE 754 double. They allow an error of between |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
72 9 and 19 ulps. |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
73 """ |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
74 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
75 # special values testing |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
76 if math.isnan(a): |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
77 if math.isnan(b): |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
78 return |
|
57839
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
79 self.fail(msg or '{!r} should be nan'.format(b)) |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
80 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
81 if math.isinf(a): |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
82 if a == b: |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
83 return |
|
57839
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
84 self.fail(msg or 'finite result where infinity expected: ' |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
85 'expected {!r}, got {!r}'.format(a, b)) |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
86 |
|
57839
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
87 # if both a and b are zero, check whether they have the same sign |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
88 # (in theory there are examples where it would be legitimate for a |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
89 # and b to have opposite signs; in practice these hardly ever |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
90 # occur). |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
91 if not a and not b: |
|
57839
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
92 if math.copysign(1., a) != math.copysign(1., b): |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
93 self.fail(msg or 'zero has wrong sign: expected {!r}, ' |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
94 'got {!r}'.format(a, b)) |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
95 |
|
57839
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
96 # if a-b overflows, or b is infinite, return False. Again, in |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
97 # theory there are examples where a is within a few ulps of the |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
98 # max representable float, and then b could legitimately be |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
99 # infinite. In practice these examples are rare. |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
100 try: |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
101 absolute_error = abs(b-a) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
102 except OverflowError: |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
103 pass |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
104 else: |
|
57839
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
105 # test passes if either the absolute error or the relative |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
106 # error is sufficiently small. The defaults amount to an |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
107 # error of between 9 ulps and 19 ulps on an IEEE-754 compliant |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
108 # machine. |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
109 if absolute_error <= max(abs_err, rel_err * abs(a)): |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
110 return |
|
57839
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
111 self.fail(msg or |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
112 '{!r} and {!r} are not sufficiently close'.format(a, b)) |
|
41219
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
113 |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
114 def test_constants(self): |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
115 e_expected = 2.71828182845904523536 |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
116 pi_expected = 3.14159265358979323846 |
|
57833
d8562002f8e5
Merged revisions 76941 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
57831
diff
changeset
|
117 self.assertAlmostEqual(cmath.pi, pi_expected, places=9, |
|
57839
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
118 msg="cmath.pi is {}; should be {}".format(cmath.pi, pi_expected)) |
|
57833
d8562002f8e5
Merged revisions 76941 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
57831
diff
changeset
|
119 self.assertAlmostEqual(cmath.e, e_expected, places=9, |
|
57839
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
120 msg="cmath.e is {}; should be {}".format(cmath.e, e_expected)) |
|
41219
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
121 |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
122 def test_user_object(self): |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
123 # Test automatic calling of __complex__ and __float__ by cmath |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
124 # functions |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
125 |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
126 # some random values to use as test values; we avoid values |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
127 # for which any of the functions in cmath is undefined |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
128 # (i.e. 0., 1., -1., 1j, -1j) or would cause overflow |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
129 cx_arg = 4.419414439 + 1.497100113j |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
130 flt_arg = -6.131677725 |
|
32350
11c31a98dfd6
Patch #826074: cmath.log optional base argument, fixes #823209
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
131 |
|
41219
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
132 # a variety of non-complex numbers, used to check that |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
133 # non-complex return values from __complex__ give an error |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
134 non_complexes = ["not complex", 1, 5L, 2., None, |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
135 object(), NotImplemented] |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
136 |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
137 # Now we introduce a variety of classes whose instances might |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
138 # end up being passed to the cmath functions |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
139 |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
140 # usual case: new-style class implementing __complex__ |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
141 class MyComplex(object): |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
142 def __init__(self, value): |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
143 self.value = value |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
144 def __complex__(self): |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
145 return self.value |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
146 |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
147 # old-style class implementing __complex__ |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
148 class MyComplexOS: |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
149 def __init__(self, value): |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
150 self.value = value |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
151 def __complex__(self): |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
152 return self.value |
|
32350
11c31a98dfd6
Patch #826074: cmath.log optional base argument, fixes #823209
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
153 |
|
41219
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
154 # classes for which __complex__ raises an exception |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
155 class SomeException(Exception): |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
156 pass |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
157 class MyComplexException(object): |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
158 def __complex__(self): |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
159 raise SomeException |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
160 class MyComplexExceptionOS: |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
161 def __complex__(self): |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
162 raise SomeException |
|
32350
11c31a98dfd6
Patch #826074: cmath.log optional base argument, fixes #823209
Raymond Hettinger <python@rcn.com>
parents:
24364
diff
changeset
|
163 |
|
41219
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
164 # some classes not providing __float__ or __complex__ |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
165 class NeitherComplexNorFloat(object): |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
166 pass |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
167 class NeitherComplexNorFloatOS: |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
168 pass |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
169 class MyInt(object): |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
170 def __int__(self): return 2 |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
171 def __long__(self): return 2L |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
172 def __index__(self): return 2 |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
173 class MyIntOS: |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
174 def __int__(self): return 2 |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
175 def __long__(self): return 2L |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
176 def __index__(self): return 2 |
|
4638
5f7d246bbeb7
New test module for complex math module
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff
changeset
|
177 |
|
41219
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
178 # other possible combinations of __float__ and __complex__ |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
179 # that should work |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
180 class FloatAndComplex(object): |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
181 def __float__(self): |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
182 return flt_arg |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
183 def __complex__(self): |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
184 return cx_arg |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
185 class FloatAndComplexOS: |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
186 def __float__(self): |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
187 return flt_arg |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
188 def __complex__(self): |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
189 return cx_arg |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
190 class JustFloat(object): |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
191 def __float__(self): |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
192 return flt_arg |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
193 class JustFloatOS: |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
194 def __float__(self): |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
195 return flt_arg |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
196 |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
197 for f in self.test_functions: |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
198 # usual usage |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
199 self.assertEqual(f(MyComplex(cx_arg)), f(cx_arg)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
200 self.assertEqual(f(MyComplexOS(cx_arg)), f(cx_arg)) |
|
41219
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
201 # other combinations of __float__ and __complex__ |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
202 self.assertEqual(f(FloatAndComplex()), f(cx_arg)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
203 self.assertEqual(f(FloatAndComplexOS()), f(cx_arg)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
204 self.assertEqual(f(JustFloat()), f(flt_arg)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
205 self.assertEqual(f(JustFloatOS()), f(flt_arg)) |
|
41219
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
206 # TypeError should be raised for classes not providing |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
207 # either __complex__ or __float__, even if they provide |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
208 # __int__, __long__ or __index__. An old-style class |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
209 # currently raises AttributeError instead of a TypeError; |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
210 # this could be considered a bug. |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
211 self.assertRaises(TypeError, f, NeitherComplexNorFloat()) |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
212 self.assertRaises(TypeError, f, MyInt()) |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
213 self.assertRaises(Exception, f, NeitherComplexNorFloatOS()) |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
214 self.assertRaises(Exception, f, MyIntOS()) |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
215 # non-complex return value from __complex__ -> TypeError |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
216 for bad_complex in non_complexes: |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
217 self.assertRaises(TypeError, f, MyComplex(bad_complex)) |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
218 self.assertRaises(TypeError, f, MyComplexOS(bad_complex)) |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
219 # exceptions in __complex__ should be propagated correctly |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
220 self.assertRaises(SomeException, f, MyComplexException()) |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
221 self.assertRaises(SomeException, f, MyComplexExceptionOS()) |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
222 |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
223 def test_input_type(self): |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
224 # ints and longs should be acceptable inputs to all cmath |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
225 # functions, by virtue of providing a __float__ method |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
226 for f in self.test_functions: |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
227 for arg in [2, 2L, 2.]: |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
228 self.assertEqual(f(arg), f(arg.__float__())) |
|
4811
9ae3cccfa7cd
Many scripts, but small changes. Update the way the scripts obtain the
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4638
diff
changeset
|
229 |
|
41219
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
230 # but strings should give a TypeError |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
231 for f in self.test_functions: |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
232 for arg in ["a", "long_string", "0", "1j", ""]: |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
233 self.assertRaises(TypeError, f, arg) |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
234 |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
235 def test_cmath_matches_math(self): |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
236 # check that corresponding cmath and math functions are equal |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
237 # for floats in the appropriate range |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
238 |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
239 # test_values in (0, 1) |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
240 test_values = [0.01, 0.1, 0.2, 0.5, 0.9, 0.99] |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
241 |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
242 # test_values for functions defined on [-1., 1.] |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
243 unit_interval = test_values + [-x for x in test_values] + \ |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
244 [0., 1., -1.] |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
245 |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
246 # test_values for log, log10, sqrt |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
247 positive = test_values + [1.] + [1./x for x in test_values] |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
248 nonnegative = [0.] + positive |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
249 |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
250 # test_values for functions defined on the whole real line |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
251 real_line = [0.] + positive + [-x for x in positive] |
|
4811
9ae3cccfa7cd
Many scripts, but small changes. Update the way the scripts obtain the
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
4638
diff
changeset
|
252 |
|
41219
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
253 test_functions = { |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
254 'acos' : unit_interval, |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
255 'asin' : unit_interval, |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
256 'atan' : real_line, |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
257 'cos' : real_line, |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
258 'cosh' : real_line, |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
259 'exp' : real_line, |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
260 'log' : positive, |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
261 'log10' : positive, |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
262 'sin' : real_line, |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
263 'sinh' : real_line, |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
264 'sqrt' : nonnegative, |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
265 'tan' : real_line, |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
266 'tanh' : real_line} |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
267 |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
268 for fn, values in test_functions.items(): |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
269 float_fn = getattr(math, fn) |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
270 complex_fn = getattr(cmath, fn) |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
271 for v in values: |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
272 z = complex_fn(v) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
273 self.rAssertAlmostEqual(float_fn(v), z.real) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
274 self.assertEqual(0., z.imag) |
|
41219
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
275 |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
276 # test two-argument version of log with various bases |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
277 for base in [0.5, 2., 10.]: |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
278 for v in positive: |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
279 z = cmath.log(v, base) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
280 self.rAssertAlmostEqual(math.log(v, base), z.real) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
281 self.assertEqual(0., z.imag) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
282 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
283 def test_specific_values(self): |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
284 if not float.__getformat__("double").startswith("IEEE"): |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
285 return |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
286 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
287 def rect_complex(z): |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
288 """Wrapped version of rect that accepts a complex number instead of |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
289 two float arguments.""" |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
290 return cmath.rect(z.real, z.imag) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
291 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
292 def polar_complex(z): |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
293 """Wrapped version of polar that returns a complex number instead of |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
294 two floats.""" |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
295 return complex(*polar(z)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
296 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
297 for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file): |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
298 arg = complex(ar, ai) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
299 expected = complex(er, ei) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
300 if fn == 'rect': |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
301 function = rect_complex |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
302 elif fn == 'polar': |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
303 function = polar_complex |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
304 else: |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
305 function = getattr(cmath, fn) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
306 if 'divide-by-zero' in flags or 'invalid' in flags: |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
307 try: |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
308 actual = function(arg) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
309 except ValueError: |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
310 continue |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
311 else: |
|
57839
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
312 self.fail('ValueError not raised in test ' |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
313 '{}: {}(complex({!r}, {!r}))'.format(id, fn, ar, ai)) |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
314 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
315 if 'overflow' in flags: |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
316 try: |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
317 actual = function(arg) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
318 except OverflowError: |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
319 continue |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
320 else: |
|
57839
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
321 self.fail('OverflowError not raised in test ' |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
322 '{}: {}(complex({!r}, {!r}))'.format(id, fn, ar, ai)) |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
323 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
324 actual = function(arg) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
325 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
326 if 'ignore-real-sign' in flags: |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
327 actual = complex(abs(actual.real), actual.imag) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
328 expected = complex(abs(expected.real), expected.imag) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
329 if 'ignore-imag-sign' in flags: |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
330 actual = complex(actual.real, abs(actual.imag)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
331 expected = complex(expected.real, abs(expected.imag)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
332 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
333 # for the real part of the log function, we allow an |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
334 # absolute error of up to 2e-15. |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
335 if fn in ('log', 'log10'): |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
336 real_abs_err = 2e-15 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
337 else: |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
338 real_abs_err = 5e-323 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
339 |
|
57839
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
340 error_message = ( |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
341 '{}: {}(complex({!r}, {!r}))\n' |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
342 'Expected: complex({!r}, {!r})\n' |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
343 'Received: complex({!r}, {!r})\n' |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
344 'Received value insufficiently close to expected value.' |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
345 ).format(id, fn, ar, ai, |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
346 expected.real, expected.imag, |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
347 actual.real, actual.imag) |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
348 self.rAssertAlmostEqual(expected.real, actual.real, |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
349 abs_err=real_abs_err, |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
350 msg=error_message) |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
351 self.rAssertAlmostEqual(expected.imag, actual.imag, |
|
79717b7d379d
Issue #7554: Various fixups in test_cmath.py: remove code duplication,
Mark Dickinson <dickinsm@gmail.com>
parents:
57832
diff
changeset
|
352 msg=error_message) |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
353 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
354 def assertCISEqual(self, a, b): |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
355 eps = 1E-7 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
356 if abs(a[0] - b[0]) > eps or abs(a[1] - b[1]) > eps: |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
357 self.fail((a ,b)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
358 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
359 def test_polar(self): |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
360 self.assertCISEqual(polar(0), (0., 0.)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
361 self.assertCISEqual(polar(1.), (1., 0.)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
362 self.assertCISEqual(polar(-1.), (1., pi)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
363 self.assertCISEqual(polar(1j), (1., pi/2)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
364 self.assertCISEqual(polar(-1j), (1., -pi/2)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
365 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
366 def test_phase(self): |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
367 self.assertAlmostEqual(phase(0), 0.) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
368 self.assertAlmostEqual(phase(1.), 0.) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
369 self.assertAlmostEqual(phase(-1.), pi) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
370 self.assertAlmostEqual(phase(-1.+1E-300j), pi) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
371 self.assertAlmostEqual(phase(-1.-1E-300j), -pi) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
372 self.assertAlmostEqual(phase(1j), pi/2) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
373 self.assertAlmostEqual(phase(-1j), -pi/2) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
374 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
375 # zeros |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
376 self.assertEqual(phase(complex(0.0, 0.0)), 0.0) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
377 self.assertEqual(phase(complex(0.0, -0.0)), -0.0) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
378 self.assertEqual(phase(complex(-0.0, 0.0)), pi) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
379 self.assertEqual(phase(complex(-0.0, -0.0)), -pi) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
380 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
381 # infinities |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
382 self.assertAlmostEqual(phase(complex(-INF, -0.0)), -pi) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
383 self.assertAlmostEqual(phase(complex(-INF, -2.3)), -pi) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
384 self.assertAlmostEqual(phase(complex(-INF, -INF)), -0.75*pi) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
385 self.assertAlmostEqual(phase(complex(-2.3, -INF)), -pi/2) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
386 self.assertAlmostEqual(phase(complex(-0.0, -INF)), -pi/2) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
387 self.assertAlmostEqual(phase(complex(0.0, -INF)), -pi/2) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
388 self.assertAlmostEqual(phase(complex(2.3, -INF)), -pi/2) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
389 self.assertAlmostEqual(phase(complex(INF, -INF)), -pi/4) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
390 self.assertEqual(phase(complex(INF, -2.3)), -0.0) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
391 self.assertEqual(phase(complex(INF, -0.0)), -0.0) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
392 self.assertEqual(phase(complex(INF, 0.0)), 0.0) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
393 self.assertEqual(phase(complex(INF, 2.3)), 0.0) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
394 self.assertAlmostEqual(phase(complex(INF, INF)), pi/4) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
395 self.assertAlmostEqual(phase(complex(2.3, INF)), pi/2) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
396 self.assertAlmostEqual(phase(complex(0.0, INF)), pi/2) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
397 self.assertAlmostEqual(phase(complex(-0.0, INF)), pi/2) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
398 self.assertAlmostEqual(phase(complex(-2.3, INF)), pi/2) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
399 self.assertAlmostEqual(phase(complex(-INF, INF)), 0.75*pi) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
400 self.assertAlmostEqual(phase(complex(-INF, 2.3)), pi) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
401 self.assertAlmostEqual(phase(complex(-INF, 0.0)), pi) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
402 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
403 # real or imaginary part NaN |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
404 for z in complex_nans: |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
46473
diff
changeset
|
405 self.assertTrue(math.isnan(phase(z))) |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
406 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
407 def test_abs(self): |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
408 # zeros |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
409 for z in complex_zeros: |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
410 self.assertEqual(abs(z), 0.0) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
411 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
412 # infinities |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
413 for z in complex_infinities: |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
414 self.assertEqual(abs(z), INF) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
415 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
416 # real or imaginary part NaN |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
417 self.assertEqual(abs(complex(NAN, -INF)), INF) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
46473
diff
changeset
|
418 self.assertTrue(math.isnan(abs(complex(NAN, -2.3)))) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
46473
diff
changeset
|
419 self.assertTrue(math.isnan(abs(complex(NAN, -0.0)))) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
46473
diff
changeset
|
420 self.assertTrue(math.isnan(abs(complex(NAN, 0.0)))) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
46473
diff
changeset
|
421 self.assertTrue(math.isnan(abs(complex(NAN, 2.3)))) |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
422 self.assertEqual(abs(complex(NAN, INF)), INF) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
423 self.assertEqual(abs(complex(-INF, NAN)), INF) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
46473
diff
changeset
|
424 self.assertTrue(math.isnan(abs(complex(-2.3, NAN)))) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
46473
diff
changeset
|
425 self.assertTrue(math.isnan(abs(complex(-0.0, NAN)))) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
46473
diff
changeset
|
426 self.assertTrue(math.isnan(abs(complex(0.0, NAN)))) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
46473
diff
changeset
|
427 self.assertTrue(math.isnan(abs(complex(2.3, NAN)))) |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
428 self.assertEqual(abs(complex(INF, NAN)), INF) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
46473
diff
changeset
|
429 self.assertTrue(math.isnan(abs(complex(NAN, NAN)))) |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
430 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
431 # result overflows |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
432 if float.__getformat__("double").startswith("IEEE"): |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
433 self.assertRaises(OverflowError, abs, complex(1.4e308, 1.4e308)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
434 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
435 def assertCEqual(self, a, b): |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
436 eps = 1E-7 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
437 if abs(a.real - b[0]) > eps or abs(a.imag - b[1]) > eps: |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
438 self.fail((a ,b)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
439 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
440 def test_rect(self): |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
441 self.assertCEqual(rect(0, 0), (0, 0)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
442 self.assertCEqual(rect(1, 0), (1., 0)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
443 self.assertCEqual(rect(1, -pi), (-1., 0)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
444 self.assertCEqual(rect(1, pi/2), (0, 1.)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
445 self.assertCEqual(rect(1, -pi/2), (0, -1.)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
446 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
447 def test_isnan(self): |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
46473
diff
changeset
|
448 self.assertFalse(cmath.isnan(1)) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
46473
diff
changeset
|
449 self.assertFalse(cmath.isnan(1j)) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
46473
diff
changeset
|
450 self.assertFalse(cmath.isnan(INF)) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
46473
diff
changeset
|
451 self.assertTrue(cmath.isnan(NAN)) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
46473
diff
changeset
|
452 self.assertTrue(cmath.isnan(complex(NAN, 0))) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
46473
diff
changeset
|
453 self.assertTrue(cmath.isnan(complex(0, NAN))) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
46473
diff
changeset
|
454 self.assertTrue(cmath.isnan(complex(NAN, NAN))) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
46473
diff
changeset
|
455 self.assertTrue(cmath.isnan(complex(NAN, INF))) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
46473
diff
changeset
|
456 self.assertTrue(cmath.isnan(complex(INF, NAN))) |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
457 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
458 def test_isinf(self): |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
46473
diff
changeset
|
459 self.assertFalse(cmath.isinf(1)) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
46473
diff
changeset
|
460 self.assertFalse(cmath.isinf(1j)) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
46473
diff
changeset
|
461 self.assertFalse(cmath.isinf(NAN)) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
46473
diff
changeset
|
462 self.assertTrue(cmath.isinf(INF)) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
46473
diff
changeset
|
463 self.assertTrue(cmath.isinf(complex(INF, 0))) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
46473
diff
changeset
|
464 self.assertTrue(cmath.isinf(complex(0, INF))) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
46473
diff
changeset
|
465 self.assertTrue(cmath.isinf(complex(INF, INF))) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
46473
diff
changeset
|
466 self.assertTrue(cmath.isinf(complex(NAN, INF))) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
46473
diff
changeset
|
467 self.assertTrue(cmath.isinf(complex(INF, NAN))) |
|
46473
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
41219
diff
changeset
|
468 |
|
41219
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
469 |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
470 def test_main(): |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
471 run_unittest(CMathTests) |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
472 |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
473 if __name__ == "__main__": |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
32350
diff
changeset
|
474 test_main() |
