Mercurial > cpython-lru_cache-weakref
annotate Objects/complexobject.c @ 62449:53a152b400a7 legacy-trunk
must force gc here
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Tue, 22 Jun 2010 20:26:20 +0000 |
| parents | 3edfad2549c6 |
| children | b36cb4602e21 |
| 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 |
|
23820
130fff486fa6
Rearrange the #ifndef WITHOUT_COMPLEX so it can be picked up from
Guido van Rossum <guido@python.org>
parents:
23703
diff
changeset
|
11 #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
|
12 |
|
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
|
13 /* 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
|
14 |
|
65a638a24f7e
When 1.6 boosted the # of digits produced by repr(float), repr(complex)
Tim Peters <tim.peters@gmail.com>
parents:
15976
diff
changeset
|
15 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
|
16 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
|
17 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
|
18 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
|
19 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
|
20 |
|
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 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
|
22 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
|
23 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
|
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 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
26 #define PREC_REPR 17 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
27 #define PREC_STR 12 |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 /* elementary operations on complex numbers */ |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 |
| 3771 | 31 static Py_complex c_1 = {1., 0.}; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
33 Py_complex |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
34 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
|
35 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
36 Py_complex r; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
37 r.real = a.real + b.real; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
38 r.imag = a.imag + b.imag; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
39 return r; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
42 Py_complex |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
43 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
|
44 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
45 Py_complex r; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
46 r.real = a.real - b.real; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
47 r.imag = a.imag - b.imag; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
48 return r; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
51 Py_complex |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
52 c_neg(Py_complex a) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
54 Py_complex r; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
55 r.real = -a.real; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
56 r.imag = -a.imag; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
57 return r; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
60 Py_complex |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
61 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
|
62 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
63 Py_complex r; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
64 r.real = a.real*b.real - a.imag*b.imag; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
65 r.imag = a.real*b.imag + a.imag*b.real; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
66 return r; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
69 Py_complex |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
70 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
|
71 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
72 /****************************************************************** |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
73 This was the original algorithm. It's grossly prone to spurious |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
74 overflow and underflow errors. It also merrily divides by 0 despite |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
75 checking for that(!). The code still serves a doc purpose here, as |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
76 the algorithm following is a simple by-cases transformation of this |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
77 one: |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
78 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
79 Py_complex r; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
80 double d = b.real*b.real + b.imag*b.imag; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
81 if (d == 0.) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
82 errno = EDOM; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
83 r.real = (a.real*b.real + a.imag*b.imag)/d; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
84 r.imag = (a.imag*b.real - a.real*b.imag)/d; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
85 return r; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
86 ******************************************************************/ |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
87 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
88 /* This algorithm is better, and is pretty obvious: first divide the |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
89 * numerators and denominator by whichever of {b.real, b.imag} has |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
90 * larger magnitude. The earliest reference I found was to CACM |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
91 * Algorithm 116 (Complex Division, Robert L. Smith, Stanford |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
92 * University). As usual, though, we're still ignoring all IEEE |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
93 * endcases. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
94 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
95 Py_complex r; /* the result */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
96 const double abs_breal = b.real < 0 ? -b.real : b.real; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
97 const double abs_bimag = b.imag < 0 ? -b.imag : b.imag; |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
98 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
99 if (abs_breal >= abs_bimag) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
100 /* divide tops and bottom by b.real */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
101 if (abs_breal == 0.0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
102 errno = EDOM; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
103 r.real = r.imag = 0.0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
104 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
105 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
106 const double ratio = b.imag / b.real; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
107 const double denom = b.real + b.imag * ratio; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
108 r.real = (a.real + a.imag * ratio) / denom; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
109 r.imag = (a.imag - a.real * ratio) / denom; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
110 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
111 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
112 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
113 /* divide tops and bottom by b.imag */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
114 const double ratio = b.real / b.imag; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
115 const double denom = b.real * ratio + b.imag; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
116 assert(b.imag != 0.0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
117 r.real = (a.real * ratio + a.imag) / denom; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
118 r.imag = (a.imag * ratio - a.real) / denom; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
119 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
120 return r; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
123 Py_complex |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
124 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
|
125 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
126 Py_complex r; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
127 double vabs,len,at,phase; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
128 if (b.real == 0. && b.imag == 0.) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
129 r.real = 1.; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
130 r.imag = 0.; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
131 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
132 else if (a.real == 0. && a.imag == 0.) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
133 if (b.imag != 0. || b.real < 0.) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
134 errno = EDOM; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
135 r.real = 0.; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
136 r.imag = 0.; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
137 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
138 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
139 vabs = hypot(a.real,a.imag); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
140 len = pow(vabs,b.real); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
141 at = atan2(a.imag, a.real); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
142 phase = at*b.real; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
143 if (b.imag != 0.0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
144 len /= exp(at*b.imag); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
145 phase += b.imag*log(vabs); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
146 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
147 r.real = len*cos(phase); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
148 r.imag = len*sin(phase); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
149 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
150 return r; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
153 static Py_complex |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
154 c_powu(Py_complex x, long n) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
156 Py_complex r, p; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
157 long mask = 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
158 r = c_1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
159 p = x; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
160 while (mask > 0 && n >= mask) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
161 if (n & mask) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
162 r = c_prod(r,p); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
163 mask <<= 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
164 p = c_prod(p,p); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
165 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
166 return r; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
167 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
168 |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
169 static Py_complex |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
17029
diff
changeset
|
170 c_powi(Py_complex x, long n) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
171 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
172 Py_complex cn; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
174 if (n > 100 || n < -100) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
175 cn.real = (double) n; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
176 cn.imag = 0.; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
177 return c_pow(x,cn); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
178 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
179 else if (n > 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
180 return c_powu(x,n); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
181 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
182 return c_quot(c_1,c_powu(x,-n)); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
183 |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
184 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
185 |
|
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
|
186 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
|
187 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
|
188 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
189 /* sets errno = ERANGE on overflow; otherwise errno = 0 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
190 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
|
191 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
192 if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
193 /* C99 rules: if either the real or the imaginary part is an |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
194 infinity, return infinity, even if the other part is a |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
195 NaN. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
196 if (Py_IS_INFINITY(z.real)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
197 result = fabs(z.real); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
198 errno = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
199 return result; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
200 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
201 if (Py_IS_INFINITY(z.imag)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
202 result = fabs(z.imag); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
203 errno = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
204 return result; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
205 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
206 /* either the real or imaginary part is a NaN, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
207 and neither is infinite. Result should be NaN. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
208 return Py_NAN; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
209 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
210 result = hypot(z.real, z.imag); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
211 if (!Py_IS_FINITE(result)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
212 errno = ERANGE; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
213 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
214 errno = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
215 return 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
|
216 } |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot 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 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
218 static PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
219 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
|
220 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
221 PyObject *op; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
222 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
223 op = type->tp_alloc(type, 0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
224 if (op != NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
225 ((PyComplexObject *)op)->cval = cval; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
226 return op; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
227 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
228 |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
229 PyObject * |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
230 PyComplex_FromCComplex(Py_complex cval) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
231 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
232 register PyComplexObject *op; |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11882
diff
changeset
|
233 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
234 /* Inline PyObject_New */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
235 op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
236 if (op == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
237 return PyErr_NoMemory(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
238 PyObject_INIT(op, &PyComplex_Type); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
239 op->cval = cval; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
240 return (PyObject *) op; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
241 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
242 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
243 static PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
244 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
|
245 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
246 Py_complex c; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
247 c.real = real; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
248 c.imag = imag; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
249 return complex_subtype_from_c_complex(type, c); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
250 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
251 |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
252 PyObject * |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
253 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
|
254 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
255 Py_complex c; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
256 c.real = real; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
257 c.imag = imag; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
258 return PyComplex_FromCComplex(c); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
259 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
260 |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
261 double |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
262 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
|
263 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
264 if (PyComplex_Check(op)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
265 return ((PyComplexObject *)op)->cval.real; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
266 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
267 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
268 return PyFloat_AsDouble(op); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
269 } |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
270 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
271 |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
272 double |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
273 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
|
274 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
275 if (PyComplex_Check(op)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
276 return ((PyComplexObject *)op)->cval.imag; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
277 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
278 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
279 return 0.0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
280 } |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
281 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
282 |
|
58142
f7e09ba0cb55
factor out __complex__ lookup code to fix another case
Benjamin Peterson <benjamin@python.org>
parents:
58141
diff
changeset
|
283 static PyObject * |
|
f7e09ba0cb55
factor out __complex__ lookup code to fix another case
Benjamin Peterson <benjamin@python.org>
parents:
58141
diff
changeset
|
284 try_complex_special_method(PyObject *op) { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
285 PyObject *f; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
286 static PyObject *complexstr; |
|
58142
f7e09ba0cb55
factor out __complex__ lookup code to fix another case
Benjamin Peterson <benjamin@python.org>
parents:
58141
diff
changeset
|
287 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
288 if (complexstr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
289 complexstr = PyString_InternFromString("__complex__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
290 if (complexstr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
291 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
292 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
293 if (PyInstance_Check(op)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
294 f = PyObject_GetAttr(op, complexstr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
295 if (f == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
296 if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
297 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
298 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
299 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
300 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
301 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
302 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
303 f = _PyObject_LookupSpecial(op, "__complex__", &complexstr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
304 if (f == NULL && PyErr_Occurred()) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
305 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
306 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
307 if (f != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
308 PyObject *res = PyObject_CallFunctionObjArgs(f, NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
309 Py_DECREF(f); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
310 return res; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
311 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
312 return NULL; |
|
58142
f7e09ba0cb55
factor out __complex__ lookup code to fix another case
Benjamin Peterson <benjamin@python.org>
parents:
58141
diff
changeset
|
313 } |
|
f7e09ba0cb55
factor out __complex__ lookup code to fix another case
Benjamin Peterson <benjamin@python.org>
parents:
58141
diff
changeset
|
314 |
| 3771 | 315 Py_complex |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
316 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
|
317 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
318 Py_complex cv; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
319 PyObject *newop = NULL; |
|
41219
ebe52dbc3553
Patch #1675423: PyComplex_AsCComplex() now tries to convert an object
Georg Brandl <georg@python.org>
parents:
41143
diff
changeset
|
320 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
321 assert(op); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
322 /* If op is already of type PyComplex_Type, return its value */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
323 if (PyComplex_Check(op)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
324 return ((PyComplexObject *)op)->cval; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
325 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
326 /* 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
|
327 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
328 /* return -1 on failure */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
329 cv.real = -1.; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
330 cv.imag = 0.; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
331 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
332 newop = try_complex_special_method(op); |
|
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
|
333 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
334 if (newop) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
335 if (!PyComplex_Check(newop)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
336 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
337 "__complex__ should return a complex object"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
338 Py_DECREF(newop); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
339 return cv; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
340 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
341 cv = ((PyComplexObject *)newop)->cval; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
342 Py_DECREF(newop); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
343 return cv; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
344 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
345 else if (PyErr_Occurred()) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
346 return cv; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
347 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
348 /* If neither of the above works, interpret op as a float giving the |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
349 real part of the result, and fill in the imaginary part as 0. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
350 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
351 /* PyFloat_AsDouble will return -1 on failure */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
352 cv.real = PyFloat_AsDouble(op); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
353 return cv; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
354 } |
|
3328
4c655115dfce
Added PyComplex_AsCComplex
Guido van Rossum <guido@python.org>
parents:
3316
diff
changeset
|
355 } |
|
4c655115dfce
Added PyComplex_AsCComplex
Guido van Rossum <guido@python.org>
parents:
3316
diff
changeset
|
356 |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
357 static void |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
358 complex_dealloc(PyObject *op) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
359 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
360 op->ob_type->tp_free(op); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
361 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
362 |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
363 |
| 53338 | 364 static PyObject * |
|
53802
1d8f24d40c2c
Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents:
53712
diff
changeset
|
365 complex_format(PyComplexObject *v, int precision, char format_code) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
366 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
367 PyObject *result = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
368 Py_ssize_t len; |
| 53338 | 369 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
370 /* If these are non-NULL, they'll need to be freed. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
371 char *pre = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
372 char *im = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
373 char *buf = NULL; |
| 53338 | 374 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
375 /* These do not need to be freed. re is either an alias |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
376 for pre or a pointer to a constant. lead and tail |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
377 are pointers to constants. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
378 char *re = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
379 char *lead = ""; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
380 char *tail = ""; |
| 53338 | 381 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
382 if (v->cval.real == 0. && copysign(1.0, v->cval.real)==1.0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
383 re = ""; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
384 im = PyOS_double_to_string(v->cval.imag, format_code, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
385 precision, 0, NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
386 if (!im) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
387 PyErr_NoMemory(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
388 goto done; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
389 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
390 } else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
391 /* Format imaginary part with sign, real part without */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
392 pre = PyOS_double_to_string(v->cval.real, format_code, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
393 precision, 0, NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
394 if (!pre) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
395 PyErr_NoMemory(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
396 goto done; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
397 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
398 re = pre; |
| 53338 | 399 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
400 im = PyOS_double_to_string(v->cval.imag, format_code, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
401 precision, Py_DTSF_SIGN, NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
402 if (!im) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
403 PyErr_NoMemory(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
404 goto done; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
405 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
406 lead = "("; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
407 tail = ")"; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
408 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
409 /* Alloc the final buffer. Add one for the "j" in the format string, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
410 and one for the trailing zero. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
411 len = strlen(lead) + strlen(re) + strlen(im) + strlen(tail) + 2; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
412 buf = PyMem_Malloc(len); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
413 if (!buf) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
414 PyErr_NoMemory(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
415 goto done; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
416 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
417 PyOS_snprintf(buf, len, "%s%s%sj%s", lead, re, im, tail); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
418 result = PyString_FromString(buf); |
| 53338 | 419 done: |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
420 PyMem_Free(im); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
421 PyMem_Free(pre); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
422 PyMem_Free(buf); |
| 53338 | 423 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
424 return result; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
425 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
426 |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
427 static int |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
428 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
|
429 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
430 PyObject *formatv; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
431 char *buf; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
432 if (flags & Py_PRINT_RAW) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
433 formatv = complex_format(v, PyFloat_STR_PRECISION, 'g'); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
434 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
435 formatv = complex_format(v, 0, 'r'); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
436 if (formatv == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
437 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
438 buf = PyString_AS_STRING(formatv); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
439 Py_BEGIN_ALLOW_THREADS |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
440 fputs(buf, fp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
441 Py_END_ALLOW_THREADS |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
442 Py_DECREF(formatv); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
443 return 0; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
444 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
445 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
446 static PyObject * |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
447 complex_repr(PyComplexObject *v) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
448 { |
|
53802
1d8f24d40c2c
Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents:
53712
diff
changeset
|
449 return complex_format(v, 0, 'r'); |
|
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
|
450 } |
|
65a638a24f7e
When 1.6 boosted the # of digits produced by repr(float), repr(complex)
Tim Peters <tim.peters@gmail.com>
parents:
15976
diff
changeset
|
451 |
|
65a638a24f7e
When 1.6 boosted the # of digits produced by repr(float), repr(complex)
Tim Peters <tim.peters@gmail.com>
parents:
15976
diff
changeset
|
452 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
|
453 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
|
454 { |
|
53802
1d8f24d40c2c
Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents:
53712
diff
changeset
|
455 return complex_format(v, PyFloat_STR_PRECISION, 'g'); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
456 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
457 |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
458 static long |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
459 complex_hash(PyComplexObject *v) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
460 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
461 long hashreal, hashimag, combined; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
462 hashreal = _Py_HashDouble(v->cval.real); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
463 if (hashreal == -1) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
464 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
465 hashimag = _Py_HashDouble(v->cval.imag); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
466 if (hashimag == -1) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
467 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
468 /* Note: if the imaginary part is 0, hashimag is 0 now, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
469 * so the following returns hashreal unchanged. This is |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
470 * important because numbers of different types that |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
471 * compare equal must have the same hash value, so that |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
472 * hash(x + 0*j) must equal hash(x). |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
473 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
474 combined = hashreal + 1000003 * hashimag; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
475 if (combined == -1) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
476 combined = -2; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
477 return combined; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
478 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
479 |
|
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
|
480 /* 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
|
481 #define TO_COMPLEX(obj, c) \ |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
482 if (PyComplex_Check(obj)) \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
483 c = ((PyComplexObject *)(obj))->cval; \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
484 else if (to_complex(&(obj), &(c)) < 0) \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
485 return (obj) |
|
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
|
486 |
|
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 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
|
488 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
|
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 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
|
491 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
492 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
|
493 if (PyInt_Check(obj)) { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
494 pc->real = PyInt_AS_LONG(obj); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
495 return 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
|
496 } |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
497 if (PyLong_Check(obj)) { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
498 pc->real = PyLong_AsDouble(obj); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
499 if (pc->real == -1.0 && PyErr_Occurred()) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
500 *pobj = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
501 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
502 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
503 return 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
|
504 } |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
505 if (PyFloat_Check(obj)) { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
506 pc->real = PyFloat_AsDouble(obj); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
507 return 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
|
508 } |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44132
diff
changeset
|
509 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
|
510 *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
|
511 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
|
512 } |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
513 |
|
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
|
514 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
515 static PyObject * |
|
58978
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58142
diff
changeset
|
516 complex_add(PyObject *v, PyObject *w) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
517 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
518 Py_complex result; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
519 Py_complex a, b; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
520 TO_COMPLEX(v, a); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
521 TO_COMPLEX(w, b); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
522 PyFPE_START_PROTECT("complex_add", return 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
523 result = c_sum(a, b); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
524 PyFPE_END_PROTECT(result) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
525 return PyComplex_FromCComplex(result); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
526 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
527 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
528 static PyObject * |
|
58978
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58142
diff
changeset
|
529 complex_sub(PyObject *v, PyObject *w) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
530 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
531 Py_complex result; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
532 Py_complex a, b; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
533 TO_COMPLEX(v, a); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
534 TO_COMPLEX(w, b);; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
535 PyFPE_START_PROTECT("complex_sub", return 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
536 result = c_diff(a, b); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
537 PyFPE_END_PROTECT(result) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
538 return PyComplex_FromCComplex(result); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
539 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
540 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
541 static PyObject * |
|
58978
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58142
diff
changeset
|
542 complex_mul(PyObject *v, PyObject *w) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
543 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
544 Py_complex result; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
545 Py_complex a, b; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
546 TO_COMPLEX(v, a); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
547 TO_COMPLEX(w, b); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
548 PyFPE_START_PROTECT("complex_mul", return 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
549 result = c_prod(a, b); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
550 PyFPE_END_PROTECT(result) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
551 return PyComplex_FromCComplex(result); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
552 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
553 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
554 static PyObject * |
|
58978
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58142
diff
changeset
|
555 complex_div(PyObject *v, PyObject *w) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
556 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
557 Py_complex quot; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
558 Py_complex a, b; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
559 TO_COMPLEX(v, a); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
560 TO_COMPLEX(w, b); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
561 PyFPE_START_PROTECT("complex_div", return 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
562 errno = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
563 quot = c_quot(a, b); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
564 PyFPE_END_PROTECT(quot) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
565 if (errno == EDOM) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
566 PyErr_SetString(PyExc_ZeroDivisionError, "complex division by zero"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
567 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
568 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
569 return PyComplex_FromCComplex(quot); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
570 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
571 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
572 static PyObject * |
|
58978
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58142
diff
changeset
|
573 complex_classic_div(PyObject *v, PyObject *w) |
|
19633
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
574 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
575 Py_complex quot; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
576 Py_complex a, b; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
577 TO_COMPLEX(v, a); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
578 TO_COMPLEX(w, b); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
579 if (Py_DivisionWarningFlag >= 2 && |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
580 PyErr_Warn(PyExc_DeprecationWarning, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
581 "classic complex division") < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
582 return NULL; |
|
19633
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
583 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
584 PyFPE_START_PROTECT("complex_classic_div", return 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
585 errno = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
586 quot = c_quot(a, b); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
587 PyFPE_END_PROTECT(quot) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
588 if (errno == EDOM) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
589 PyErr_SetString(PyExc_ZeroDivisionError, "complex division by zero"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
590 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
591 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
592 return PyComplex_FromCComplex(quot); |
|
19633
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
593 } |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
594 |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
595 static PyObject * |
|
58978
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58142
diff
changeset
|
596 complex_remainder(PyObject *v, PyObject *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
|
597 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
598 Py_complex div, mod; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
599 Py_complex a, b; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
600 TO_COMPLEX(v, a); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
601 TO_COMPLEX(w, b); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
602 if (PyErr_Warn(PyExc_DeprecationWarning, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
603 "complex divmod(), // and % are deprecated") < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
604 return NULL; |
|
23043
7fecd6d58f38
Deprecate % as well. The message for deprecation of //, % and divmod
Guido van Rossum <guido@python.org>
parents:
23039
diff
changeset
|
605 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
606 errno = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
607 div = c_quot(a, b); /* The raw divisor value. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
608 if (errno == EDOM) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
609 PyErr_SetString(PyExc_ZeroDivisionError, "complex remainder"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
610 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
611 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
612 div.real = floor(div.real); /* Use the floor of the real part. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
613 div.imag = 0.0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
614 mod = c_diff(a, c_prod(b, div)); |
|
4304
95c31931b76a
Properly(?) implemented remainder and divmod (Tim Hochberg)
Guido van Rossum <guido@python.org>
parents:
4290
diff
changeset
|
615 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
616 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
|
617 } |
|
cb841f1b022e
Don't dump core on complex % or divmod -- raise a TypeError exception.
Guido van Rossum <guido@python.org>
parents:
4025
diff
changeset
|
618 |
|
cb841f1b022e
Don't dump core on complex % or divmod -- raise a TypeError exception.
Guido van Rossum <guido@python.org>
parents:
4025
diff
changeset
|
619 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
620 static PyObject * |
|
58978
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58142
diff
changeset
|
621 complex_divmod(PyObject *v, PyObject *w) |
|
4304
95c31931b76a
Properly(?) implemented remainder and divmod (Tim Hochberg)
Guido van Rossum <guido@python.org>
parents:
4290
diff
changeset
|
622 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
623 Py_complex div, mod; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
624 PyObject *d, *m, *z; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
625 Py_complex a, b; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
626 TO_COMPLEX(v, a); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
627 TO_COMPLEX(w, b); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
628 if (PyErr_Warn(PyExc_DeprecationWarning, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
629 "complex divmod(), // and % are deprecated") < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
630 return NULL; |
| 23039 | 631 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
632 errno = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
633 div = c_quot(a, b); /* The raw divisor value. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
634 if (errno == EDOM) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
635 PyErr_SetString(PyExc_ZeroDivisionError, "complex divmod()"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
636 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
637 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
638 div.real = floor(div.real); /* Use the floor of the real part. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
639 div.imag = 0.0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
640 mod = c_diff(a, c_prod(b, div)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
641 d = PyComplex_FromCComplex(div); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
642 m = PyComplex_FromCComplex(mod); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
643 z = PyTuple_Pack(2, d, m); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
644 Py_XDECREF(d); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
645 Py_XDECREF(m); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
646 return z; |
|
4304
95c31931b76a
Properly(?) implemented remainder and divmod (Tim Hochberg)
Guido van Rossum <guido@python.org>
parents:
4290
diff
changeset
|
647 } |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
648 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
649 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
|
650 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
|
651 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
652 Py_complex p; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
653 Py_complex exponent; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
654 long int_exponent; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
655 Py_complex a, b; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
656 TO_COMPLEX(v, a); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
657 TO_COMPLEX(w, b); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
658 if (z!=Py_None) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
659 PyErr_SetString(PyExc_ValueError, "complex modulo"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
660 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
661 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
662 PyFPE_START_PROTECT("complex_pow", return 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
663 errno = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
664 exponent = b; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
665 int_exponent = (long)exponent.real; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
666 if (exponent.imag == 0. && exponent.real == int_exponent) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
667 p = c_powi(a,int_exponent); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
668 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
669 p = c_pow(a,exponent); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
670 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
671 PyFPE_END_PROTECT(p) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
672 Py_ADJUST_ERANGE2(p.real, p.imag); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
673 if (errno == EDOM) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
674 PyErr_SetString(PyExc_ZeroDivisionError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
675 "0.0 to a negative or complex power"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
676 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
677 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
678 else if (errno == ERANGE) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
679 PyErr_SetString(PyExc_OverflowError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
680 "complex exponentiation"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
681 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
682 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
683 return PyComplex_FromCComplex(p); |
|
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 * |
|
58978
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58142
diff
changeset
|
687 complex_int_div(PyObject *v, PyObject *w) |
|
19051
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
688 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
689 PyObject *t, *r; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
690 Py_complex a, b; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
691 TO_COMPLEX(v, a); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
692 TO_COMPLEX(w, b); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
693 if (PyErr_Warn(PyExc_DeprecationWarning, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
694 "complex divmod(), // and % are deprecated") < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
695 return NULL; |
|
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
|
696 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
697 t = complex_divmod(v, w); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
698 if (t != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
699 r = PyTuple_GET_ITEM(t, 0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
700 Py_INCREF(r); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
701 Py_DECREF(t); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
702 return r; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
703 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
704 return NULL; |
|
19051
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
705 } |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
706 |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
707 static PyObject * |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
708 complex_neg(PyComplexObject *v) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
709 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
710 Py_complex neg; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
711 neg.real = -v->cval.real; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
712 neg.imag = -v->cval.imag; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
713 return PyComplex_FromCComplex(neg); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
714 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
715 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
716 static PyObject * |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
717 complex_pos(PyComplexObject *v) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
718 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
719 if (PyComplex_CheckExact(v)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
720 Py_INCREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
721 return (PyObject *)v; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
722 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
723 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
724 return PyComplex_FromCComplex(v->cval); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
725 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
726 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
727 static PyObject * |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
728 complex_abs(PyComplexObject *v) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
729 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
730 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
|
731 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
732 PyFPE_START_PROTECT("complex_abs", return 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
733 result = c_abs(v->cval); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
734 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
|
735 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
736 if (errno == ERANGE) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
737 PyErr_SetString(PyExc_OverflowError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
738 "absolute value too large"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
739 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
740 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
741 return PyFloat_FromDouble(result); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
742 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
743 |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
744 static int |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
745 complex_nonzero(PyComplexObject *v) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
746 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
747 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
|
748 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
749 |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
750 static int |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
751 complex_coerce(PyObject **pv, PyObject **pw) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
752 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
753 Py_complex cval; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
754 cval.imag = 0.; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
755 if (PyInt_Check(*pw)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
756 cval.real = (double)PyInt_AsLong(*pw); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
757 *pw = PyComplex_FromCComplex(cval); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
758 Py_INCREF(*pv); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
759 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
760 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
761 else if (PyLong_Check(*pw)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
762 cval.real = PyLong_AsDouble(*pw); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
763 if (cval.real == -1.0 && PyErr_Occurred()) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
764 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
765 *pw = PyComplex_FromCComplex(cval); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
766 Py_INCREF(*pv); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
767 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
768 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
769 else if (PyFloat_Check(*pw)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
770 cval.real = PyFloat_AsDouble(*pw); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
771 *pw = PyComplex_FromCComplex(cval); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
772 Py_INCREF(*pv); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
773 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
774 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
775 else if (PyComplex_Check(*pw)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
776 Py_INCREF(*pv); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
777 Py_INCREF(*pw); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
778 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
779 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
780 return 1; /* Can't do it */ |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
781 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
782 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
783 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
|
784 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
|
785 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
786 PyObject *res; |
|
61991
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
787 Py_complex i; |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
788 int equal; |
|
15976
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
789 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
790 if (op != Py_EQ && op != Py_NE) { |
|
61991
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
791 /* for backwards compatibility, comparisons with non-numbers return |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
792 * NotImplemented. Only comparisons with core numeric types raise |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
793 * TypeError. |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
794 */ |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
795 if (PyInt_Check(w) || PyLong_Check(w) || |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
796 PyFloat_Check(w) || PyComplex_Check(w)) { |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
797 PyErr_SetString(PyExc_TypeError, |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
798 "no ordering relation is defined " |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
799 "for complex numbers"); |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
800 return NULL; |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
801 } |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
802 goto Unimplemented; |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
803 } |
|
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
|
804 |
|
61991
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
805 assert(PyComplex_Check(v)); |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
806 TO_COMPLEX(v, i); |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
807 |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
808 if (PyInt_Check(w) || PyLong_Check(w)) { |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
809 /* Check for 0.0 imaginary part first to avoid the rich |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
810 * comparison when possible. |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
811 */ |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
812 if (i.imag == 0.0) { |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
813 PyObject *j, *sub_res; |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
814 j = PyFloat_FromDouble(i.real); |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
815 if (j == NULL) |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
816 return NULL; |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
817 |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
818 sub_res = PyObject_RichCompare(j, w, op); |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
819 Py_DECREF(j); |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
820 return sub_res; |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
821 } |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
822 else { |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
823 equal = 0; |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
824 } |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
825 } |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
826 else if (PyFloat_Check(w)) { |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
827 equal = (i.real == PyFloat_AsDouble(w) && i.imag == 0.0); |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
828 } |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
829 else if (PyComplex_Check(w)) { |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
830 Py_complex j; |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
831 |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
832 TO_COMPLEX(w, j); |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
833 equal = (i.real == j.real && i.imag == j.imag); |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
834 } |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
835 else { |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
836 goto Unimplemented; |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
837 } |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
838 |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
839 if (equal == (op == Py_EQ)) |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
840 res = Py_True; |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
841 else |
|
61991
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
842 res = Py_False; |
|
15976
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
843 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
844 Py_INCREF(res); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
845 return res; |
|
61991
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
846 |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
847 Unimplemented: |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
848 Py_INCREF(Py_NotImplemented); |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61989
diff
changeset
|
849 return Py_NotImplemented; |
|
15976
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
850 } |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
851 |
|
51fc45276a43
Use rich comparisons to fulfill an old wish: complex numbers now raise
Guido van Rossum <guido@python.org>
parents:
14869
diff
changeset
|
852 static PyObject * |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
853 complex_int(PyObject *v) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
854 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
855 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
856 "can't convert complex to int"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
857 return NULL; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
858 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
859 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
860 static PyObject * |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
861 complex_long(PyObject *v) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
862 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
863 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
864 "can't convert complex to long"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
865 return NULL; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
866 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
867 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
868 static PyObject * |
|
13452
ed4f7a22e928
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
869 complex_float(PyObject *v) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
870 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
871 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
872 "can't convert complex to float"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
873 return NULL; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
874 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
875 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
876 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
|
877 complex_conjugate(PyObject *self) |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
878 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
879 Py_complex c; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
880 c = ((PyComplexObject *)self)->cval; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
881 c.imag = -c.imag; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
882 return PyComplex_FromCComplex(c); |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
883 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
884 |
|
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
|
885 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
|
886 "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
|
887 "\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
|
888 "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
|
889 |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
25064
diff
changeset
|
890 static PyObject * |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
25064
diff
changeset
|
891 complex_getnewargs(PyComplexObject *v) |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
25064
diff
changeset
|
892 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
893 Py_complex c = v->cval; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
894 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
|
895 } |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
25064
diff
changeset
|
896 |
|
53629
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53464
diff
changeset
|
897 PyDoc_STRVAR(complex__format__doc, |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53464
diff
changeset
|
898 "complex.__format__() -> str\n" |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53464
diff
changeset
|
899 "\n" |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53464
diff
changeset
|
900 "Converts to a string according to format_spec."); |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53464
diff
changeset
|
901 |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53464
diff
changeset
|
902 static PyObject * |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53464
diff
changeset
|
903 complex__format__(PyObject* self, PyObject* args) |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53464
diff
changeset
|
904 { |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53464
diff
changeset
|
905 PyObject *format_spec; |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53464
diff
changeset
|
906 |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53464
diff
changeset
|
907 if (!PyArg_ParseTuple(args, "O:__format__", &format_spec)) |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
908 return NULL; |
|
53629
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53464
diff
changeset
|
909 if (PyBytes_Check(format_spec)) |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
910 return _PyComplex_FormatAdvanced(self, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
911 PyBytes_AS_STRING(format_spec), |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
912 PyBytes_GET_SIZE(format_spec)); |
|
53629
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53464
diff
changeset
|
913 if (PyUnicode_Check(format_spec)) { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
914 /* Convert format_spec to a str */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
915 PyObject *result; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
916 PyObject *str_spec = PyObject_Str(format_spec); |
|
53629
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53464
diff
changeset
|
917 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
918 if (str_spec == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
919 return NULL; |
|
53629
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53464
diff
changeset
|
920 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
921 result = _PyComplex_FormatAdvanced(self, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
922 PyBytes_AS_STRING(str_spec), |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
923 PyBytes_GET_SIZE(str_spec)); |
|
53629
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53464
diff
changeset
|
924 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
925 Py_DECREF(str_spec); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
926 return result; |
|
53629
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53464
diff
changeset
|
927 } |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53464
diff
changeset
|
928 PyErr_SetString(PyExc_TypeError, "__format__ requires str or unicode"); |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53464
diff
changeset
|
929 return NULL; |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53464
diff
changeset
|
930 } |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53464
diff
changeset
|
931 |
|
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
|
932 #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
|
933 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
|
934 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
|
935 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
936 Py_complex c; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
937 c = ((PyComplexObject *)self)->cval; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
938 return PyBool_FromLong((long)(Py_IS_FINITE(c.real) && |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
939 Py_IS_FINITE(c.imag))); |
|
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
|
940 } |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot 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
|
941 |
|
5bc7b20dc04a
I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot 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
|
942 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
|
943 "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
|
944 "\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
|
945 "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
|
946 #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
|
947 |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
948 static PyMethodDef complex_methods[] = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
949 {"conjugate", (PyCFunction)complex_conjugate, METH_NOARGS, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
950 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
|
951 #if 0 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
952 {"is_finite", (PyCFunction)complex_is_finite, METH_NOARGS, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
953 complex_is_finite_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
|
954 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
955 {"__getnewargs__", (PyCFunction)complex_getnewargs, METH_NOARGS}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
956 {"__format__", (PyCFunction)complex__format__, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
957 METH_VARARGS, complex__format__doc}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
958 {NULL, NULL} /* sentinel */ |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
959 }; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
960 |
|
20012
3faabf54b8b8
Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents:
19974
diff
changeset
|
961 static PyMemberDef complex_members[] = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
962 {"real", T_DOUBLE, offsetof(PyComplexObject, cval.real), READONLY, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
963 "the real part of a complex number"}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
964 {"imag", T_DOUBLE, offsetof(PyComplexObject, cval.imag), READONLY, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
965 "the imaginary part of a complex number"}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
966 {0}, |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
967 }; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
968 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
969 static PyObject * |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
970 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
|
971 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
972 const char *s, *start; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
973 char *end; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
974 double x=0.0, y=0.0, z; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
975 int got_bracket=0; |
|
20666
88a242bfb23a
complex_subtype_from_string(): move the declaration of s_buffer[] out
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
976 #ifdef Py_USING_UNICODE |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
977 char *s_buffer = NULL; |
|
20666
88a242bfb23a
complex_subtype_from_string(): move the declaration of s_buffer[] out
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
978 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
979 Py_ssize_t len; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
980 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
981 if (PyString_Check(v)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
982 s = PyString_AS_STRING(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
983 len = PyString_GET_SIZE(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
984 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
985 #ifdef Py_USING_UNICODE |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
986 else if (PyUnicode_Check(v)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
987 s_buffer = (char *)PyMem_MALLOC(PyUnicode_GET_SIZE(v)+1); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
988 if (s_buffer == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
989 return PyErr_NoMemory(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
990 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v), |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
991 PyUnicode_GET_SIZE(v), |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
992 s_buffer, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
993 NULL)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
994 goto error; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
995 s = s_buffer; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
996 len = strlen(s); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
997 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
998 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
999 else if (PyObject_AsCharBuffer(v, &s, &len)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1000 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1001 "complex() arg is not a string"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1002 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1003 } |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1004 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1005 /* position on first nonblank */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1006 start = s; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1007 while (Py_ISSPACE(*s)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1008 s++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1009 if (*s == '(') { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1010 /* Skip over possible bracket from repr(). */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1011 got_bracket = 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1012 s++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1013 while (Py_ISSPACE(*s)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1014 s++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1015 } |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1016 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1017 /* a valid complex string usually takes one of the three forms: |
| 53338 | 1018 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1019 <float> - real part only |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1020 <float>j - imaginary part only |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1021 <float><signed-float>j - real and imaginary parts |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1022 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1023 where <float> represents any numeric string that's accepted by the |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1024 float constructor (including 'nan', 'inf', 'infinity', etc.), and |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1025 <signed-float> is any string of the form <float> whose first |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1026 character is '+' or '-'. |
| 53338 | 1027 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1028 For backwards compatibility, the extra forms |
| 53338 | 1029 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1030 <float><sign>j |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1031 <sign>j |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1032 j |
| 53338 | 1033 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1034 are also accepted, though support for these forms may be removed from |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1035 a future version of Python. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1036 */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1037 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1038 /* first look for forms starting with <float> */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1039 z = PyOS_string_to_double(s, &end, NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1040 if (z == -1.0 && PyErr_Occurred()) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1041 if (PyErr_ExceptionMatches(PyExc_ValueError)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1042 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1043 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1044 goto error; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1045 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1046 if (end != s) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1047 /* all 4 forms starting with <float> land here */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1048 s = end; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1049 if (*s == '+' || *s == '-') { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1050 /* <float><signed-float>j | <float><sign>j */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1051 x = z; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1052 y = PyOS_string_to_double(s, &end, NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1053 if (y == -1.0 && PyErr_Occurred()) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1054 if (PyErr_ExceptionMatches(PyExc_ValueError)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1055 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1056 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1057 goto error; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1058 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1059 if (end != s) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1060 /* <float><signed-float>j */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1061 s = end; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1062 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1063 /* <float><sign>j */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1064 y = *s == '+' ? 1.0 : -1.0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1065 s++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1066 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1067 if (!(*s == 'j' || *s == 'J')) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1068 goto parse_error; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1069 s++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1070 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1071 else if (*s == 'j' || *s == 'J') { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1072 /* <float>j */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1073 s++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1074 y = z; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1075 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1076 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1077 /* <float> */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1078 x = z; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1079 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1080 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1081 /* not starting with <float>; must be <sign>j or j */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1082 if (*s == '+' || *s == '-') { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1083 /* <sign>j */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1084 y = *s == '+' ? 1.0 : -1.0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1085 s++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1086 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1087 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1088 /* j */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1089 y = 1.0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1090 if (!(*s == 'j' || *s == 'J')) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1091 goto parse_error; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1092 s++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1093 } |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1094 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1095 /* trailing whitespace and closing bracket */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1096 while (Py_ISSPACE(*s)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1097 s++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1098 if (got_bracket) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1099 /* if there was an opening parenthesis, then the corresponding |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1100 closing parenthesis should be right here */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1101 if (*s != ')') |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1102 goto parse_error; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1103 s++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1104 while (Py_ISSPACE(*s)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1105 s++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1106 } |
| 53338 | 1107 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1108 /* we should now be at the end of the string */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1109 if (s-start != len) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1110 goto parse_error; |
| 53338 | 1111 |
|
56771
0b1a088012c7
Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents:
54227
diff
changeset
|
1112 |
|
0b1a088012c7
Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents:
54227
diff
changeset
|
1113 #ifdef Py_USING_UNICODE |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1114 if (s_buffer) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1115 PyMem_FREE(s_buffer); |
|
56771
0b1a088012c7
Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents:
54227
diff
changeset
|
1116 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1117 return complex_subtype_from_doubles(type, x, y); |
| 53338 | 1118 |
| 1119 parse_error: | |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1120 PyErr_SetString(PyExc_ValueError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1121 "complex() arg is a malformed string"); |
|
56771
0b1a088012c7
Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents:
54227
diff
changeset
|
1122 error: |
|
0b1a088012c7
Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents:
54227
diff
changeset
|
1123 #ifdef Py_USING_UNICODE |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1124 if (s_buffer) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1125 PyMem_FREE(s_buffer); |
|
56771
0b1a088012c7
Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents:
54227
diff
changeset
|
1126 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1127 return NULL; |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1128 } |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1129 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1130 static PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1131 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
|
1132 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1133 PyObject *r, *i, *tmp; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1134 PyNumberMethods *nbr, *nbi = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1135 Py_complex cr, ci; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1136 int own_r = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1137 int cr_is_complex = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1138 int ci_is_complex = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1139 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
|
1140 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1141 r = Py_False; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1142 i = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1143 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:complex", kwlist, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1144 &r, &i)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1145 return NULL; |
|
25064
81a36d2a2d13
complex() was the only numeric constructor that created a new instance
Raymond Hettinger <python@rcn.com>
parents:
24898
diff
changeset
|
1146 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1147 /* Special-case for a single argument when type(arg) is complex. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1148 if (PyComplex_CheckExact(r) && i == NULL && |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1149 type == &PyComplex_Type) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1150 /* Note that we can't know whether it's safe to return |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1151 a complex *subclass* instance as-is, hence the restriction |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1152 to exact complexes here. If either the input or the |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1153 output is a complex subclass, it will be handled below |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1154 as a non-orthogonal vector. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1155 Py_INCREF(r); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1156 return r; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1157 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1158 if (PyString_Check(r) || PyUnicode_Check(r)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1159 if (i != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1160 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1161 "complex() can't take second arg" |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1162 " if first is a string"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1163 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1164 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1165 return complex_subtype_from_string(type, r); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1166 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1167 if (i != NULL && (PyString_Check(i) || PyUnicode_Check(i))) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1168 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1169 "complex() second arg can't be a string"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1170 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1171 } |
|
19889
733314fcfdaa
Again perhaps the end of [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19758
diff
changeset
|
1172 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1173 tmp = try_complex_special_method(r); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1174 if (tmp) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1175 r = tmp; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1176 own_r = 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1177 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1178 else if (PyErr_Occurred()) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1179 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1180 } |
|
58142
f7e09ba0cb55
factor out __complex__ lookup code to fix another case
Benjamin Peterson <benjamin@python.org>
parents:
58141
diff
changeset
|
1181 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1182 nbr = r->ob_type->tp_as_number; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1183 if (i != NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1184 nbi = i->ob_type->tp_as_number; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1185 if (nbr == NULL || nbr->nb_float == NULL || |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1186 ((i != NULL) && (nbi == NULL || nbi->nb_float == NULL))) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1187 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1188 "complex() argument must be a string or a number"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1189 if (own_r) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1190 Py_DECREF(r); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1191 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1192 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1193 } |
|
41143
bed57dff3db8
Patch #1642844: comments to clarify the complexobject constructor.
Georg Brandl <georg@python.org>
parents:
41093
diff
changeset
|
1194 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1195 /* If we get this far, then the "real" and "imag" parts should |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1196 both be treated as numbers, and the constructor should return a |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1197 complex number equal to (real + imag*1j). |
|
41143
bed57dff3db8
Patch #1642844: comments to clarify the complexobject constructor.
Georg Brandl <georg@python.org>
parents:
41093
diff
changeset
|
1198 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1199 Note that we do NOT assume the input to already be in canonical |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1200 form; the "real" and "imag" parts might themselves be complex |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1201 numbers, which slightly complicates the code below. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1202 if (PyComplex_Check(r)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1203 /* Note that if r is of a complex subtype, we're only |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1204 retaining its real & imag parts here, and the return |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1205 value is (properly) of the builtin complex type. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1206 cr = ((PyComplexObject*)r)->cval; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1207 cr_is_complex = 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1208 if (own_r) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1209 Py_DECREF(r); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1210 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1211 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1212 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1213 /* The "real" part really is entirely real, and contributes |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1214 nothing in the imaginary direction. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1215 Just treat it as a double. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1216 tmp = PyNumber_Float(r); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1217 if (own_r) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1218 /* r was a newly created complex number, rather |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1219 than the original "real" argument. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1220 Py_DECREF(r); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1221 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1222 if (tmp == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1223 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1224 if (!PyFloat_Check(tmp)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1225 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1226 "float(r) didn't return a float"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1227 Py_DECREF(tmp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1228 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1229 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1230 cr.real = PyFloat_AsDouble(tmp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1231 cr.imag = 0.0; /* Shut up compiler warning */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1232 Py_DECREF(tmp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1233 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1234 if (i == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1235 ci.real = 0.0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1236 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1237 else if (PyComplex_Check(i)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1238 ci = ((PyComplexObject*)i)->cval; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1239 ci_is_complex = 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1240 } else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1241 /* The "imag" part really is entirely imaginary, and |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1242 contributes nothing in the real direction. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1243 Just treat it as a double. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1244 tmp = (*nbi->nb_float)(i); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1245 if (tmp == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1246 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1247 ci.real = PyFloat_AsDouble(tmp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1248 Py_DECREF(tmp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1249 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1250 /* If the input was in canonical form, then the "real" and "imag" |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1251 parts are real numbers, so that ci.imag and cr.imag are zero. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1252 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
|
1253 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1254 if (ci_is_complex) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1255 cr.real -= ci.imag; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1256 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1257 if (cr_is_complex) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1258 ci.real += cr.imag; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1259 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1260 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
|
1261 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
1262 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23820
diff
changeset
|
1263 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
|
1264 "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
|
1265 "\n" |
|
733314fcfdaa
Again perhaps the end of [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19758
diff
changeset
|
1266 "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
|
1267 "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
|
1268 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
1269 static PyNumberMethods complex_as_number = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1270 (binaryfunc)complex_add, /* nb_add */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1271 (binaryfunc)complex_sub, /* nb_subtract */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1272 (binaryfunc)complex_mul, /* nb_multiply */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1273 (binaryfunc)complex_classic_div, /* nb_divide */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1274 (binaryfunc)complex_remainder, /* nb_remainder */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1275 (binaryfunc)complex_divmod, /* nb_divmod */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1276 (ternaryfunc)complex_pow, /* nb_power */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1277 (unaryfunc)complex_neg, /* nb_negative */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1278 (unaryfunc)complex_pos, /* nb_positive */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1279 (unaryfunc)complex_abs, /* nb_absolute */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1280 (inquiry)complex_nonzero, /* nb_nonzero */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1281 0, /* nb_invert */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1282 0, /* nb_lshift */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1283 0, /* nb_rshift */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1284 0, /* nb_and */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1285 0, /* nb_xor */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1286 0, /* nb_or */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1287 complex_coerce, /* nb_coerce */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1288 complex_int, /* nb_int */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1289 complex_long, /* nb_long */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1290 complex_float, /* nb_float */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1291 0, /* nb_oct */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1292 0, /* nb_hex */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1293 0, /* nb_inplace_add */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1294 0, /* nb_inplace_subtract */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1295 0, /* nb_inplace_multiply*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1296 0, /* nb_inplace_divide */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1297 0, /* nb_inplace_remainder */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1298 0, /* nb_inplace_power */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1299 0, /* nb_inplace_lshift */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1300 0, /* nb_inplace_rshift */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1301 0, /* nb_inplace_and */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1302 0, /* nb_inplace_xor */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1303 0, /* nb_inplace_or */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1304 (binaryfunc)complex_int_div, /* nb_floor_divide */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1305 (binaryfunc)complex_div, /* nb_true_divide */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1306 0, /* nb_inplace_floor_divide */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1307 0, /* nb_inplace_true_divide */ |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1308 }; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1309 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5286
diff
changeset
|
1310 PyTypeObject PyComplex_Type = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1311 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1312 "complex", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1313 sizeof(PyComplexObject), |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1314 0, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1315 complex_dealloc, /* tp_dealloc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1316 (printfunc)complex_print, /* tp_print */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1317 0, /* tp_getattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1318 0, /* tp_setattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1319 0, /* tp_compare */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1320 (reprfunc)complex_repr, /* tp_repr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1321 &complex_as_number, /* tp_as_number */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1322 0, /* tp_as_sequence */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1323 0, /* tp_as_mapping */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1324 (hashfunc)complex_hash, /* tp_hash */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1325 0, /* tp_call */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1326 (reprfunc)complex_str, /* tp_str */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1327 PyObject_GenericGetAttr, /* tp_getattro */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1328 0, /* tp_setattro */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1329 0, /* tp_as_buffer */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1330 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1331 Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1332 complex_doc, /* tp_doc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1333 0, /* tp_traverse */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1334 0, /* tp_clear */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1335 complex_richcompare, /* tp_richcompare */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1336 0, /* tp_weaklistoffset */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1337 0, /* tp_iter */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1338 0, /* tp_iternext */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1339 complex_methods, /* tp_methods */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1340 complex_members, /* tp_members */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1341 0, /* tp_getset */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1342 0, /* tp_base */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1343 0, /* tp_dict */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1344 0, /* tp_descr_get */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1345 0, /* tp_descr_set */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1346 0, /* tp_dictoffset */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1347 0, /* tp_init */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1348 PyType_GenericAlloc, /* tp_alloc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1349 complex_new, /* tp_new */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59013
diff
changeset
|
1350 PyObject_Del, /* tp_free */ |
|
3316
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1351 }; |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1352 |
|
e5d1f242daf9
complex numbers a la Konrad Hinsen
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1353 #endif |
