annotate Lib/test/test_cmath.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents d8562002f8e5
children b36cb4602e21
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
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
4 import os, sys
32350
11c31a98dfd6 Patch #826074: cmath.log optional base argument, fixes #823209
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
5 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
6 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
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
8 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
9 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
10
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_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
12 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
13 (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
14 (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
15 (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
16 (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
17 (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
18 (-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
19 (-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
20 (-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
21 (-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
22 (-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
23 (-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
24 (-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
25 (-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
26 (-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
27 (-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
28 (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
29 (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
30 (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
31 (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
32 (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
33 ]]
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 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
35 (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
36 (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
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, 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
39 (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
40 (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
41 (-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
42 (-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
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 (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
45 (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
46 (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
47 ]]
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
48
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
49 def almostEqualF(a, b, rel_err=2e-15, 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
50 """Determine whether floating-point values a and b are equal to within
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
51 a (small) rounding error. The default values for rel_err and
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
52 abs_err are chosen to be suitable for platforms where a float is
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
53 represented by an IEEE 754 double. They allow an error of between
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
54 9 and 19 ulps."""
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
55
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
56 # 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
57 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
58 return 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
59 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
60 return 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
61
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 # if both a and b are zero, check whether they have the same sign
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 # (in theory there are examples where it would be legitimate for 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
64 # and b to have opposite signs; in practice these hardly ever
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
65 # occur).
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
66 if not a and not 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
67 return math.copysign(1., a) == math.copysign(1., 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
68
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
69 # if a-b overflows, or b is infinite, return False. Again, 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
70 # theory there are examples where a is within a few ulps of the
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
71 # max representable float, and then b could legitimately be
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
72 # infinite. In practice these examples are rare.
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
73 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
74 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
75 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
76 return False
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 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
78 return absolute_error <= max(abs_err, rel_err * abs(a))
41219
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
79
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
80 class CMathTests(unittest.TestCase):
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
81 # 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
82 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
83 '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
84 '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
85 'sqrt', 'tan', 'tanh']]
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
86 # 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
87 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
88 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
89
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
90 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
91 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
92
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
93 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
94 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
95
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
96 def rAssertAlmostEqual(self, a, b, rel_err = 2e-15, 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
97 """Check that two floating-point numbers are almost equal."""
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
98
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
99 # 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
100 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
101 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
102 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
103 self.fail("%s should be nan" % repr(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
104
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
105 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
106 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
107 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
108 self.fail("finite result where infinity excpected: "
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 "expected %s, got %s" % (repr(a), repr(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
110
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
111 if not a and not 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
112 if math.atan2(a, -1.) != math.atan2(b, -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
113 self.fail("zero has wrong sign: expected %s, got %s" %
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
114 (repr(a), repr(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
115
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
116 # test passes if either the absolute error or the relative
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
117 # error is sufficiently small. The defaults amount to 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
118 # error of between 9 ulps and 19 ulps on an IEEE-754 compliant
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
119 # machine.
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
120
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
121 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
122 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
123 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
124 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
125 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
126 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
127 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
128 self.fail("%s and %s are not sufficiently close" % (repr(a), repr(b)))
41219
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
129
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
130 def test_constants(self):
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
131 e_expected = 2.71828182845904523536
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
132 pi_expected = 3.14159265358979323846
57833
d8562002f8e5 Merged revisions 76941 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57831
diff changeset
133 self.assertAlmostEqual(cmath.pi, pi_expected, places=9,
d8562002f8e5 Merged revisions 76941 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57831
diff changeset
134 msg="cmath.pi is %s; should be %s" % (cmath.pi, pi_expected))
d8562002f8e5 Merged revisions 76941 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57831
diff changeset
135 self.assertAlmostEqual(cmath.e, e_expected, places=9,
d8562002f8e5 Merged revisions 76941 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57831
diff changeset
136 msg="cmath.e is %s; should be %s" % (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
137
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
138 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
139 # 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
140 # functions
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
141
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
142 # 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
143 # 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
144 # (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
145 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
146 flt_arg = -6.131677725
32350
11c31a98dfd6 Patch #826074: cmath.log optional base argument, fixes #823209
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
147
41219
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
148 # 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
149 # 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
150 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
151 object(), NotImplemented]
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
152
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
153 # 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
154 # 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
155
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
156 # 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
157 class MyComplex(object):
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
158 def __init__(self, value):
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
159 self.value = value
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
160 def __complex__(self):
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
161 return self.value
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
162
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
163 # 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
164 class MyComplexOS:
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
165 def __init__(self, value):
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
166 self.value = value
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
167 def __complex__(self):
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
168 return self.value
32350
11c31a98dfd6 Patch #826074: cmath.log optional base argument, fixes #823209
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
169
41219
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
170 # 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
171 class SomeException(Exception):
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
172 pass
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
173 class MyComplexException(object):
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
174 def __complex__(self):
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
175 raise SomeException
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
176 class MyComplexExceptionOS:
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
177 def __complex__(self):
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
178 raise SomeException
32350
11c31a98dfd6 Patch #826074: cmath.log optional base argument, fixes #823209
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
179
41219
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
180 # 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
181 class NeitherComplexNorFloat(object):
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
182 pass
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
183 class NeitherComplexNorFloatOS:
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
184 pass
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
185 class MyInt(object):
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
186 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
187 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
188 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
189 class MyIntOS:
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
190 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
191 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
192 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
193
41219
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
194 # 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
195 # that should work
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
196 class FloatAndComplex(object):
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
197 def __float__(self):
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
198 return flt_arg
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
199 def __complex__(self):
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
200 return cx_arg
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
201 class FloatAndComplexOS:
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
202 def __float__(self):
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
203 return flt_arg
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
204 def __complex__(self):
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
205 return cx_arg
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
206 class JustFloat(object):
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
207 def __float__(self):
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
208 return flt_arg
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
209 class JustFloatOS:
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
210 def __float__(self):
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
211 return flt_arg
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
212
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
213 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
214 # 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
215 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
216 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
217 # 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
218 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
219 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
220 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
221 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
222 # 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
223 # 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
224 # __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
225 # 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
226 # 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
227 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
228 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
229 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
230 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
231 # 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
232 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
233 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
234 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
235 # 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
236 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
237 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
238
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
239 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
240 # 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
241 # 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
242 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
243 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
244 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
245
41219
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
246 # 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
247 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
248 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
249 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
250
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
251 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
252 # 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
253 # 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
254
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
255 # 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
256 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
257
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
258 # 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
259 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
260 [0., 1., -1.]
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
261
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
262 # 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
263 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
264 nonnegative = [0.] + positive
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
265
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
266 # 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
267 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
268
41219
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
269 test_functions = {
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
270 'acos' : unit_interval,
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
271 'asin' : unit_interval,
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
272 'atan' : real_line,
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
273 'cos' : real_line,
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
274 'cosh' : real_line,
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
275 'exp' : real_line,
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
276 'log' : positive,
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
277 'log10' : positive,
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
278 'sin' : real_line,
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
279 'sinh' : real_line,
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
280 'sqrt' : nonnegative,
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
281 'tan' : real_line,
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
282 'tanh' : real_line}
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
283
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
284 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
285 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
286 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
287 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
288 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
289 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
290 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
291
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
292 # 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
293 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
294 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
295 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
296 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
297 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
298
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 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
300 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
301 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
302
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 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
304 """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
305 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
306 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
307
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 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
309 """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
310 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
311 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
312
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
313 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
314 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
315 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
316 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
317 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
318 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
319 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
320 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
321 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
322 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
323 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
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 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
326 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
327 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
328 test_str = "%s: %s(complex(%r, %r))" % (id, fn, 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
329 self.fail('ValueError not raised in test %s' % test_str)
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
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 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
332 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
333 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
334 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
335 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
336 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
337 test_str = "%s: %s(complex(%r, %r))" % (id, fn, 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
338 self.fail('OverflowError not raised in test %s' % test_str)
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
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
340 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
341
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
342 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
343 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
344 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
345 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
346 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
347 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
348
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
349 # 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
350 # 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
351 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
352 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
353 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
354 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
355
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 not (almostEqualF(expected.real, actual.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
357 abs_err = real_abs_err) and
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 almostEqualF(expected.imag, 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
359 error_message = (
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 "%s: %s(complex(%r, %r))\n" % (id, fn, 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
361 "Expected: complex(%r, %r)\n" %
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 (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
363 "Received: complex(%r, %r)\n" %
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 (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
365 "Received value insufficiently close to expected value.")
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 self.fail(error_message)
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
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 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
369 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
370 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
371 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
372
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 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
374 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
375 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
376 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
377 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
378 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
379
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 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
381 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
382 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
383 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
384 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
385 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
386 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
387 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
388
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 # 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
390 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
391 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
392 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
393 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
394
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 # 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
396 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
397 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
398 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
399 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
400 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
401 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
402 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
403 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
404 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
405 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
406 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
407 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
408 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
409 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
410 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
411 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
412 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
413 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
414 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
415 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
416
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 # 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
418 for z in complex_nans:
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
419 self.assert_(math.isnan(phase(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
420
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
421 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
422 # 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
423 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
424 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
425
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
426 # 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
427 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
428 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
429
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 # 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
431 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
432 self.assert_(math.isnan(abs(complex(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
433 self.assert_(math.isnan(abs(complex(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
434 self.assert_(math.isnan(abs(complex(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
435 self.assert_(math.isnan(abs(complex(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
436 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
437 self.assertEqual(abs(complex(-INF, 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
438 self.assert_(math.isnan(abs(complex(-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
439 self.assert_(math.isnan(abs(complex(-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
440 self.assert_(math.isnan(abs(complex(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
441 self.assert_(math.isnan(abs(complex(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
442 self.assertEqual(abs(complex(INF, 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
443 self.assert_(math.isnan(abs(complex(NAN, 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
444
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 # 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
446 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
447 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
448
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
449 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
450 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
451 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
452 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
453
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
454 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
455 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
456 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
457 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
458 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
459 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
460
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
461 def test_isnan(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
462 self.failIf(cmath.isnan(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
463 self.failIf(cmath.isnan(1j))
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
464 self.failIf(cmath.isnan(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
465 self.assert_(cmath.isnan(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
466 self.assert_(cmath.isnan(complex(NAN, 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
467 self.assert_(cmath.isnan(complex(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
468 self.assert_(cmath.isnan(complex(NAN, 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
469 self.assert_(cmath.isnan(complex(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
470 self.assert_(cmath.isnan(complex(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
471
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
472 def test_isinf(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
473 self.failIf(cmath.isinf(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
474 self.failIf(cmath.isinf(1j))
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
475 self.failIf(cmath.isinf(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
476 self.assert_(cmath.isinf(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
477 self.assert_(cmath.isinf(complex(INF, 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
478 self.assert_(cmath.isinf(complex(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
479 self.assert_(cmath.isinf(complex(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
480 self.assert_(cmath.isinf(complex(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
481 self.assert_(cmath.isinf(complex(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
482
41219
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
483
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
484 def test_main():
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
485 run_unittest(CMathTests)
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
486
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
487 if __name__ == "__main__":
ebe52dbc3553 Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents: 32350
diff changeset
488 test_main()