Mercurial > cpython-lru_cache-weakref
annotate Objects/complexobject.c @ 49955:878d40468c4e legacy-trunk
Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
| author | Vinay Sajip <vinay_sajip@yahoo.co.uk> |
|---|---|
| date | Wed, 03 Dec 2008 23:22:58 +0000 |
| parents | 52294af4b663 |
| children | 587c8e3917c6 33312b910773 |
| rev | line source |
|---|---|
|
5559
1eced1aaad5a
Got rid of c_error in favor of errno (and EDOM/ERANGE).
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1 |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 /* Complex object implementation */ |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 /* Borrows heavily from floatobject.c */ |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 |
|
5559
1eced1aaad5a
Got rid of c_error in favor of errno (and EDOM/ERANGE).
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
6 /* Submitted by Jim Hugunin */ |
|
1eced1aaad5a
Got rid of c_error in favor of errno (and EDOM/ERANGE).
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
7 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
8 #include "Python.h" |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
9 #include "structmember.h" |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 |
|
46154
1e10b30f7498
Fix compiler warning about finite() missing on Solaris.
Neal Norwitz <nnorwitz@gmail.com>
parents:
45357
diff
changeset
|
11 #ifdef HAVE_IEEEFP_H |
|
1e10b30f7498
Fix compiler warning about finite() missing on Solaris.
Neal Norwitz <nnorwitz@gmail.com>
parents:
45357
diff
changeset
|
12 #include <ieeefp.h> |
|
1e10b30f7498
Fix compiler warning about finite() missing on Solaris.
Neal Norwitz <nnorwitz@gmail.com>
parents:
45357
diff
changeset
|
13 #endif |
|
1e10b30f7498
Fix compiler warning about finite() missing on Solaris.
Neal Norwitz <nnorwitz@gmail.com>
parents:
45357
diff
changeset
|
14 |
|
23820
130fff486fa6
Rearrange the #ifndef WITHOUT_COMPLEX so it can be picked up from
Guido van Rossum <guido@python.org>
parents:
23703
diff
changeset
|
15 #ifndef WITHOUT_COMPLEX |
|
130fff486fa6
Rearrange the #ifndef WITHOUT_COMPLEX so it can be picked up from
Guido van Rossum <guido@python.org>
parents:
23703
diff
changeset
|
16 |
|
17029
65a638a24f7e
When 1.6 boosted the # of digits produced by repr(float), repr(complex)
Tim Peters <tim.peters@gmail.com>
parents:
15976
diff
changeset
|
17 /* Precisions used by repr() and str(), respectively. |
|
65a638a24f7e
When 1.6 boosted the # of digits produced by repr(float), repr(complex)
Tim Peters <tim.peters@gmail.com>
parents:
15976
diff
changeset
|
18 |
|
65a638a24f7e
When 1.6 boosted the # of digits produced by repr(float), repr(complex)
Tim Peters <tim.peters@gmail.com>
parents:
15976
diff
changeset
|
19 The repr() precision (17 significant decimal digits) is the minimal number |
|
65a638a24f7e
When 1.6 boosted the # of digits produced by repr(float), repr(complex)
Tim Peters <tim.peters@gmail.com>
parents:
15976
diff
changeset
|
20 that is guaranteed to have enough precision so that if the number is read |
|
65a638a24f7e
When 1.6 boosted the # of digits produced by repr(float), repr(complex)
Tim Peters <tim.peters@gmail.com>
parents:
15976
diff
changeset
|
21 back in the exact same binary value is recreated. This is true for IEEE |
|
65a638a24f7e
When 1.6 boosted the # of digits produced by repr(float), repr(complex)
Tim Peters <tim.peters@gmail.com>
parents:
15976
diff
changeset
|
22 floating point by design, and also happens to work for all other modern |
|
65a638a24f7e
When 1.6 boosted the # of digits produced by repr(float), repr(complex)
Tim Peters <tim.peters@gmail.com>
parents:
15976
diff
changeset
|
23 hardware. |
|
65a638a24f7e
When 1.6 boosted the # of digits produced by repr(float), repr(complex)
Tim Peters <tim.peters@gmail.com>
parents:
15976
diff
changeset
|
24 |
|
65a638a24f7e
When 1.6 boosted the # of digits produced by repr(float), repr(complex)
Tim Peters <tim.peters@gmail.com>
parents:
15976
diff
changeset
|
25 The str() precision is chosen so that in most cases, the rounding noise |
|
65a638a24f7e
When 1.6 boosted the # of digits produced by repr(float), repr(complex)
Tim Peters <tim.peters@gmail.com>
parents:
15976
diff
changeset
|
26 created by various operations is suppressed, while giving plenty of |
|
65a638a24f7e
When 1.6 boosted the # of digits produced by repr(float), repr(complex)
Tim Peters <tim.peters@gmail.com>
parents:
15976
diff
changeset
|
27 precision for practical use. |
|
65a638a24f7e
When 1.6 boosted the # of digits produced by repr(float), repr(complex)
Tim Peters <tim.peters@gmail.com>
parents:
15976
diff
changeset
|
28 */ |
|
65a638a24f7e
When 1.6 boosted the # of digits produced by repr(float), repr(complex)
Tim Peters <tim.peters@gmail.com>
parents:
15976
diff
changeset
|
29 |
|
65a638a24f7e
When 1.6 boosted the # of digits produced by repr(float), repr(complex)
Tim Peters <tim.peters@gmail.com>
parents:
15976
diff
changeset
|
30 #define PREC_REPR 17 |
|
65a638a24f7e
When 1.6 boosted the # of digits produced by repr(float), repr(complex)
Tim Peters <tim.peters@gmail.com>
parents:
15976
diff
changeset
|
31 #define PREC_STR 12 |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 /* elementary operations on complex numbers */ |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 |
| 3771 | 35 static Py_complex c_1 = {1., 0.}; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
37 Py_complex |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
38 c_sum(Py_complex a, Py_complex b) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 { |
| 3771 | 40 Py_complex r; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 r.real = a.real + b.real; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 r.imag = a.imag + b.imag; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 return r; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
46 Py_complex |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
47 c_diff(Py_complex a, Py_complex b) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 { |
| 3771 | 49 Py_complex r; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 r.real = a.real - b.real; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 r.imag = a.imag - b.imag; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 return r; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
55 Py_complex |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
56 c_neg(Py_complex a) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 { |
| 3771 | 58 Py_complex r; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 r.real = -a.real; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 r.imag = -a.imag; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 return r; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
64 Py_complex |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
65 c_prod(Py_complex a, Py_complex b) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 { |
| 3771 | 67 Py_complex r; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 r.real = a.real*b.real - a.imag*b.imag; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 r.imag = a.real*b.imag + a.imag*b.real; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 return r; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
73 Py_complex |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
74 c_quot(Py_complex a, Py_complex b) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 { |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
76 /****************************************************************** |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
77 This was the original algorithm. It's grossly prone to spurious |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
78 overflow and underflow errors. It also merrily divides by 0 despite |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
79 checking for that(!). The code still serves a doc purpose here, as |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
80 the algorithm following is a simple by-cases transformation of this |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
81 one: |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
82 |
| 3771 | 83 Py_complex r; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 double d = b.real*b.real + b.imag*b.imag; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 if (d == 0.) |
|
5559
1eced1aaad5a
Got rid of c_error in favor of errno (and EDOM/ERANGE).
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
86 errno = EDOM; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
87 r.real = (a.real*b.real + a.imag*b.imag)/d; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 r.imag = (a.imag*b.real - a.real*b.imag)/d; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
89 return r; |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
90 ******************************************************************/ |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
91 |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
92 /* This algorithm is better, and is pretty obvious: first divide the |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
93 * numerators and denominator by whichever of {b.real, b.imag} has |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
94 * larger magnitude. The earliest reference I found was to CACM |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
95 * Algorithm 116 (Complex Division, Robert L. Smith, Stanford |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
96 * University). As usual, though, we're still ignoring all IEEE |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
97 * endcases. |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
98 */ |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
99 Py_complex r; /* the result */ |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
100 const double abs_breal = b.real < 0 ? -b.real : b.real; |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
101 const double abs_bimag = b.imag < 0 ? -b.imag : b.imag; |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
102 |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
103 if (abs_breal >= abs_bimag) { |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
104 /* divide tops and bottom by b.real */ |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
105 if (abs_breal == 0.0) { |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
106 errno = EDOM; |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
107 r.real = r.imag = 0.0; |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
108 } |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
109 else { |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
110 const double ratio = b.imag / b.real; |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
111 const double denom = b.real + b.imag * ratio; |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
112 r.real = (a.real + a.imag * ratio) / denom; |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
113 r.imag = (a.imag - a.real * ratio) / denom; |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
114 } |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
115 } |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
116 else { |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
117 /* divide tops and bottom by b.imag */ |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
118 const double ratio = b.real / b.imag; |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
119 const double denom = b.real * ratio + b.imag; |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
120 assert(b.imag != 0.0); |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
121 r.real = (a.real * ratio + a.imag) / denom; |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
122 r.imag = (a.imag * ratio - a.real) / denom; |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
123 } |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
124 return r; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
125 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
127 Py_complex |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
128 c_pow(Py_complex a, Py_complex b) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 { |
| 3771 | 130 Py_complex r; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
131 double vabs,len,at,phase; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
132 if (b.real == 0. && b.imag == 0.) { |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 r.real = 1.; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 r.imag = 0.; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 else if (a.real == 0. && a.imag == 0.) { |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 if (b.imag != 0. || b.real < 0.) |
|
22540
75ef1f16725e
backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents:
21894
diff
changeset
|
138 errno = EDOM; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 r.real = 0.; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 r.imag = 0.; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 else { |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
143 vabs = hypot(a.real,a.imag); |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 len = pow(vabs,b.real); |
|
19758
64bcef1702ad
Revert parts of patch #453627, documenting the resulting test failures
Martin v. Löwis <martin@v.loewis.de>
parents:
19730
diff
changeset
|
145 at = atan2(a.imag, a.real); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 phase = at*b.real; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 if (b.imag != 0.0) { |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 len /= exp(at*b.imag); |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 phase += b.imag*log(vabs); |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
150 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 r.real = len*cos(phase); |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 r.imag = len*sin(phase); |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 return r; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
157 static Py_complex |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
158 c_powu(Py_complex x, long n) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 { |
|
4025
854f25fde8f4
Changes to make the file acceptable to K&R C compilers (HPUX, SunOS 4.x).
Guido van Rossum <guido@python.org>
parents:
3771
diff
changeset
|
160 Py_complex r, p; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
161 long mask = 1; |
|
4025
854f25fde8f4
Changes to make the file acceptable to K&R C compilers (HPUX, SunOS 4.x).
Guido van Rossum <guido@python.org>
parents:
3771
diff
changeset
|
162 r = c_1; |
|
854f25fde8f4
Changes to make the file acceptable to K&R C compilers (HPUX, SunOS 4.x).
Guido van Rossum <guido@python.org>
parents:
3771
diff
changeset
|
163 p = x; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
164 while (mask > 0 && n >= mask) { |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
165 if (n & mask) |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
166 r = c_prod(r,p); |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
167 mask <<= 1; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
168 p = c_prod(p,p); |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
169 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
170 return r; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
171 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
173 static Py_complex |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
174 c_powi(Py_complex x, long n) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
175 { |
| 3771 | 176 Py_complex cn; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
177 |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
178 if (n > 100 || n < -100) { |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 cn.real = (double) n; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 cn.imag = 0.; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 return c_pow(x,cn); |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
182 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
183 else if (n > 0) |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
184 return c_powu(x,n); |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
185 else |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
186 return c_quot(c_1,c_powu(x,-n)); |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
187 |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
188 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
189 |
|
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:
46154
diff
changeset
|
190 double |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
191 c_abs(Py_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:
46154
diff
changeset
|
192 { |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
193 /* sets errno = ERANGE on overflow; otherwise errno = 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:
46154
diff
changeset
|
194 double result; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
195 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
196 if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(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:
46154
diff
changeset
|
197 /* C99 rules: if either the real or the imaginary part is 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:
46154
diff
changeset
|
198 infinity, return infinity, even if the other part is 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:
46154
diff
changeset
|
199 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:
46154
diff
changeset
|
200 if (Py_IS_INFINITY(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:
46154
diff
changeset
|
201 result = fabs(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:
46154
diff
changeset
|
202 errno = 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:
46154
diff
changeset
|
203 return result; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
204 } |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
205 if (Py_IS_INFINITY(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:
46154
diff
changeset
|
206 result = fabs(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:
46154
diff
changeset
|
207 errno = 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:
46154
diff
changeset
|
208 return result; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
209 } |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
210 /* either the real or imaginary part is a 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:
46154
diff
changeset
|
211 and neither is infinite. Result should be 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:
46154
diff
changeset
|
212 return Py_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:
46154
diff
changeset
|
213 } |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
214 result = hypot(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:
46154
diff
changeset
|
215 if (!Py_IS_FINITE(result)) |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
216 errno = ERANGE; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
217 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:
46154
diff
changeset
|
218 errno = 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:
46154
diff
changeset
|
219 return result; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
220 } |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
221 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
222 static PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
223 complex_subtype_from_c_complex(PyTypeObject *type, Py_complex cval) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
224 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
225 PyObject *op; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
226 |
|
38533
3031a7110da9
Correctly allocate complex types with tp_alloc. (bug #1498638)
Georg Brandl <georg@python.org>
parents:
37815
diff
changeset
|
227 op = type->tp_alloc(type, 0); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
228 if (op != NULL) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
229 ((PyComplexObject *)op)->cval = cval; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
230 return op; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
231 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
232 |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
233 PyObject * |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
234 PyComplex_FromCComplex(Py_complex cval) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
235 { |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11882
diff
changeset
|
236 register PyComplexObject *op; |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11882
diff
changeset
|
237 |
|
24898
a869d2de536f
Call me anal, but there was a particular phrase that was speading to
Guido van Rossum <guido@python.org>
parents:
23832
diff
changeset
|
238 /* Inline PyObject_New */ |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11882
diff
changeset
|
239 op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject)); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
240 if (op == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
241 return PyErr_NoMemory(); |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11882
diff
changeset
|
242 PyObject_INIT(op, &PyComplex_Type); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
243 op->cval = cval; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
244 return (PyObject *) op; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
245 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
246 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
247 static PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
248 complex_subtype_from_doubles(PyTypeObject *type, double real, double imag) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
249 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
250 Py_complex c; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
251 c.real = real; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
252 c.imag = imag; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
253 return complex_subtype_from_c_complex(type, c); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
254 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
255 |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
256 PyObject * |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
257 PyComplex_FromDoubles(double real, double imag) |
|
4025
854f25fde8f4
Changes to make the file acceptable to K&R C compilers (HPUX, SunOS 4.x).
Guido van Rossum <guido@python.org>
parents:
3771
diff
changeset
|
258 { |
|
854f25fde8f4
Changes to make the file acceptable to K&R C compilers (HPUX, SunOS 4.x).
Guido van Rossum <guido@python.org>
parents:
3771
diff
changeset
|
259 Py_complex c; |
|
854f25fde8f4
Changes to make the file acceptable to K&R C compilers (HPUX, SunOS 4.x).
Guido van Rossum <guido@python.org>
parents:
3771
diff
changeset
|
260 c.real = real; |
|
854f25fde8f4
Changes to make the file acceptable to K&R C compilers (HPUX, SunOS 4.x).
Guido van Rossum <guido@python.org>
parents:
3771
diff
changeset
|
261 c.imag = imag; |
|
854f25fde8f4
Changes to make the file acceptable to K&R C compilers (HPUX, SunOS 4.x).
Guido van Rossum <guido@python.org>
parents:
3771
diff
changeset
|
262 return PyComplex_FromCComplex(c); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
263 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
264 |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
265 double |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
266 PyComplex_RealAsDouble(PyObject *op) |
|
4025
854f25fde8f4
Changes to make the file acceptable to K&R C compilers (HPUX, SunOS 4.x).
Guido van Rossum <guido@python.org>
parents:
3771
diff
changeset
|
267 { |
|
854f25fde8f4
Changes to make the file acceptable to K&R C compilers (HPUX, SunOS 4.x).
Guido van Rossum <guido@python.org>
parents:
3771
diff
changeset
|
268 if (PyComplex_Check(op)) { |
|
854f25fde8f4
Changes to make the file acceptable to K&R C compilers (HPUX, SunOS 4.x).
Guido van Rossum <guido@python.org>
parents:
3771
diff
changeset
|
269 return ((PyComplexObject *)op)->cval.real; |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
270 } |
|
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
271 else { |
|
4025
854f25fde8f4
Changes to make the file acceptable to K&R C compilers (HPUX, SunOS 4.x).
Guido van Rossum <guido@python.org>
parents:
3771
diff
changeset
|
272 return PyFloat_AsDouble(op); |
|
854f25fde8f4
Changes to make the file acceptable to K&R C compilers (HPUX, SunOS 4.x).
Guido van Rossum <guido@python.org>
parents:
3771
diff
changeset
|
273 } |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
274 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
275 |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
276 double |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
277 PyComplex_ImagAsDouble(PyObject *op) |
|
4025
854f25fde8f4
Changes to make the file acceptable to K&R C compilers (HPUX, SunOS 4.x).
Guido van Rossum <guido@python.org>
parents:
3771
diff
changeset
|
278 { |
|
854f25fde8f4
Changes to make the file acceptable to K&R C compilers (HPUX, SunOS 4.x).
Guido van Rossum <guido@python.org>
parents:
3771
diff
changeset
|
279 if (PyComplex_Check(op)) { |
|
854f25fde8f4
Changes to make the file acceptable to K&R C compilers (HPUX, SunOS 4.x).
Guido van Rossum <guido@python.org>
parents:
3771
diff
changeset
|
280 return ((PyComplexObject *)op)->cval.imag; |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
281 } |
|
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
282 else { |
|
4025
854f25fde8f4
Changes to make the file acceptable to K&R C compilers (HPUX, SunOS 4.x).
Guido van Rossum <guido@python.org>
parents:
3771
diff
changeset
|
283 return 0.0; |
|
854f25fde8f4
Changes to make the file acceptable to K&R C compilers (HPUX, SunOS 4.x).
Guido van Rossum <guido@python.org>
parents:
3771
diff
changeset
|
284 } |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
285 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
286 |
| 3771 | 287 Py_complex |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
288 PyComplex_AsCComplex(PyObject *op) |
|
4025
854f25fde8f4
Changes to make the file acceptable to K&R C compilers (HPUX, SunOS 4.x).
Guido van Rossum <guido@python.org>
parents:
3771
diff
changeset
|
289 { |
| 3771 | 290 Py_complex cv; |
|
41219
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
291 PyObject *newop = NULL; |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
292 static PyObject *complex_str = NULL; |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
293 |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
294 assert(op); |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
295 /* If op is already of type PyComplex_Type, return its value */ |
|
3328
4c655115dfce
Added PyComplex_AsCComplex
Guido van Rossum <guido@python.org>
parents:
3316
diff
changeset
|
296 if (PyComplex_Check(op)) { |
|
4c655115dfce
Added PyComplex_AsCComplex
Guido van Rossum <guido@python.org>
parents:
3316
diff
changeset
|
297 return ((PyComplexObject *)op)->cval; |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
298 } |
|
41219
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
299 /* If not, use op's __complex__ method, if it exists */ |
|
45028
1c9ac00c4758
static PyObject* variables should use PyString_InternFromString() instead of PyObject_FromString() to store a python string in a function level static var.
Christian Heimes <christian@cheimes.de>
parents:
44760
diff
changeset
|
300 |
|
41219
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
301 /* return -1 on failure */ |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
302 cv.real = -1.; |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
303 cv.imag = 0.; |
|
45028
1c9ac00c4758
static PyObject* variables should use PyString_InternFromString() instead of PyObject_FromString() to store a python string in a function level static var.
Christian Heimes <christian@cheimes.de>
parents:
44760
diff
changeset
|
304 |
|
1c9ac00c4758
static PyObject* variables should use PyString_InternFromString() instead of PyObject_FromString() to store a python string in a function level static var.
Christian Heimes <christian@cheimes.de>
parents:
44760
diff
changeset
|
305 if (complex_str == NULL) { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47471
diff
changeset
|
306 if (!(complex_str = PyString_InternFromString("__complex__"))) |
|
45028
1c9ac00c4758
static PyObject* variables should use PyString_InternFromString() instead of PyObject_FromString() to store a python string in a function level static var.
Christian Heimes <christian@cheimes.de>
parents:
44760
diff
changeset
|
307 return cv; |
|
1c9ac00c4758
static PyObject* variables should use PyString_InternFromString() instead of PyObject_FromString() to store a python string in a function level static var.
Christian Heimes <christian@cheimes.de>
parents:
44760
diff
changeset
|
308 } |
|
41219
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
309 |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
310 if (PyInstance_Check(op)) { |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
311 /* this can go away in python 3000 */ |
|
45028
1c9ac00c4758
static PyObject* variables should use PyString_InternFromString() instead of PyObject_FromString() to store a python string in a function level static var.
Christian Heimes <christian@cheimes.de>
parents:
44760
diff
changeset
|
312 if (PyObject_HasAttr(op, complex_str)) { |
|
41219
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
313 newop = PyObject_CallMethod(op, "__complex__", NULL); |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
314 if (!newop) |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
315 return cv; |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
316 } |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
317 /* else try __float__ */ |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
318 } else { |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
319 PyObject *complexfunc; |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
320 complexfunc = _PyType_Lookup(op->ob_type, complex_str); |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
321 /* complexfunc is a borrowed reference */ |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
322 if (complexfunc) { |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
323 newop = PyObject_CallFunctionObjArgs(complexfunc, op, NULL); |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
324 if (!newop) |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
325 return cv; |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
326 } |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
327 } |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
328 |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
329 if (newop) { |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
330 if (!PyComplex_Check(newop)) { |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
331 PyErr_SetString(PyExc_TypeError, |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
332 "__complex__ should return a complex object"); |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
333 Py_DECREF(newop); |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
334 return cv; |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
335 } |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
336 cv = ((PyComplexObject *)newop)->cval; |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
337 Py_DECREF(newop); |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
338 return cv; |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
339 } |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
340 /* If neither of the above works, interpret op as a float giving the |
|
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
341 real part of the result, and fill in the imaginary part as 0. */ |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
342 else { |
|
41219
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
343 /* PyFloat_AsDouble will return -1 on failure */ |
|
3328
4c655115dfce
Added PyComplex_AsCComplex
Guido van Rossum <guido@python.org>
parents:
3316
diff
changeset
|
344 cv.real = PyFloat_AsDouble(op); |
|
4c655115dfce
Added PyComplex_AsCComplex
Guido van Rossum <guido@python.org>
parents:
3316
diff
changeset
|
345 return cv; |
|
17029
65a638a24f7e
When 1.6 boosted the # of digits produced by repr(float), repr(complex)
Tim Peters <tim.peters@gmail.com>
parents:
15976
diff
changeset
|
346 } |
|
3328
4c655115dfce
Added PyComplex_AsCComplex
Guido van Rossum <guido@python.org>
parents:
3316
diff
changeset
|
347 } |
|
4c655115dfce
Added PyComplex_AsCComplex
Guido van Rossum <guido@python.org>
parents:
3316
diff
changeset
|
348 |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
349 static void |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
350 complex_dealloc(PyObject *op) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
351 { |
|
20277
2983b54256c7
Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents:
20140
diff
changeset
|
352 op->ob_type->tp_free(op); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
353 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
354 |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
355 |
|
3608
8573e65fa948
make some things static
Guido van Rossum <guido@python.org>
parents:
3350
diff
changeset
|
356 static void |
|
21078
025ad27d4224
complex_to_buf(), complex_subtype_from_c_complex(): Conversion of
Barry Warsaw <barry@python.org>
parents:
20666
diff
changeset
|
357 complex_to_buf(char *buf, int bufsz, PyComplexObject *v, int precision) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
358 { |
|
32305
13656d58d450
Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
30683
diff
changeset
|
359 char format[32]; |
|
13656d58d450
Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
30683
diff
changeset
|
360 if (v->cval.real == 0.) { |
|
45357
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
361 if (!Py_IS_FINITE(v->cval.imag)) { |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
362 if (Py_IS_NAN(v->cval.imag)) |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
363 strncpy(buf, "nan*j", 6); |
|
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:
46154
diff
changeset
|
364 else if (copysign(1, v->cval.imag) == 1) |
|
45357
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
365 strncpy(buf, "inf*j", 6); |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
366 else |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
367 strncpy(buf, "-inf*j", 7); |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
368 } |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
369 else { |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
370 PyOS_snprintf(format, sizeof(format), "%%.%ig", precision); |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
371 PyOS_ascii_formatd(buf, bufsz - 1, format, v->cval.imag); |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
372 strncat(buf, "j", 1); |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
373 } |
|
32305
13656d58d450
Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
30683
diff
changeset
|
374 } else { |
|
13656d58d450
Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
30683
diff
changeset
|
375 char re[64], im[64]; |
|
35580
f4c2c61cb560
backport patch [ 1118729 ] Error in representation of complex numbers(again)
Georg Brandl <georg@python.org>
parents:
34195
diff
changeset
|
376 /* Format imaginary part with sign, real part without */ |
|
45357
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
377 if (!Py_IS_FINITE(v->cval.real)) { |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
378 if (Py_IS_NAN(v->cval.real)) |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
379 strncpy(re, "nan", 4); |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
380 /* else if (copysign(1, v->cval.real) == 1) */ |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
381 else if (v->cval.real > 0) |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
382 strncpy(re, "inf", 4); |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
383 else |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
384 strncpy(re, "-inf", 5); |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
385 } |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
386 else { |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
387 PyOS_snprintf(format, sizeof(format), "%%.%ig", precision); |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
388 PyOS_ascii_formatd(re, sizeof(re), format, v->cval.real); |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
389 } |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
390 if (!Py_IS_FINITE(v->cval.imag)) { |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
391 if (Py_IS_NAN(v->cval.imag)) |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
392 strncpy(im, "+nan*", 6); |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
393 /* else if (copysign(1, v->cval.imag) == 1) */ |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
394 else if (v->cval.imag > 0) |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
395 strncpy(im, "+inf*", 6); |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
396 else |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
397 strncpy(im, "-inf*", 6); |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
398 } |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
399 else { |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
400 PyOS_snprintf(format, sizeof(format), "%%+.%ig", precision); |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
401 PyOS_ascii_formatd(im, sizeof(im), format, v->cval.imag); |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45028
diff
changeset
|
402 } |
|
35580
f4c2c61cb560
backport patch [ 1118729 ] Error in representation of complex numbers(again)
Georg Brandl <georg@python.org>
parents:
34195
diff
changeset
|
403 PyOS_snprintf(buf, bufsz, "(%s%sj)", re, im); |
|
32305
13656d58d450
Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
30683
diff
changeset
|
404 } |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
405 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
406 |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
407 static int |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
408 complex_print(PyComplexObject *v, FILE *fp, int flags) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
409 { |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
410 char buf[100]; |
|
21078
025ad27d4224
complex_to_buf(), complex_subtype_from_c_complex(): Conversion of
Barry Warsaw <barry@python.org>
parents:
20666
diff
changeset
|
411 complex_to_buf(buf, sizeof(buf), v, |
|
17029
65a638a24f7e
When 1.6 boosted the # of digits produced by repr(float), repr(complex)
Tim Peters <tim.peters@gmail.com>
parents:
15976
diff
changeset
|
412 (flags & Py_PRINT_RAW) ? PREC_STR : PREC_REPR); |
|
43378
7630248b593c
Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents:
42333
diff
changeset
|
413 Py_BEGIN_ALLOW_THREADS |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
414 fputs(buf, fp); |
|
43378
7630248b593c
Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents:
42333
diff
changeset
|
415 Py_END_ALLOW_THREADS |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
416 return 0; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
417 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
418 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
419 static PyObject * |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
420 complex_repr(PyComplexObject *v) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
421 { |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
422 char buf[100]; |
|
21078
025ad27d4224
complex_to_buf(), complex_subtype_from_c_complex(): Conversion of
Barry Warsaw <barry@python.org>
parents:
20666
diff
changeset
|
423 complex_to_buf(buf, sizeof(buf), v, PREC_REPR); |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47471
diff
changeset
|
424 return PyString_FromString(buf); |
|
17029
65a638a24f7e
When 1.6 boosted the # of digits produced by repr(float), repr(complex)
Tim Peters <tim.peters@gmail.com>
parents:
15976
diff
changeset
|
425 } |
|
65a638a24f7e
When 1.6 boosted the # of digits produced by repr(float), repr(complex)
Tim Peters <tim.peters@gmail.com>
parents:
15976
diff
changeset
|
426 |
|
65a638a24f7e
When 1.6 boosted the # of digits produced by repr(float), repr(complex)
Tim Peters <tim.peters@gmail.com>
parents:
15976
diff
changeset
|
427 static PyObject * |
|
65a638a24f7e
When 1.6 boosted the # of digits produced by repr(float), repr(complex)
Tim Peters <tim.peters@gmail.com>
parents:
15976
diff
changeset
|
428 complex_str(PyComplexObject *v) |
|
65a638a24f7e
When 1.6 boosted the # of digits produced by repr(float), repr(complex)
Tim Peters <tim.peters@gmail.com>
parents:
15976
diff
changeset
|
429 { |
|
65a638a24f7e
When 1.6 boosted the # of digits produced by repr(float), repr(complex)
Tim Peters <tim.peters@gmail.com>
parents:
15976
diff
changeset
|
430 char buf[100]; |
|
21078
025ad27d4224
complex_to_buf(), complex_subtype_from_c_complex(): Conversion of
Barry Warsaw <barry@python.org>
parents:
20666
diff
changeset
|
431 complex_to_buf(buf, sizeof(buf), v, PREC_STR); |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47471
diff
changeset
|
432 return PyString_FromString(buf); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
433 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
434 |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
435 static long |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
436 complex_hash(PyComplexObject *v) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
437 { |
|
13976
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13761
diff
changeset
|
438 long hashreal, hashimag, combined; |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13761
diff
changeset
|
439 hashreal = _Py_HashDouble(v->cval.real); |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13761
diff
changeset
|
440 if (hashreal == -1) |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13761
diff
changeset
|
441 return -1; |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13761
diff
changeset
|
442 hashimag = _Py_HashDouble(v->cval.imag); |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13761
diff
changeset
|
443 if (hashimag == -1) |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13761
diff
changeset
|
444 return -1; |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13761
diff
changeset
|
445 /* Note: if the imaginary part is 0, hashimag is 0 now, |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13761
diff
changeset
|
446 * so the following returns hashreal unchanged. This is |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13761
diff
changeset
|
447 * important because numbers of different types that |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13761
diff
changeset
|
448 * compare equal must have the same hash value, so that |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13761
diff
changeset
|
449 * hash(x + 0*j) must equal hash(x). |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13761
diff
changeset
|
450 */ |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13761
diff
changeset
|
451 combined = hashreal + 1000003 * hashimag; |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13761
diff
changeset
|
452 if (combined == -1) |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13761
diff
changeset
|
453 combined = -2; |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13761
diff
changeset
|
454 return combined; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
455 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
456 |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
457 /* This macro may return! */ |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
458 #define TO_COMPLEX(obj, c) \ |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
459 if (PyComplex_Check(obj)) \ |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
460 c = ((PyComplexObject *)(obj))->cval; \ |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
461 else if (to_complex(&(obj), &(c)) < 0) \ |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
462 return (obj) |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
463 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
464 static int |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
465 to_complex(PyObject **pobj, Py_complex *pc) |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
466 { |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
467 PyObject *obj = *pobj; |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
468 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
469 pc->real = pc->imag = 0.0; |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
470 if (PyInt_Check(obj)) { |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
471 pc->real = PyInt_AS_LONG(obj); |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
472 return 0; |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
473 } |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
474 if (PyLong_Check(obj)) { |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
475 pc->real = PyLong_AsDouble(obj); |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
476 if (pc->real == -1.0 && PyErr_Occurred()) { |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
477 *pobj = NULL; |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
478 return -1; |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
479 } |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
480 return 0; |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
481 } |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
482 if (PyFloat_Check(obj)) { |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
483 pc->real = PyFloat_AsDouble(obj); |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
484 return 0; |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
485 } |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
486 Py_INCREF(Py_NotImplemented); |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
487 *pobj = Py_NotImplemented; |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
488 return -1; |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
489 } |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
490 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
491 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
492 static PyObject * |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
493 complex_add(PyComplexObject *v, PyComplexObject *w) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
494 { |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
495 Py_complex result; |
|
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
496 PyFPE_START_PROTECT("complex_add", return 0) |
|
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
497 result = c_sum(v->cval,w->cval); |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5118
diff
changeset
|
498 PyFPE_END_PROTECT(result) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
499 return PyComplex_FromCComplex(result); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
500 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
501 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
502 static PyObject * |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
503 complex_sub(PyComplexObject *v, PyComplexObject *w) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
504 { |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
505 Py_complex result; |
|
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
506 PyFPE_START_PROTECT("complex_sub", return 0) |
|
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
507 result = c_diff(v->cval,w->cval); |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5118
diff
changeset
|
508 PyFPE_END_PROTECT(result) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
509 return PyComplex_FromCComplex(result); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
510 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
511 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
512 static PyObject * |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
513 complex_mul(PyComplexObject *v, PyComplexObject *w) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
514 { |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
515 Py_complex result; |
|
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
516 PyFPE_START_PROTECT("complex_mul", return 0) |
|
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
517 result = c_prod(v->cval,w->cval); |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5118
diff
changeset
|
518 PyFPE_END_PROTECT(result) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
519 return PyComplex_FromCComplex(result); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
520 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
521 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
522 static PyObject * |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
523 complex_div(PyComplexObject *v, PyComplexObject *w) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
524 { |
| 3771 | 525 Py_complex quot; |
|
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:
46154
diff
changeset
|
526 |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
527 PyFPE_START_PROTECT("complex_div", return 0) |
|
5559
1eced1aaad5a
Got rid of c_error in favor of errno (and EDOM/ERANGE).
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
528 errno = 0; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
529 quot = c_quot(v->cval,w->cval); |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5118
diff
changeset
|
530 PyFPE_END_PROTECT(quot) |
|
5559
1eced1aaad5a
Got rid of c_error in favor of errno (and EDOM/ERANGE).
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
531 if (errno == EDOM) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
532 PyErr_SetString(PyExc_ZeroDivisionError, "complex division"); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
533 return NULL; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
534 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
535 return PyComplex_FromCComplex(quot); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
536 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
537 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
538 static PyObject * |
|
19633
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
539 complex_classic_div(PyComplexObject *v, PyComplexObject *w) |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
540 { |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
541 Py_complex quot; |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
542 |
|
19673
b3271297b984
PEP 238 documented -Qwarn as warning only for classic int or long
Guido van Rossum <guido@python.org>
parents:
19633
diff
changeset
|
543 if (Py_DivisionWarningFlag >= 2 && |
|
19633
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
544 PyErr_Warn(PyExc_DeprecationWarning, |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
545 "classic complex division") < 0) |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
546 return NULL; |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
547 |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
548 PyFPE_START_PROTECT("complex_classic_div", return 0) |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
549 errno = 0; |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
550 quot = c_quot(v->cval,w->cval); |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
551 PyFPE_END_PROTECT(quot) |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
552 if (errno == EDOM) { |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
553 PyErr_SetString(PyExc_ZeroDivisionError, "complex division"); |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
554 return NULL; |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
555 } |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
556 return PyComplex_FromCComplex(quot); |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
557 } |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
558 |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
559 static PyObject * |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
560 complex_remainder(PyComplexObject *v, PyComplexObject *w) |
|
4275
cb841f1b022e
Don't dump core on complex % or divmod -- raise a TypeError exception.
Guido van Rossum <guido@python.org>
parents:
4025
diff
changeset
|
561 { |
|
44760
3d72f5209e67
Indentation normalization.
Georg Brandl <georg@python.org>
parents:
44432
diff
changeset
|
562 Py_complex div, mod; |
|
23043
7fecd6d58f38
Deprecate % as well. The message for deprecation of //, % and divmod
Guido van Rossum <guido@python.org>
parents:
23039
diff
changeset
|
563 |
|
7fecd6d58f38
Deprecate % as well. The message for deprecation of //, % and divmod
Guido van Rossum <guido@python.org>
parents:
23039
diff
changeset
|
564 if (PyErr_Warn(PyExc_DeprecationWarning, |
|
7fecd6d58f38
Deprecate % as well. The message for deprecation of //, % and divmod
Guido van Rossum <guido@python.org>
parents:
23039
diff
changeset
|
565 "complex divmod(), // and % are deprecated") < 0) |
|
7fecd6d58f38
Deprecate % as well. The message for deprecation of //, % and divmod
Guido van Rossum <guido@python.org>
parents:
23039
diff
changeset
|
566 return NULL; |
|
7fecd6d58f38
Deprecate % as well. The message for deprecation of //, % and divmod
Guido van Rossum <guido@python.org>
parents:
23039
diff
changeset
|
567 |
|
5559
1eced1aaad5a
Got rid of c_error in favor of errno (and EDOM/ERANGE).
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
568 errno = 0; |
|
4304
95c31931b76a
Properly(?) implemented remainder and divmod (Tim Hochberg)
Guido van Rossum <guido@python.org>
parents:
4290
diff
changeset
|
569 div = c_quot(v->cval,w->cval); /* The raw divisor value. */ |
|
5559
1eced1aaad5a
Got rid of c_error in favor of errno (and EDOM/ERANGE).
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
570 if (errno == EDOM) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
571 PyErr_SetString(PyExc_ZeroDivisionError, "complex remainder"); |
|
4304
95c31931b76a
Properly(?) implemented remainder and divmod (Tim Hochberg)
Guido van Rossum <guido@python.org>
parents:
4290
diff
changeset
|
572 return NULL; |
|
95c31931b76a
Properly(?) implemented remainder and divmod (Tim Hochberg)
Guido van Rossum <guido@python.org>
parents:
4290
diff
changeset
|
573 } |
|
95c31931b76a
Properly(?) implemented remainder and divmod (Tim Hochberg)
Guido van Rossum <guido@python.org>
parents:
4290
diff
changeset
|
574 div.real = floor(div.real); /* Use the floor of the real part. */ |
|
95c31931b76a
Properly(?) implemented remainder and divmod (Tim Hochberg)
Guido van Rossum <guido@python.org>
parents:
4290
diff
changeset
|
575 div.imag = 0.0; |
|
95c31931b76a
Properly(?) implemented remainder and divmod (Tim Hochberg)
Guido van Rossum <guido@python.org>
parents:
4290
diff
changeset
|
576 mod = c_diff(v->cval, c_prod(w->cval, div)); |
|
95c31931b76a
Properly(?) implemented remainder and divmod (Tim Hochberg)
Guido van Rossum <guido@python.org>
parents:
4290
diff
changeset
|
577 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
578 return PyComplex_FromCComplex(mod); |
|
4275
cb841f1b022e
Don't dump core on complex % or divmod -- raise a TypeError exception.
Guido van Rossum <guido@python.org>
parents:
4025
diff
changeset
|
579 } |
|
cb841f1b022e
Don't dump core on complex % or divmod -- raise a TypeError exception.
Guido van Rossum <guido@python.org>
parents:
4025
diff
changeset
|
580 |
|
cb841f1b022e
Don't dump core on complex % or divmod -- raise a TypeError exception.
Guido van Rossum <guido@python.org>
parents:
4025
diff
changeset
|
581 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
582 static PyObject * |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
583 complex_divmod(PyComplexObject *v, PyComplexObject *w) |
|
4304
95c31931b76a
Properly(?) implemented remainder and divmod (Tim Hochberg)
Guido van Rossum <guido@python.org>
parents:
4290
diff
changeset
|
584 { |
|
44760
3d72f5209e67
Indentation normalization.
Georg Brandl <georg@python.org>
parents:
44432
diff
changeset
|
585 Py_complex div, mod; |
|
4304
95c31931b76a
Properly(?) implemented remainder and divmod (Tim Hochberg)
Guido van Rossum <guido@python.org>
parents:
4290
diff
changeset
|
586 PyObject *d, *m, *z; |
| 23039 | 587 |
| 588 if (PyErr_Warn(PyExc_DeprecationWarning, | |
|
23043
7fecd6d58f38
Deprecate % as well. The message for deprecation of //, % and divmod
Guido van Rossum <guido@python.org>
parents:
23039
diff
changeset
|
589 "complex divmod(), // and % are deprecated") < 0) |
| 23039 | 590 return NULL; |
| 591 | |
|
5559
1eced1aaad5a
Got rid of c_error in favor of errno (and EDOM/ERANGE).
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
592 errno = 0; |
|
4304
95c31931b76a
Properly(?) implemented remainder and divmod (Tim Hochberg)
Guido van Rossum <guido@python.org>
parents:
4290
diff
changeset
|
593 div = c_quot(v->cval,w->cval); /* The raw divisor value. */ |
|
5559
1eced1aaad5a
Got rid of c_error in favor of errno (and EDOM/ERANGE).
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
594 if (errno == EDOM) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
595 PyErr_SetString(PyExc_ZeroDivisionError, "complex divmod()"); |
|
4304
95c31931b76a
Properly(?) implemented remainder and divmod (Tim Hochberg)
Guido van Rossum <guido@python.org>
parents:
4290
diff
changeset
|
596 return NULL; |
|
95c31931b76a
Properly(?) implemented remainder and divmod (Tim Hochberg)
Guido van Rossum <guido@python.org>
parents:
4290
diff
changeset
|
597 } |
|
95c31931b76a
Properly(?) implemented remainder and divmod (Tim Hochberg)
Guido van Rossum <guido@python.org>
parents:
4290
diff
changeset
|
598 div.real = floor(div.real); /* Use the floor of the real part. */ |
|
95c31931b76a
Properly(?) implemented remainder and divmod (Tim Hochberg)
Guido van Rossum <guido@python.org>
parents:
4290
diff
changeset
|
599 div.imag = 0.0; |
|
95c31931b76a
Properly(?) implemented remainder and divmod (Tim Hochberg)
Guido van Rossum <guido@python.org>
parents:
4290
diff
changeset
|
600 mod = c_diff(v->cval, c_prod(w->cval, div)); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
601 d = PyComplex_FromCComplex(div); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
602 m = PyComplex_FromCComplex(mod); |
|
30683
3b59fa5d25b1
Simplify and speedup uses of Py_BuildValue():
Raymond Hettinger <python@rcn.com>
parents:
30252
diff
changeset
|
603 z = PyTuple_Pack(2, d, m); |
|
4304
95c31931b76a
Properly(?) implemented remainder and divmod (Tim Hochberg)
Guido van Rossum <guido@python.org>
parents:
4290
diff
changeset
|
604 Py_XDECREF(d); |
|
95c31931b76a
Properly(?) implemented remainder and divmod (Tim Hochberg)
Guido van Rossum <guido@python.org>
parents:
4290
diff
changeset
|
605 Py_XDECREF(m); |
|
95c31931b76a
Properly(?) implemented remainder and divmod (Tim Hochberg)
Guido van Rossum <guido@python.org>
parents:
4290
diff
changeset
|
606 return z; |
|
95c31931b76a
Properly(?) implemented remainder and divmod (Tim Hochberg)
Guido van Rossum <guido@python.org>
parents:
4290
diff
changeset
|
607 } |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
608 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
609 static PyObject * |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
610 complex_pow(PyObject *v, PyObject *w, PyObject *z) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
611 { |
| 3771 | 612 Py_complex p; |
| 613 Py_complex exponent; | |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
614 long int_exponent; |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
615 Py_complex a, b; |
|
44760
3d72f5209e67
Indentation normalization.
Georg Brandl <georg@python.org>
parents:
44432
diff
changeset
|
616 TO_COMPLEX(v, a); |
|
3d72f5209e67
Indentation normalization.
Georg Brandl <georg@python.org>
parents:
44432
diff
changeset
|
617 TO_COMPLEX(w, b); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
618 |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
619 if (z!=Py_None) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
620 PyErr_SetString(PyExc_ValueError, "complex modulo"); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
621 return NULL; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
622 } |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
623 PyFPE_START_PROTECT("complex_pow", return 0) |
|
5559
1eced1aaad5a
Got rid of c_error in favor of errno (and EDOM/ERANGE).
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
624 errno = 0; |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
625 exponent = b; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
626 int_exponent = (long)exponent.real; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
627 if (exponent.imag == 0. && exponent.real == int_exponent) |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
628 p = c_powi(a,int_exponent); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
629 else |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
630 p = c_pow(a,exponent); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
631 |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5118
diff
changeset
|
632 PyFPE_END_PROTECT(p) |
|
22540
75ef1f16725e
backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents:
21894
diff
changeset
|
633 Py_ADJUST_ERANGE2(p.real, p.imag); |
|
75ef1f16725e
backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents:
21894
diff
changeset
|
634 if (errno == EDOM) { |
|
75ef1f16725e
backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents:
21894
diff
changeset
|
635 PyErr_SetString(PyExc_ZeroDivisionError, |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
636 "0.0 to a negative or complex power"); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
637 return NULL; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
638 } |
|
22540
75ef1f16725e
backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents:
21894
diff
changeset
|
639 else if (errno == ERANGE) { |
|
75ef1f16725e
backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents:
21894
diff
changeset
|
640 PyErr_SetString(PyExc_OverflowError, |
|
41085
f75457d7532c
Fix SF #1676971, Complex OverflowError has a typo
Neal Norwitz <nnorwitz@gmail.com>
parents:
39100
diff
changeset
|
641 "complex exponentiation"); |
|
22540
75ef1f16725e
backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents:
21894
diff
changeset
|
642 return NULL; |
|
75ef1f16725e
backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents:
21894
diff
changeset
|
643 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
644 return PyComplex_FromCComplex(p); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
645 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
646 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
647 static PyObject * |
|
19051
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
648 complex_int_div(PyComplexObject *v, PyComplexObject *w) |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
649 { |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
650 PyObject *t, *r; |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
651 |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
652 if (PyErr_Warn(PyExc_DeprecationWarning, |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
653 "complex divmod(), // and % are deprecated") < 0) |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
654 return NULL; |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
655 |
|
19051
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
656 t = complex_divmod(v, w); |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
657 if (t != NULL) { |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
658 r = PyTuple_GET_ITEM(t, 0); |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
659 Py_INCREF(r); |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
660 Py_DECREF(t); |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
661 return r; |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
662 } |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
663 return NULL; |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
664 } |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
665 |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
666 static PyObject * |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
667 complex_neg(PyComplexObject *v) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
668 { |
| 3771 | 669 Py_complex neg; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
670 neg.real = -v->cval.real; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
671 neg.imag = -v->cval.imag; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
672 return PyComplex_FromCComplex(neg); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
673 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
674 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
675 static PyObject * |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
676 complex_pos(PyComplexObject *v) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
677 { |
|
19889
733314fcfdaa
Again perhaps the end of [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19758
diff
changeset
|
678 if (PyComplex_CheckExact(v)) { |
|
733314fcfdaa
Again perhaps the end of [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19758
diff
changeset
|
679 Py_INCREF(v); |
|
733314fcfdaa
Again perhaps the end of [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19758
diff
changeset
|
680 return (PyObject *)v; |
|
733314fcfdaa
Again perhaps the end of [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19758
diff
changeset
|
681 } |
|
733314fcfdaa
Again perhaps the end of [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19758
diff
changeset
|
682 else |
|
733314fcfdaa
Again perhaps the end of [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19758
diff
changeset
|
683 return PyComplex_FromCComplex(v->cval); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
684 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
685 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
686 static PyObject * |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
687 complex_abs(PyComplexObject *v) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
688 { |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
689 double result; |
|
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:
46154
diff
changeset
|
690 |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4933
diff
changeset
|
691 PyFPE_START_PROTECT("complex_abs", return 0) |
|
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:
46154
diff
changeset
|
692 result = c_abs(v->cval); |
|
5197
74c12704a0bd
New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents:
5118
diff
changeset
|
693 PyFPE_END_PROTECT(result) |
|
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:
46154
diff
changeset
|
694 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
695 if (errno == ERANGE) { |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
696 PyErr_SetString(PyExc_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:
46154
diff
changeset
|
697 "absolute value too large"); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
698 return NULL; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
699 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
700 return PyFloat_FromDouble(result); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
701 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
702 |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
703 static int |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
704 complex_nonzero(PyComplexObject *v) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
705 { |
|
9940
93b9eafe1f1d
Ai! complex_nonzero() has && which should be ||, else decidedly
Guido van Rossum <guido@python.org>
parents:
8261
diff
changeset
|
706 return v->cval.real != 0.0 || v->cval.imag != 0.0; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
707 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
708 |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
709 static int |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
710 complex_coerce(PyObject **pv, PyObject **pw) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
711 { |
| 3771 | 712 Py_complex cval; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
713 cval.imag = 0.; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
714 if (PyInt_Check(*pw)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
715 cval.real = (double)PyInt_AsLong(*pw); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
716 *pw = PyComplex_FromCComplex(cval); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
717 Py_INCREF(*pv); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
718 return 0; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
719 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
720 else if (PyLong_Check(*pw)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
721 cval.real = PyLong_AsDouble(*pw); |
|
19674
715244108faa
Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents:
19673
diff
changeset
|
722 if (cval.real == -1.0 && PyErr_Occurred()) |
|
715244108faa
Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents:
19673
diff
changeset
|
723 return -1; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
724 *pw = PyComplex_FromCComplex(cval); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
725 Py_INCREF(*pv); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
726 return 0; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
727 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
728 else if (PyFloat_Check(*pw)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
729 cval.real = PyFloat_AsDouble(*pw); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
730 *pw = PyComplex_FromCComplex(cval); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
731 Py_INCREF(*pv); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
732 return 0; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
733 } |
|
19974
8edc44bd0ac7
complex_coerce(): add explicit PyComplex_Check() test. Previously,
Guido van Rossum <guido@python.org>
parents:
19889
diff
changeset
|
734 else if (PyComplex_Check(*pw)) { |
|
8edc44bd0ac7
complex_coerce(): add explicit PyComplex_Check() test. Previously,
Guido van Rossum <guido@python.org>
parents:
19889
diff
changeset
|
735 Py_INCREF(*pv); |
|
8edc44bd0ac7
complex_coerce(): add explicit PyComplex_Check() test. Previously,
Guido van Rossum <guido@python.org>
parents:
19889
diff
changeset
|
736 Py_INCREF(*pw); |
|
8edc44bd0ac7
complex_coerce(): add explicit PyComplex_Check() test. Previously,
Guido van Rossum <guido@python.org>
parents:
19889
diff
changeset
|
737 return 0; |
|
8edc44bd0ac7
complex_coerce(): add explicit PyComplex_Check() test. Previously,
Guido van Rossum <guido@python.org>
parents:
19889
diff
changeset
|
738 } |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
739 return 1; /* Can't do it */ |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
740 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
741 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
742 static PyObject * |
|
15976
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
743 complex_richcompare(PyObject *v, PyObject *w, int op) |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
744 { |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
745 int c; |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
746 Py_complex i, j; |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
747 PyObject *res; |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
748 |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
749 c = PyNumber_CoerceEx(&v, &w); |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
750 if (c < 0) |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
751 return NULL; |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
752 if (c > 0) { |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
753 Py_INCREF(Py_NotImplemented); |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
754 return Py_NotImplemented; |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
755 } |
|
20140
225f17ec8932
Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents:
20061
diff
changeset
|
756 /* Make sure both arguments are complex. */ |
|
225f17ec8932
Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents:
20061
diff
changeset
|
757 if (!(PyComplex_Check(v) && PyComplex_Check(w))) { |
|
15976
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
758 Py_DECREF(v); |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
759 Py_DECREF(w); |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
760 Py_INCREF(Py_NotImplemented); |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
761 return Py_NotImplemented; |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
762 } |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
763 |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
764 i = ((PyComplexObject *)v)->cval; |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
765 j = ((PyComplexObject *)w)->cval; |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
766 Py_DECREF(v); |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
767 Py_DECREF(w); |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
768 |
|
20061
2caddb82ffea
Do the same thing to complex that I did to str: the rich comparison
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
769 if (op != Py_EQ && op != Py_NE) { |
|
2caddb82ffea
Do the same thing to complex that I did to str: the rich comparison
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
770 PyErr_SetString(PyExc_TypeError, |
|
34195
46fa0e20cbcf
Bug #1079011: Incorrect error message (somewhat)
Raymond Hettinger <python@rcn.com>
parents:
33233
diff
changeset
|
771 "no ordering relation is defined for complex numbers"); |
|
20061
2caddb82ffea
Do the same thing to complex that I did to str: the rich comparison
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
772 return NULL; |
|
2caddb82ffea
Do the same thing to complex that I did to str: the rich comparison
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
773 } |
|
2caddb82ffea
Do the same thing to complex that I did to str: the rich comparison
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
774 |
|
15976
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
775 if ((i.real == j.real && i.imag == j.imag) == (op == Py_EQ)) |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
776 res = Py_True; |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
777 else |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
778 res = Py_False; |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
779 |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
780 Py_INCREF(res); |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
781 return res; |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
782 } |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
783 |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
784 static PyObject * |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
785 complex_int(PyObject *v) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
786 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
787 PyErr_SetString(PyExc_TypeError, |
|
30252
66654ac69622
Remove 'e.g.' from error message
Raymond Hettinger <python@rcn.com>
parents:
30155
diff
changeset
|
788 "can't convert complex to int; use int(abs(z))"); |
|
4290
9c6eab0cf542
Raise exception instead of dropping imag part for conversion to int,
Guido van Rossum <guido@python.org>
parents:
4275
diff
changeset
|
789 return NULL; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
790 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
791 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
792 static PyObject * |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
793 complex_long(PyObject *v) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
794 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
795 PyErr_SetString(PyExc_TypeError, |
|
30252
66654ac69622
Remove 'e.g.' from error message
Raymond Hettinger <python@rcn.com>
parents:
30155
diff
changeset
|
796 "can't convert complex to long; use long(abs(z))"); |
|
4290
9c6eab0cf542
Raise exception instead of dropping imag part for conversion to int,
Guido van Rossum <guido@python.org>
parents:
4275
diff
changeset
|
797 return NULL; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
798 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
799 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
800 static PyObject * |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
801 complex_float(PyObject *v) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
802 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
803 PyErr_SetString(PyExc_TypeError, |
|
30252
66654ac69622
Remove 'e.g.' from error message
Raymond Hettinger <python@rcn.com>
parents:
30155
diff
changeset
|
804 "can't convert complex to float; use abs(z)"); |
|
4290
9c6eab0cf542
Raise exception instead of dropping imag part for conversion to int,
Guido van Rossum <guido@python.org>
parents:
4275
diff
changeset
|
805 return NULL; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
806 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
807 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
808 static PyObject * |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
19051
diff
changeset
|
809 complex_conjugate(PyObject *self) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
810 { |
|
4025
854f25fde8f4
Changes to make the file acceptable to K&R C compilers (HPUX, SunOS 4.x).
Guido van Rossum <guido@python.org>
parents:
3771
diff
changeset
|
811 Py_complex c; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
812 c = ((PyComplexObject *)self)->cval; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
813 c.imag = -c.imag; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
814 return PyComplex_FromCComplex(c); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
815 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
816 |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
817 PyDoc_STRVAR(complex_conjugate_doc, |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
818 "complex.conjugate() -> complex\n" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
819 "\n" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
820 "Returns the complex conjugate of its argument. (3-4j).conjugate() == 3+4j."); |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
821 |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
25064
diff
changeset
|
822 static PyObject * |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
25064
diff
changeset
|
823 complex_getnewargs(PyComplexObject *v) |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
25064
diff
changeset
|
824 { |
|
47471
71bed208fe50
Fixed complex.__getnewargs__() to not emit another complex object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47294
diff
changeset
|
825 Py_complex c = v->cval; |
|
71bed208fe50
Fixed complex.__getnewargs__() to not emit another complex object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
47294
diff
changeset
|
826 return Py_BuildValue("(dd)", c.real, c.imag); |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
25064
diff
changeset
|
827 } |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
25064
diff
changeset
|
828 |
|
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:
46154
diff
changeset
|
829 #if 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:
46154
diff
changeset
|
830 static PyObject * |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
831 complex_is_finite(PyObject *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:
46154
diff
changeset
|
832 { |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
833 Py_complex c; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
834 c = ((PyComplexObject *)self)->cval; |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
835 return PyBool_FromLong((long)(Py_IS_FINITE(c.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:
46154
diff
changeset
|
836 Py_IS_FINITE(c.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:
46154
diff
changeset
|
837 } |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
838 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
839 PyDoc_STRVAR(complex_is_finite_doc, |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
840 "complex.is_finite() -> bool\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:
46154
diff
changeset
|
841 "\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:
46154
diff
changeset
|
842 "Returns True if the real and the imaginary part is finite."); |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
843 #endif |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
844 |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
845 static PyMethodDef complex_methods[] = { |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
846 {"conjugate", (PyCFunction)complex_conjugate, METH_NOARGS, |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
847 complex_conjugate_doc}, |
|
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:
46154
diff
changeset
|
848 #if 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:
46154
diff
changeset
|
849 {"is_finite", (PyCFunction)complex_is_finite, METH_NOARGS, |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
850 complex_is_finite_doc}, |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents:
46154
diff
changeset
|
851 #endif |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
25064
diff
changeset
|
852 {"__getnewargs__", (PyCFunction)complex_getnewargs, METH_NOARGS}, |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
853 {NULL, NULL} /* sentinel */ |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
854 }; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
855 |
|
20012
3faabf54b8b8
Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents:
19974
diff
changeset
|
856 static PyMemberDef complex_members[] = { |
|
21894
f245955b3fbe
Declare real and imag as read-only attributes.
Guido van Rossum <guido@python.org>
parents:
21399
diff
changeset
|
857 {"real", T_DOUBLE, offsetof(PyComplexObject, cval.real), READONLY, |
|
20012
3faabf54b8b8
Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents:
19974
diff
changeset
|
858 "the real part of a complex number"}, |
|
21894
f245955b3fbe
Declare real and imag as read-only attributes.
Guido van Rossum <guido@python.org>
parents:
21399
diff
changeset
|
859 {"imag", T_DOUBLE, offsetof(PyComplexObject, cval.imag), READONLY, |
|
20012
3faabf54b8b8
Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents:
19974
diff
changeset
|
860 "the imaginary part of a complex number"}, |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
861 {0}, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
862 }; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
863 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
864 static PyObject * |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
865 complex_subtype_from_string(PyTypeObject *type, PyObject *v) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
866 { |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
867 const char *s, *start; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
868 char *end; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
869 double x=0.0, y=0.0, z; |
|
41093
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
41085
diff
changeset
|
870 int got_re=0, got_im=0, got_bracket=0, done=0; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
871 int digit_or_dot; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
872 int sw_error=0; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
873 int sign; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
874 char buffer[256]; /* For errors */ |
|
20666
88a242bfb23a
complex_subtype_from_string(): move the declaration of s_buffer[] out
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
875 #ifdef Py_USING_UNICODE |
|
88a242bfb23a
complex_subtype_from_string(): move the declaration of s_buffer[] out
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
876 char s_buffer[256]; |
|
88a242bfb23a
complex_subtype_from_string(): move the declaration of s_buffer[] out
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
877 #endif |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
878 Py_ssize_t len; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
879 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47471
diff
changeset
|
880 if (PyString_Check(v)) { |
|
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47471
diff
changeset
|
881 s = PyString_AS_STRING(v); |
|
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47471
diff
changeset
|
882 len = PyString_GET_SIZE(v); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
883 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
884 #ifdef Py_USING_UNICODE |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
885 else if (PyUnicode_Check(v)) { |
|
37815
20cb13bdf1f0
C++ compiler cleanup: bunch-o-casts, plus use of unsigned loop index var in a couple places
Skip Montanaro <skip@pobox.com>
parents:
37304
diff
changeset
|
886 if (PyUnicode_GET_SIZE(v) >= (Py_ssize_t)sizeof(s_buffer)) { |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
887 PyErr_SetString(PyExc_ValueError, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
888 "complex() literal too large to convert"); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
889 return NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
890 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
891 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v), |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
892 PyUnicode_GET_SIZE(v), |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
893 s_buffer, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
894 NULL)) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
895 return NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
896 s = s_buffer; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
897 len = strlen(s); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
898 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
899 #endif |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
900 else if (PyObject_AsCharBuffer(v, &s, &len)) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
901 PyErr_SetString(PyExc_TypeError, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
902 "complex() arg is not a string"); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
903 return NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
904 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
905 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
906 /* position on first nonblank */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
907 start = s; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
908 while (*s && isspace(Py_CHARMASK(*s))) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
909 s++; |
|
44760
3d72f5209e67
Indentation normalization.
Georg Brandl <georg@python.org>
parents:
44432
diff
changeset
|
910 if (s[0] == '\0') { |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
911 PyErr_SetString(PyExc_ValueError, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
912 "complex() arg is an empty string"); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
913 return NULL; |
|
44760
3d72f5209e67
Indentation normalization.
Georg Brandl <georg@python.org>
parents:
44432
diff
changeset
|
914 } |
|
41093
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
41085
diff
changeset
|
915 if (s[0] == '(') { |
|
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
41085
diff
changeset
|
916 /* Skip over possible bracket from repr(). */ |
|
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
41085
diff
changeset
|
917 got_bracket = 1; |
|
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
41085
diff
changeset
|
918 s++; |
|
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
41085
diff
changeset
|
919 while (*s && isspace(Py_CHARMASK(*s))) |
|
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
41085
diff
changeset
|
920 s++; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
921 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
922 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
923 z = -1.0; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
924 sign = 1; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
925 do { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
926 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
927 switch (*s) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
928 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
929 case '\0': |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
930 if (s-start != len) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
931 PyErr_SetString( |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
932 PyExc_ValueError, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
933 "complex() arg contains a null byte"); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
934 return NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
935 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
936 if(!done) sw_error=1; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
937 break; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
938 |
|
41093
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
41085
diff
changeset
|
939 case ')': |
|
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
41085
diff
changeset
|
940 if (!got_bracket || !(got_re || got_im)) { |
|
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
41085
diff
changeset
|
941 sw_error=1; |
|
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
41085
diff
changeset
|
942 break; |
|
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
41085
diff
changeset
|
943 } |
|
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
41085
diff
changeset
|
944 got_bracket=0; |
|
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
41085
diff
changeset
|
945 done=1; |
|
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
41085
diff
changeset
|
946 s++; |
|
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
41085
diff
changeset
|
947 while (*s && isspace(Py_CHARMASK(*s))) |
|
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
41085
diff
changeset
|
948 s++; |
|
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
41085
diff
changeset
|
949 if (*s) sw_error=1; |
|
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
41085
diff
changeset
|
950 break; |
|
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
41085
diff
changeset
|
951 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
952 case '-': |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
953 sign = -1; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
954 /* Fallthrough */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
955 case '+': |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
956 if (done) sw_error=1; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
957 s++; |
|
41093
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
41085
diff
changeset
|
958 if ( *s=='\0'||*s=='+'||*s=='-'||*s==')'|| |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
959 isspace(Py_CHARMASK(*s)) ) sw_error=1; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
960 break; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
961 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
962 case 'J': |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
963 case 'j': |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
964 if (got_im || done) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
965 sw_error = 1; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
966 break; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
967 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
968 if (z<0.0) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
969 y=sign; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
970 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
971 else{ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
972 y=sign*z; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
973 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
974 got_im=1; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
975 s++; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
976 if (*s!='+' && *s!='-' ) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
977 done=1; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
978 break; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
979 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
980 default: |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
981 if (isspace(Py_CHARMASK(*s))) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
982 while (*s && isspace(Py_CHARMASK(*s))) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
983 s++; |
|
41093
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
41085
diff
changeset
|
984 if (*s && *s != ')') |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
985 sw_error=1; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
986 else |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
987 done = 1; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
988 break; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
989 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
990 digit_or_dot = |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
991 (*s=='.' || isdigit(Py_CHARMASK(*s))); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
992 if (done||!digit_or_dot) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
993 sw_error=1; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
994 break; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
995 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
996 errno = 0; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
997 PyFPE_START_PROTECT("strtod", return 0) |
|
32305
13656d58d450
Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents:
30683
diff
changeset
|
998 z = PyOS_ascii_strtod(s, &end) ; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
999 PyFPE_END_PROTECT(z) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1000 if (errno != 0) { |
|
21078
025ad27d4224
complex_to_buf(), complex_subtype_from_c_complex(): Conversion of
Barry Warsaw <barry@python.org>
parents:
20666
diff
changeset
|
1001 PyOS_snprintf(buffer, sizeof(buffer), |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1002 "float() out of range: %.150s", s); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1003 PyErr_SetString( |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1004 PyExc_ValueError, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1005 buffer); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1006 return NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1007 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1008 s=end; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1009 if (*s=='J' || *s=='j') { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1010 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1011 break; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1012 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1013 if (got_re) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1014 sw_error=1; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1015 break; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1016 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1017 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1018 /* accept a real part */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1019 x=sign*z; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1020 got_re=1; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1021 if (got_im) done=1; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1022 z = -1.0; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1023 sign = 1; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1024 break; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1025 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1026 } /* end of switch */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1027 |
|
23031
5bda546ebe24
SF bug 543840: complex(string) accepts strings with \0
Tim Peters <tim.peters@gmail.com>
parents:
22974
diff
changeset
|
1028 } while (s - start < len && !sw_error); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1029 |
|
41093
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
41085
diff
changeset
|
1030 if (sw_error || got_bracket) { |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1031 PyErr_SetString(PyExc_ValueError, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1032 "complex() arg is a malformed string"); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1033 return NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1034 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1035 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1036 return complex_subtype_from_doubles(type, x, y); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1037 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1038 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1039 static PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1040 complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1041 { |
|
23703
abe1bb76ee44
Close SF bug 563740. complex() now finds __complex__() in new style classes.
Raymond Hettinger <python@rcn.com>
parents:
23043
diff
changeset
|
1042 PyObject *r, *i, *tmp, *f; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1043 PyNumberMethods *nbr, *nbi = NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1044 Py_complex cr, ci; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1045 int own_r = 0; |
|
44037
60e6eaa378d9
Patch # 1507 by Mark Dickinson. Make complex(x, -0) retain the sign of
Guido van Rossum <guido@python.org>
parents:
43378
diff
changeset
|
1046 int cr_is_complex = 0; |
|
60e6eaa378d9
Patch # 1507 by Mark Dickinson. Make complex(x, -0) retain the sign of
Guido van Rossum <guido@python.org>
parents:
43378
diff
changeset
|
1047 int ci_is_complex = 0; |
|
23703
abe1bb76ee44
Close SF bug 563740. complex() now finds __complex__() in new style classes.
Raymond Hettinger <python@rcn.com>
parents:
23043
diff
changeset
|
1048 static PyObject *complexstr; |
|
36675
dba6494735d0
Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36501
diff
changeset
|
1049 static char *kwlist[] = {"real", "imag", 0}; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1050 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1051 r = Py_False; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1052 i = NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1053 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:complex", kwlist, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1054 &r, &i)) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1055 return NULL; |
|
25064
81a36d2a2d13
complex() was the only numeric constructor that created a new instance
Raymond Hettinger <python@rcn.com>
parents:
24898
diff
changeset
|
1056 |
|
41143
bed57dff3db8
Patch #1642844: comments to clarify the complexobject constructor.
Georg Brandl <georg@python.org>
parents:
41093
diff
changeset
|
1057 /* Special-case for a single argument when type(arg) is complex. */ |
|
28134
cde444184336
Fix from Greg Chapman from SF bug #695651: a complex subclass
Guido van Rossum <guido@python.org>
parents:
27521
diff
changeset
|
1058 if (PyComplex_CheckExact(r) && i == NULL && |
|
cde444184336
Fix from Greg Chapman from SF bug #695651: a complex subclass
Guido van Rossum <guido@python.org>
parents:
27521
diff
changeset
|
1059 type == &PyComplex_Type) { |
|
25064
81a36d2a2d13
complex() was the only numeric constructor that created a new instance
Raymond Hettinger <python@rcn.com>
parents:
24898
diff
changeset
|
1060 /* Note that we can't know whether it's safe to return |
|
81a36d2a2d13
complex() was the only numeric constructor that created a new instance
Raymond Hettinger <python@rcn.com>
parents:
24898
diff
changeset
|
1061 a complex *subclass* instance as-is, hence the restriction |
|
41143
bed57dff3db8
Patch #1642844: comments to clarify the complexobject constructor.
Georg Brandl <georg@python.org>
parents:
41093
diff
changeset
|
1062 to exact complexes here. If either the input or the |
|
bed57dff3db8
Patch #1642844: comments to clarify the complexobject constructor.
Georg Brandl <georg@python.org>
parents:
41093
diff
changeset
|
1063 output is a complex subclass, it will be handled below |
|
bed57dff3db8
Patch #1642844: comments to clarify the complexobject constructor.
Georg Brandl <georg@python.org>
parents:
41093
diff
changeset
|
1064 as a non-orthogonal vector. */ |
|
25064
81a36d2a2d13
complex() was the only numeric constructor that created a new instance
Raymond Hettinger <python@rcn.com>
parents:
24898
diff
changeset
|
1065 Py_INCREF(r); |
|
81a36d2a2d13
complex() was the only numeric constructor that created a new instance
Raymond Hettinger <python@rcn.com>
parents:
24898
diff
changeset
|
1066 return r; |
|
81a36d2a2d13
complex() was the only numeric constructor that created a new instance
Raymond Hettinger <python@rcn.com>
parents:
24898
diff
changeset
|
1067 } |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47471
diff
changeset
|
1068 if (PyString_Check(r) || PyUnicode_Check(r)) { |
|
21399
33d59e18b68d
Ensure that complex() only accepts a string argument as the first arg,
Fred Drake <fdrake@acm.org>
parents:
21078
diff
changeset
|
1069 if (i != NULL) { |
|
33d59e18b68d
Ensure that complex() only accepts a string argument as the first arg,
Fred Drake <fdrake@acm.org>
parents:
21078
diff
changeset
|
1070 PyErr_SetString(PyExc_TypeError, |
|
33d59e18b68d
Ensure that complex() only accepts a string argument as the first arg,
Fred Drake <fdrake@acm.org>
parents:
21078
diff
changeset
|
1071 "complex() can't take second arg" |
|
33d59e18b68d
Ensure that complex() only accepts a string argument as the first arg,
Fred Drake <fdrake@acm.org>
parents:
21078
diff
changeset
|
1072 " if first is a string"); |
|
33d59e18b68d
Ensure that complex() only accepts a string argument as the first arg,
Fred Drake <fdrake@acm.org>
parents:
21078
diff
changeset
|
1073 return NULL; |
|
44760
3d72f5209e67
Indentation normalization.
Georg Brandl <georg@python.org>
parents:
44432
diff
changeset
|
1074 } |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1075 return complex_subtype_from_string(type, r); |
|
21399
33d59e18b68d
Ensure that complex() only accepts a string argument as the first arg,
Fred Drake <fdrake@acm.org>
parents:
21078
diff
changeset
|
1076 } |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47471
diff
changeset
|
1077 if (i != NULL && (PyString_Check(i) || PyUnicode_Check(i))) { |
|
21399
33d59e18b68d
Ensure that complex() only accepts a string argument as the first arg,
Fred Drake <fdrake@acm.org>
parents:
21078
diff
changeset
|
1078 PyErr_SetString(PyExc_TypeError, |
|
33d59e18b68d
Ensure that complex() only accepts a string argument as the first arg,
Fred Drake <fdrake@acm.org>
parents:
21078
diff
changeset
|
1079 "complex() second arg can't be a string"); |
|
33d59e18b68d
Ensure that complex() only accepts a string argument as the first arg,
Fred Drake <fdrake@acm.org>
parents:
21078
diff
changeset
|
1080 return NULL; |
|
33d59e18b68d
Ensure that complex() only accepts a string argument as the first arg,
Fred Drake <fdrake@acm.org>
parents:
21078
diff
changeset
|
1081 } |
|
19889
733314fcfdaa
Again perhaps the end of [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19758
diff
changeset
|
1082 |
|
23703
abe1bb76ee44
Close SF bug 563740. complex() now finds __complex__() in new style classes.
Raymond Hettinger <python@rcn.com>
parents:
23043
diff
changeset
|
1083 /* XXX Hack to support classes with __complex__ method */ |
|
abe1bb76ee44
Close SF bug 563740. complex() now finds __complex__() in new style classes.
Raymond Hettinger <python@rcn.com>
parents:
23043
diff
changeset
|
1084 if (complexstr == NULL) { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47471
diff
changeset
|
1085 complexstr = PyString_InternFromString("__complex__"); |
|
23703
abe1bb76ee44
Close SF bug 563740. complex() now finds __complex__() in new style classes.
Raymond Hettinger <python@rcn.com>
parents:
23043
diff
changeset
|
1086 if (complexstr == NULL) |
|
abe1bb76ee44
Close SF bug 563740. complex() now finds __complex__() in new style classes.
Raymond Hettinger <python@rcn.com>
parents:
23043
diff
changeset
|
1087 return NULL; |
|
abe1bb76ee44
Close SF bug 563740. complex() now finds __complex__() in new style classes.
Raymond Hettinger <python@rcn.com>
parents:
23043
diff
changeset
|
1088 } |
|
abe1bb76ee44
Close SF bug 563740. complex() now finds __complex__() in new style classes.
Raymond Hettinger <python@rcn.com>
parents:
23043
diff
changeset
|
1089 f = PyObject_GetAttr(r, complexstr); |
|
abe1bb76ee44
Close SF bug 563740. complex() now finds __complex__() in new style classes.
Raymond Hettinger <python@rcn.com>
parents:
23043
diff
changeset
|
1090 if (f == NULL) |
|
abe1bb76ee44
Close SF bug 563740. complex() now finds __complex__() in new style classes.
Raymond Hettinger <python@rcn.com>
parents:
23043
diff
changeset
|
1091 PyErr_Clear(); |
|
abe1bb76ee44
Close SF bug 563740. complex() now finds __complex__() in new style classes.
Raymond Hettinger <python@rcn.com>
parents:
23043
diff
changeset
|
1092 else { |
|
30683
3b59fa5d25b1
Simplify and speedup uses of Py_BuildValue():
Raymond Hettinger <python@rcn.com>
parents:
30252
diff
changeset
|
1093 PyObject *args = PyTuple_New(0); |
|
23703
abe1bb76ee44
Close SF bug 563740. complex() now finds __complex__() in new style classes.
Raymond Hettinger <python@rcn.com>
parents:
23043
diff
changeset
|
1094 if (args == NULL) |
|
abe1bb76ee44
Close SF bug 563740. complex() now finds __complex__() in new style classes.
Raymond Hettinger <python@rcn.com>
parents:
23043
diff
changeset
|
1095 return NULL; |
|
abe1bb76ee44
Close SF bug 563740. complex() now finds __complex__() in new style classes.
Raymond Hettinger <python@rcn.com>
parents:
23043
diff
changeset
|
1096 r = PyEval_CallObject(f, args); |
|
abe1bb76ee44
Close SF bug 563740. complex() now finds __complex__() in new style classes.
Raymond Hettinger <python@rcn.com>
parents:
23043
diff
changeset
|
1097 Py_DECREF(args); |
|
abe1bb76ee44
Close SF bug 563740. complex() now finds __complex__() in new style classes.
Raymond Hettinger <python@rcn.com>
parents:
23043
diff
changeset
|
1098 Py_DECREF(f); |
|
abe1bb76ee44
Close SF bug 563740. complex() now finds __complex__() in new style classes.
Raymond Hettinger <python@rcn.com>
parents:
23043
diff
changeset
|
1099 if (r == NULL) |
|
abe1bb76ee44
Close SF bug 563740. complex() now finds __complex__() in new style classes.
Raymond Hettinger <python@rcn.com>
parents:
23043
diff
changeset
|
1100 return NULL; |
|
abe1bb76ee44
Close SF bug 563740. complex() now finds __complex__() in new style classes.
Raymond Hettinger <python@rcn.com>
parents:
23043
diff
changeset
|
1101 own_r = 1; |
|
abe1bb76ee44
Close SF bug 563740. complex() now finds __complex__() in new style classes.
Raymond Hettinger <python@rcn.com>
parents:
23043
diff
changeset
|
1102 } |
|
19889
733314fcfdaa
Again perhaps the end of [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19758
diff
changeset
|
1103 nbr = r->ob_type->tp_as_number; |
|
733314fcfdaa
Again perhaps the end of [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19758
diff
changeset
|
1104 if (i != NULL) |
|
733314fcfdaa
Again perhaps the end of [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19758
diff
changeset
|
1105 nbi = i->ob_type->tp_as_number; |
|
733314fcfdaa
Again perhaps the end of [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19758
diff
changeset
|
1106 if (nbr == NULL || nbr->nb_float == NULL || |
|
733314fcfdaa
Again perhaps the end of [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19758
diff
changeset
|
1107 ((i != NULL) && (nbi == NULL || nbi->nb_float == NULL))) { |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1108 PyErr_SetString(PyExc_TypeError, |
|
23703
abe1bb76ee44
Close SF bug 563740. complex() now finds __complex__() in new style classes.
Raymond Hettinger <python@rcn.com>
parents:
23043
diff
changeset
|
1109 "complex() argument must be a string or a number"); |
|
30155
e3281b0df6a4
complex_new(): This could leak when the argument was neither string nor
Tim Peters <tim.peters@gmail.com>
parents:
29520
diff
changeset
|
1110 if (own_r) { |
|
e3281b0df6a4
complex_new(): This could leak when the argument was neither string nor
Tim Peters <tim.peters@gmail.com>
parents:
29520
diff
changeset
|
1111 Py_DECREF(r); |
|
e3281b0df6a4
complex_new(): This could leak when the argument was neither string nor
Tim Peters <tim.peters@gmail.com>
parents:
29520
diff
changeset
|
1112 } |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1113 return NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1114 } |
|
41143
bed57dff3db8
Patch #1642844: comments to clarify the complexobject constructor.
Georg Brandl <georg@python.org>
parents:
41093
diff
changeset
|
1115 |
|
bed57dff3db8
Patch #1642844: comments to clarify the complexobject constructor.
Georg Brandl <georg@python.org>
parents:
41093
diff
changeset
|
1116 /* If we get this far, then the "real" and "imag" parts should |
|
bed57dff3db8
Patch #1642844: comments to clarify the complexobject constructor.
Georg Brandl <georg@python.org>
parents:
41093
diff
changeset
|
1117 both be treated as numbers, and the constructor should return a |
|
bed57dff3db8
Patch #1642844: comments to clarify the complexobject constructor.
Georg Brandl <georg@python.org>
parents:
41093
diff
changeset
|
1118 complex number equal to (real + imag*1j). |
|
bed57dff3db8
Patch #1642844: comments to clarify the complexobject constructor.
Georg Brandl <georg@python.org>
parents:
41093
diff
changeset
|
1119 |
|
bed57dff3db8
Patch #1642844: comments to clarify the complexobject constructor.
Georg Brandl <georg@python.org>
parents:
41093
diff
changeset
|
1120 Note that we do NOT assume the input to already be in canonical |
|
bed57dff3db8
Patch #1642844: comments to clarify the complexobject constructor.
Georg Brandl <georg@python.org>
parents:
41093
diff
changeset
|
1121 form; the "real" and "imag" parts might themselves be complex |
|
bed57dff3db8
Patch #1642844: comments to clarify the complexobject constructor.
Georg Brandl <georg@python.org>
parents:
41093
diff
changeset
|
1122 numbers, which slightly complicates the code below. */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1123 if (PyComplex_Check(r)) { |
|
19889
733314fcfdaa
Again perhaps the end of [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19758
diff
changeset
|
1124 /* Note that if r is of a complex subtype, we're only |
|
733314fcfdaa
Again perhaps the end of [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19758
diff
changeset
|
1125 retaining its real & imag parts here, and the return |
|
733314fcfdaa
Again perhaps the end of [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19758
diff
changeset
|
1126 value is (properly) of the builtin complex type. */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1127 cr = ((PyComplexObject*)r)->cval; |
|
44037
60e6eaa378d9
Patch # 1507 by Mark Dickinson. Make complex(x, -0) retain the sign of
Guido van Rossum <guido@python.org>
parents:
43378
diff
changeset
|
1128 cr_is_complex = 1; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1129 if (own_r) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1130 Py_DECREF(r); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1131 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1132 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1133 else { |
|
41143
bed57dff3db8
Patch #1642844: comments to clarify the complexobject constructor.
Georg Brandl <georg@python.org>
parents:
41093
diff
changeset
|
1134 /* The "real" part really is entirely real, and contributes |
|
bed57dff3db8
Patch #1642844: comments to clarify the complexobject constructor.
Georg Brandl <georg@python.org>
parents:
41093
diff
changeset
|
1135 nothing in the imaginary direction. |
|
bed57dff3db8
Patch #1642844: comments to clarify the complexobject constructor.
Georg Brandl <georg@python.org>
parents:
41093
diff
changeset
|
1136 Just treat it as a double. */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1137 tmp = PyNumber_Float(r); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1138 if (own_r) { |
|
41143
bed57dff3db8
Patch #1642844: comments to clarify the complexobject constructor.
Georg Brandl <georg@python.org>
parents:
41093
diff
changeset
|
1139 /* r was a newly created complex number, rather |
|
bed57dff3db8
Patch #1642844: comments to clarify the complexobject constructor.
Georg Brandl <georg@python.org>
parents:
41093
diff
changeset
|
1140 than the original "real" argument. */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1141 Py_DECREF(r); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1142 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1143 if (tmp == NULL) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1144 return NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1145 if (!PyFloat_Check(tmp)) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1146 PyErr_SetString(PyExc_TypeError, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1147 "float(r) didn't return a float"); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1148 Py_DECREF(tmp); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1149 return NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1150 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1151 cr.real = PyFloat_AsDouble(tmp); |
|
44760
3d72f5209e67
Indentation normalization.
Georg Brandl <georg@python.org>
parents:
44432
diff
changeset
|
1152 cr.imag = 0.0; /* Shut up compiler warning */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1153 Py_DECREF(tmp); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1154 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1155 if (i == NULL) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1156 ci.real = 0.0; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1157 } |
|
44037
60e6eaa378d9
Patch # 1507 by Mark Dickinson. Make complex(x, -0) retain the sign of
Guido van Rossum <guido@python.org>
parents:
43378
diff
changeset
|
1158 else if (PyComplex_Check(i)) { |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1159 ci = ((PyComplexObject*)i)->cval; |
|
44037
60e6eaa378d9
Patch # 1507 by Mark Dickinson. Make complex(x, -0) retain the sign of
Guido van Rossum <guido@python.org>
parents:
43378
diff
changeset
|
1160 ci_is_complex = 1; |
|
60e6eaa378d9
Patch # 1507 by Mark Dickinson. Make complex(x, -0) retain the sign of
Guido van Rossum <guido@python.org>
parents:
43378
diff
changeset
|
1161 } else { |
|
41143
bed57dff3db8
Patch #1642844: comments to clarify the complexobject constructor.
Georg Brandl <georg@python.org>
parents:
41093
diff
changeset
|
1162 /* The "imag" part really is entirely imaginary, and |
|
bed57dff3db8
Patch #1642844: comments to clarify the complexobject constructor.
Georg Brandl <georg@python.org>
parents:
41093
diff
changeset
|
1163 contributes nothing in the real direction. |
|
bed57dff3db8
Patch #1642844: comments to clarify the complexobject constructor.
Georg Brandl <georg@python.org>
parents:
41093
diff
changeset
|
1164 Just treat it as a double. */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1165 tmp = (*nbi->nb_float)(i); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1166 if (tmp == NULL) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1167 return NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1168 ci.real = PyFloat_AsDouble(tmp); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1169 Py_DECREF(tmp); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1170 } |
|
41143
bed57dff3db8
Patch #1642844: comments to clarify the complexobject constructor.
Georg Brandl <georg@python.org>
parents:
41093
diff
changeset
|
1171 /* If the input was in canonical form, then the "real" and "imag" |
|
44037
60e6eaa378d9
Patch # 1507 by Mark Dickinson. Make complex(x, -0) retain the sign of
Guido van Rossum <guido@python.org>
parents:
43378
diff
changeset
|
1172 parts are real numbers, so that ci.imag and cr.imag are zero. |
|
41143
bed57dff3db8
Patch #1642844: comments to clarify the complexobject constructor.
Georg Brandl <georg@python.org>
parents:
41093
diff
changeset
|
1173 We need this correction in case they were not real numbers. */ |
|
44037
60e6eaa378d9
Patch # 1507 by Mark Dickinson. Make complex(x, -0) retain the sign of
Guido van Rossum <guido@python.org>
parents:
43378
diff
changeset
|
1174 |
|
60e6eaa378d9
Patch # 1507 by Mark Dickinson. Make complex(x, -0) retain the sign of
Guido van Rossum <guido@python.org>
parents:
43378
diff
changeset
|
1175 if (ci_is_complex) { |
|
60e6eaa378d9
Patch # 1507 by Mark Dickinson. Make complex(x, -0) retain the sign of
Guido van Rossum <guido@python.org>
parents:
43378
diff
changeset
|
1176 cr.real -= ci.imag; |
|
60e6eaa378d9
Patch # 1507 by Mark Dickinson. Make complex(x, -0) retain the sign of
Guido van Rossum <guido@python.org>
parents:
43378
diff
changeset
|
1177 } |
|
60e6eaa378d9
Patch # 1507 by Mark Dickinson. Make complex(x, -0) retain the sign of
Guido van Rossum <guido@python.org>
parents:
43378
diff
changeset
|
1178 if (cr_is_complex) { |
|
60e6eaa378d9
Patch # 1507 by Mark Dickinson. Make complex(x, -0) retain the sign of
Guido van Rossum <guido@python.org>
parents:
43378
diff
changeset
|
1179 ci.real += cr.imag; |
|
60e6eaa378d9
Patch # 1507 by Mark Dickinson. Make complex(x, -0) retain the sign of
Guido van Rossum <guido@python.org>
parents:
43378
diff
changeset
|
1180 } |
|
60e6eaa378d9
Patch # 1507 by Mark Dickinson. Make complex(x, -0) retain the sign of
Guido van Rossum <guido@python.org>
parents:
43378
diff
changeset
|
1181 return complex_subtype_from_doubles(type, cr.real, ci.real); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1182 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1183 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23820
diff
changeset
|
1184 PyDoc_STRVAR(complex_doc, |
|
19889
733314fcfdaa
Again perhaps the end of [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19758
diff
changeset
|
1185 "complex(real[, imag]) -> complex number\n" |
|
733314fcfdaa
Again perhaps the end of [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19758
diff
changeset
|
1186 "\n" |
|
733314fcfdaa
Again perhaps the end of [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19758
diff
changeset
|
1187 "Create a complex number from a real part and an optional imaginary part.\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23820
diff
changeset
|
1188 "This is equivalent to (real + imag*1j) where imag defaults to 0."); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1189 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
1190 static PyNumberMethods complex_as_number = { |
|
15976
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1191 (binaryfunc)complex_add, /* nb_add */ |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1192 (binaryfunc)complex_sub, /* nb_subtract */ |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1193 (binaryfunc)complex_mul, /* nb_multiply */ |
|
19633
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
1194 (binaryfunc)complex_classic_div, /* nb_divide */ |
|
15976
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1195 (binaryfunc)complex_remainder, /* nb_remainder */ |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1196 (binaryfunc)complex_divmod, /* nb_divmod */ |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1197 (ternaryfunc)complex_pow, /* nb_power */ |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1198 (unaryfunc)complex_neg, /* nb_negative */ |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1199 (unaryfunc)complex_pos, /* nb_positive */ |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1200 (unaryfunc)complex_abs, /* nb_absolute */ |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1201 (inquiry)complex_nonzero, /* nb_nonzero */ |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1202 0, /* nb_invert */ |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1203 0, /* nb_lshift */ |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1204 0, /* nb_rshift */ |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1205 0, /* nb_and */ |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1206 0, /* nb_xor */ |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1207 0, /* nb_or */ |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36675
diff
changeset
|
1208 complex_coerce, /* nb_coerce */ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36675
diff
changeset
|
1209 complex_int, /* nb_int */ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36675
diff
changeset
|
1210 complex_long, /* nb_long */ |
|
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36675
diff
changeset
|
1211 complex_float, /* nb_float */ |
|
15976
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1212 0, /* nb_oct */ |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1213 0, /* nb_hex */ |
|
19051
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1214 0, /* nb_inplace_add */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1215 0, /* nb_inplace_subtract */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1216 0, /* nb_inplace_multiply*/ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1217 0, /* nb_inplace_divide */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1218 0, /* nb_inplace_remainder */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1219 0, /* nb_inplace_power */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1220 0, /* nb_inplace_lshift */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1221 0, /* nb_inplace_rshift */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1222 0, /* nb_inplace_and */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1223 0, /* nb_inplace_xor */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1224 0, /* nb_inplace_or */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1225 (binaryfunc)complex_int_div, /* nb_floor_divide */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1226 (binaryfunc)complex_div, /* nb_true_divide */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1227 0, /* nb_inplace_floor_divide */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1228 0, /* nb_inplace_true_divide */ |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1229 }; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1230 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
1231 PyTypeObject PyComplex_Type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41219
diff
changeset
|
1232 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1233 "complex", |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
1234 sizeof(PyComplexObject), |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1235 0, |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
36675
diff
changeset
|
1236 complex_dealloc, /* tp_dealloc */ |
|
15976
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1237 (printfunc)complex_print, /* tp_print */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1238 0, /* tp_getattr */ |
|
15976
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1239 0, /* tp_setattr */ |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1240 0, /* tp_compare */ |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1241 (reprfunc)complex_repr, /* tp_repr */ |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1242 &complex_as_number, /* tp_as_number */ |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1243 0, /* tp_as_sequence */ |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1244 0, /* tp_as_mapping */ |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1245 (hashfunc)complex_hash, /* tp_hash */ |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1246 0, /* tp_call */ |
|
17029
65a638a24f7e
When 1.6 boosted the # of digits produced by repr(float), repr(complex)
Tim Peters <tim.peters@gmail.com>
parents:
15976
diff
changeset
|
1247 (reprfunc)complex_str, /* tp_str */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1248 PyObject_GenericGetAttr, /* tp_getattro */ |
|
15976
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1249 0, /* tp_setattro */ |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1250 0, /* tp_as_buffer */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1251 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1252 complex_doc, /* tp_doc */ |
|
15976
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1253 0, /* tp_traverse */ |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1254 0, /* tp_clear */ |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
1255 complex_richcompare, /* tp_richcompare */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1256 0, /* tp_weaklistoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1257 0, /* tp_iter */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1258 0, /* tp_iternext */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1259 complex_methods, /* tp_methods */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1260 complex_members, /* tp_members */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1261 0, /* tp_getset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1262 0, /* tp_base */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1263 0, /* tp_dict */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1264 0, /* tp_descr_get */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1265 0, /* tp_descr_set */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1266 0, /* tp_dictoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1267 0, /* tp_init */ |
|
38533
3031a7110da9
Correctly allocate complex types with tp_alloc. (bug #1498638)
Georg Brandl <georg@python.org>
parents:
37815
diff
changeset
|
1268 PyType_GenericAlloc, /* tp_alloc */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1269 complex_new, /* tp_new */ |
|
22974
72778ec94dec
PyObject_Del can now be used as a function designator.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22540
diff
changeset
|
1270 PyObject_Del, /* tp_free */ |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1271 }; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1272 |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1273 #endif |
