blob: a08253e0dc5a52f808739e636156f5ec318585ac [file] [log] [blame]
Guido van Rossum96783941997-05-20 18:21:34 +00001
Guido van Rossumf9fca921996-01-12 00:47:05 +00002/* Complex object implementation */
3
4/* Borrows heavily from floatobject.c */
5
Guido van Rossum96783941997-05-20 18:21:34 +00006/* Submitted by Jim Hugunin */
7
Guido van Rossumc0b618a1997-05-02 03:12:38 +00008#include "Python.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +00009#include "structmember.h"
Guido van Rossumf9fca921996-01-12 00:47:05 +000010
Guido van Rossum65ce6de2002-06-13 17:07:07 +000011#ifndef WITHOUT_COMPLEX
12
Tim Peters70695122001-03-11 08:37:29 +000013/* Precisions used by repr() and str(), respectively.
14
15 The repr() precision (17 significant decimal digits) is the minimal number
16 that is guaranteed to have enough precision so that if the number is read
17 back in the exact same binary value is recreated. This is true for IEEE
18 floating point by design, and also happens to work for all other modern
19 hardware.
20
21 The str() precision is chosen so that in most cases, the rounding noise
22 created by various operations is suppressed, while giving plenty of
23 precision for practical use.
24*/
25
26#define PREC_REPR 17
27#define PREC_STR 12
Guido van Rossumf9fca921996-01-12 00:47:05 +000028
29/* elementary operations on complex numbers */
30
Guido van Rossum9e720e31996-07-21 02:31:35 +000031static Py_complex c_1 = {1., 0.};
Guido van Rossumf9fca921996-01-12 00:47:05 +000032
Tim Peters0f336042001-03-18 08:21:57 +000033Py_complex
34c_sum(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +000035{
Guido van Rossum9e720e31996-07-21 02:31:35 +000036 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000037 r.real = a.real + b.real;
38 r.imag = a.imag + b.imag;
39 return r;
40}
41
Tim Peters0f336042001-03-18 08:21:57 +000042Py_complex
43c_diff(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +000044{
Guido van Rossum9e720e31996-07-21 02:31:35 +000045 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000046 r.real = a.real - b.real;
47 r.imag = a.imag - b.imag;
48 return r;
49}
50
Tim Peters0f336042001-03-18 08:21:57 +000051Py_complex
52c_neg(Py_complex a)
Guido van Rossumf9fca921996-01-12 00:47:05 +000053{
Guido van Rossum9e720e31996-07-21 02:31:35 +000054 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000055 r.real = -a.real;
56 r.imag = -a.imag;
57 return r;
58}
59
Tim Peters0f336042001-03-18 08:21:57 +000060Py_complex
61c_prod(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +000062{
Guido van Rossum9e720e31996-07-21 02:31:35 +000063 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000064 r.real = a.real*b.real - a.imag*b.imag;
65 r.imag = a.real*b.imag + a.imag*b.real;
66 return r;
67}
68
Tim Peters0f336042001-03-18 08:21:57 +000069Py_complex
70c_quot(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +000071{
Tim Peters0f336042001-03-18 08:21:57 +000072 /******************************************************************
73 This was the original algorithm. It's grossly prone to spurious
74 overflow and underflow errors. It also merrily divides by 0 despite
75 checking for that(!). The code still serves a doc purpose here, as
76 the algorithm following is a simple by-cases transformation of this
77 one:
78
Guido van Rossum9e720e31996-07-21 02:31:35 +000079 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000080 double d = b.real*b.real + b.imag*b.imag;
81 if (d == 0.)
Guido van Rossum96783941997-05-20 18:21:34 +000082 errno = EDOM;
Guido van Rossumf9fca921996-01-12 00:47:05 +000083 r.real = (a.real*b.real + a.imag*b.imag)/d;
84 r.imag = (a.imag*b.real - a.real*b.imag)/d;
85 return r;
Tim Peters0f336042001-03-18 08:21:57 +000086 ******************************************************************/
87
88 /* This algorithm is better, and is pretty obvious: first divide the
89 * numerators and denominator by whichever of {b.real, b.imag} has
90 * larger magnitude. The earliest reference I found was to CACM
91 * Algorithm 116 (Complex Division, Robert L. Smith, Stanford
92 * University). As usual, though, we're still ignoring all IEEE
93 * endcases.
94 */
95 Py_complex r; /* the result */
96 const double abs_breal = b.real < 0 ? -b.real : b.real;
97 const double abs_bimag = b.imag < 0 ? -b.imag : b.imag;
98
99 if (abs_breal >= abs_bimag) {
100 /* divide tops and bottom by b.real */
101 if (abs_breal == 0.0) {
102 errno = EDOM;
103 r.real = r.imag = 0.0;
104 }
105 else {
106 const double ratio = b.imag / b.real;
107 const double denom = b.real + b.imag * ratio;
108 r.real = (a.real + a.imag * ratio) / denom;
109 r.imag = (a.imag - a.real * ratio) / denom;
110 }
111 }
112 else {
113 /* divide tops and bottom by b.imag */
114 const double ratio = b.real / b.imag;
115 const double denom = b.real * ratio + b.imag;
116 assert(b.imag != 0.0);
117 r.real = (a.real * ratio + a.imag) / denom;
118 r.imag = (a.imag * ratio - a.real) / denom;
119 }
120 return r;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000121}
122
Tim Peters0f336042001-03-18 08:21:57 +0000123Py_complex
124c_pow(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000125{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000126 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000127 double vabs,len,at,phase;
128 if (b.real == 0. && b.imag == 0.) {
129 r.real = 1.;
130 r.imag = 0.;
131 }
132 else if (a.real == 0. && a.imag == 0.) {
133 if (b.imag != 0. || b.real < 0.)
Tim Petersbab22be2002-03-22 02:48:46 +0000134 errno = EDOM;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000135 r.real = 0.;
136 r.imag = 0.;
137 }
138 else {
139 vabs = hypot(a.real,a.imag);
140 len = pow(vabs,b.real);
Martin v. Löwis387c5472001-09-06 08:16:17 +0000141 at = atan2(a.imag, a.real);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000142 phase = at*b.real;
143 if (b.imag != 0.0) {
144 len /= exp(at*b.imag);
145 phase += b.imag*log(vabs);
146 }
147 r.real = len*cos(phase);
148 r.imag = len*sin(phase);
149 }
150 return r;
151}
152
Tim Peters0f336042001-03-18 08:21:57 +0000153static Py_complex
154c_powu(Py_complex x, long n)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000155{
Guido van Rossum926518b1996-08-19 19:30:45 +0000156 Py_complex r, p;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000157 long mask = 1;
Guido van Rossum926518b1996-08-19 19:30:45 +0000158 r = c_1;
159 p = x;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000160 while (mask > 0 && n >= mask) {
161 if (n & mask)
162 r = c_prod(r,p);
163 mask <<= 1;
164 p = c_prod(p,p);
165 }
166 return r;
167}
168
Tim Peters0f336042001-03-18 08:21:57 +0000169static Py_complex
170c_powi(Py_complex x, long n)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000171{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000172 Py_complex cn;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000173
174 if (n > 100 || n < -100) {
175 cn.real = (double) n;
176 cn.imag = 0.;
177 return c_pow(x,cn);
178 }
179 else if (n > 0)
180 return c_powu(x,n);
181 else
182 return c_quot(c_1,c_powu(x,-n));
183
184}
185
Tim Peters6d6c1a32001-08-02 04:15:00 +0000186static PyObject *
187complex_subtype_from_c_complex(PyTypeObject *type, Py_complex cval)
188{
189 PyObject *op;
190
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000191 op = type->tp_alloc(type, 0);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000192 if (op != NULL)
193 ((PyComplexObject *)op)->cval = cval;
194 return op;
195}
196
Guido van Rossumf9fca921996-01-12 00:47:05 +0000197PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000198PyComplex_FromCComplex(Py_complex cval)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000199{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000200 register PyComplexObject *op;
201
Guido van Rossume3a8e7e2002-08-19 19:26:42 +0000202 /* Inline PyObject_New */
Guido van Rossumb18618d2000-05-03 23:44:39 +0000203 op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject));
Guido van Rossumf9fca921996-01-12 00:47:05 +0000204 if (op == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000205 return PyErr_NoMemory();
Guido van Rossumb18618d2000-05-03 23:44:39 +0000206 PyObject_INIT(op, &PyComplex_Type);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000207 op->cval = cval;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000208 return (PyObject *) op;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000209}
210
Tim Peters6d6c1a32001-08-02 04:15:00 +0000211static PyObject *
212complex_subtype_from_doubles(PyTypeObject *type, double real, double imag)
213{
214 Py_complex c;
215 c.real = real;
216 c.imag = imag;
217 return complex_subtype_from_c_complex(type, c);
218}
219
Guido van Rossumf9fca921996-01-12 00:47:05 +0000220PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000221PyComplex_FromDoubles(double real, double imag)
Guido van Rossum926518b1996-08-19 19:30:45 +0000222{
223 Py_complex c;
224 c.real = real;
225 c.imag = imag;
226 return PyComplex_FromCComplex(c);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000227}
228
229double
Fred Drake4288c802000-07-09 04:36:04 +0000230PyComplex_RealAsDouble(PyObject *op)
Guido van Rossum926518b1996-08-19 19:30:45 +0000231{
232 if (PyComplex_Check(op)) {
233 return ((PyComplexObject *)op)->cval.real;
Fred Drake4288c802000-07-09 04:36:04 +0000234 }
235 else {
Guido van Rossum926518b1996-08-19 19:30:45 +0000236 return PyFloat_AsDouble(op);
237 }
Guido van Rossumf9fca921996-01-12 00:47:05 +0000238}
239
240double
Fred Drake4288c802000-07-09 04:36:04 +0000241PyComplex_ImagAsDouble(PyObject *op)
Guido van Rossum926518b1996-08-19 19:30:45 +0000242{
243 if (PyComplex_Check(op)) {
244 return ((PyComplexObject *)op)->cval.imag;
Fred Drake4288c802000-07-09 04:36:04 +0000245 }
246 else {
Guido van Rossum926518b1996-08-19 19:30:45 +0000247 return 0.0;
248 }
Guido van Rossumf9fca921996-01-12 00:47:05 +0000249}
250
Guido van Rossum9e720e31996-07-21 02:31:35 +0000251Py_complex
Fred Drake4288c802000-07-09 04:36:04 +0000252PyComplex_AsCComplex(PyObject *op)
Guido van Rossum926518b1996-08-19 19:30:45 +0000253{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000254 Py_complex cv;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000255 PyObject *newop = NULL;
256 static PyObject *complex_str = NULL;
257
258 assert(op);
259 /* If op is already of type PyComplex_Type, return its value */
Guido van Rossumcf3d1081996-01-12 01:21:14 +0000260 if (PyComplex_Check(op)) {
261 return ((PyComplexObject *)op)->cval;
Fred Drake4288c802000-07-09 04:36:04 +0000262 }
Guido van Rossumd8faa362007-04-27 19:54:29 +0000263 /* If not, use op's __complex__ method, if it exists */
264
265 /* return -1 on failure */
266 cv.real = -1.;
267 cv.imag = 0.;
Christian Heimesfe82e772008-01-28 02:38:20 +0000268
269 if (complex_str == NULL) {
270 if (!(complex_str = PyUnicode_FromString("__complex__")))
271 return cv;
272 }
273
Guido van Rossumd8faa362007-04-27 19:54:29 +0000274 {
275 PyObject *complexfunc;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000276 complexfunc = _PyType_Lookup(op->ob_type, complex_str);
277 /* complexfunc is a borrowed reference */
278 if (complexfunc) {
279 newop = PyObject_CallFunctionObjArgs(complexfunc, op, NULL);
280 if (!newop)
281 return cv;
282 }
283 }
284
285 if (newop) {
286 if (!PyComplex_Check(newop)) {
287 PyErr_SetString(PyExc_TypeError,
288 "__complex__ should return a complex object");
289 Py_DECREF(newop);
290 return cv;
291 }
292 cv = ((PyComplexObject *)newop)->cval;
293 Py_DECREF(newop);
294 return cv;
295 }
296 /* If neither of the above works, interpret op as a float giving the
297 real part of the result, and fill in the imaginary part as 0. */
Fred Drake4288c802000-07-09 04:36:04 +0000298 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000299 /* PyFloat_AsDouble will return -1 on failure */
Guido van Rossumcf3d1081996-01-12 01:21:14 +0000300 cv.real = PyFloat_AsDouble(op);
Guido van Rossumcf3d1081996-01-12 01:21:14 +0000301 return cv;
Tim Peters70695122001-03-11 08:37:29 +0000302 }
Guido van Rossumcf3d1081996-01-12 01:21:14 +0000303}
304
Guido van Rossumf9fca921996-01-12 00:47:05 +0000305static void
Fred Drake4288c802000-07-09 04:36:04 +0000306complex_dealloc(PyObject *op)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000307{
Guido van Rossum9475a232001-10-05 20:51:39 +0000308 op->ob_type->tp_free(op);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000309}
310
311
Guido van Rossum363078a1996-05-24 20:45:01 +0000312static void
Barry Warsaw01d697a2001-11-28 20:50:56 +0000313complex_to_buf(char *buf, int bufsz, PyComplexObject *v, int precision)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000314{
Martin v. Löwis737ea822004-06-08 18:52:54 +0000315 char format[32];
316 if (v->cval.real == 0.) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000317 PyOS_snprintf(format, sizeof(format), "%%.%ig", precision);
318 PyOS_ascii_formatd(buf, bufsz - 1, format, v->cval.imag);
319 strncat(buf, "j", 1);
Martin v. Löwis737ea822004-06-08 18:52:54 +0000320 } else {
321 char re[64], im[64];
Georg Brandlc404ff22005-09-16 06:42:26 +0000322 /* Format imaginary part with sign, real part without */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000323 PyOS_snprintf(format, sizeof(format), "%%.%ig", precision);
324 PyOS_ascii_formatd(re, sizeof(re), format, v->cval.real);
325 PyOS_snprintf(format, sizeof(format), "%%+.%ig", precision);
326 PyOS_ascii_formatd(im, sizeof(im), format, v->cval.imag);
Georg Brandlc404ff22005-09-16 06:42:26 +0000327 PyOS_snprintf(buf, bufsz, "(%s%sj)", re, im);
Martin v. Löwis737ea822004-06-08 18:52:54 +0000328 }
Guido van Rossumf9fca921996-01-12 00:47:05 +0000329}
330
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000331static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000332complex_repr(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000333{
334 char buf[100];
Barry Warsaw01d697a2001-11-28 20:50:56 +0000335 complex_to_buf(buf, sizeof(buf), v, PREC_REPR);
Walter Dörwald1ab83302007-05-18 17:15:44 +0000336 return PyUnicode_FromString(buf);
Tim Peters70695122001-03-11 08:37:29 +0000337}
338
339static PyObject *
340complex_str(PyComplexObject *v)
341{
342 char buf[100];
Barry Warsaw01d697a2001-11-28 20:50:56 +0000343 complex_to_buf(buf, sizeof(buf), v, PREC_STR);
Walter Dörwald7696ed72007-05-31 15:51:35 +0000344 return PyUnicode_FromString(buf);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000345}
346
Guido van Rossumf9fca921996-01-12 00:47:05 +0000347static long
Fred Drake4288c802000-07-09 04:36:04 +0000348complex_hash(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000349{
Tim Peters39dce292000-08-15 03:34:48 +0000350 long hashreal, hashimag, combined;
351 hashreal = _Py_HashDouble(v->cval.real);
352 if (hashreal == -1)
353 return -1;
354 hashimag = _Py_HashDouble(v->cval.imag);
355 if (hashimag == -1)
356 return -1;
357 /* Note: if the imaginary part is 0, hashimag is 0 now,
358 * so the following returns hashreal unchanged. This is
359 * important because numbers of different types that
360 * compare equal must have the same hash value, so that
361 * hash(x + 0*j) must equal hash(x).
362 */
363 combined = hashreal + 1000003 * hashimag;
364 if (combined == -1)
365 combined = -2;
366 return combined;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000367}
368
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000369/* This macro may return! */
370#define TO_COMPLEX(obj, c) \
371 if (PyComplex_Check(obj)) \
372 c = ((PyComplexObject *)(obj))->cval; \
373 else if (to_complex(&(obj), &(c)) < 0) \
374 return (obj)
375
376static int
377to_complex(PyObject **pobj, Py_complex *pc)
378{
Christian Heimes587c2bf2008-01-19 16:21:02 +0000379 PyObject *obj = *pobj;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000380
Christian Heimes587c2bf2008-01-19 16:21:02 +0000381 pc->real = pc->imag = 0.0;
382 if (PyLong_Check(obj)) {
383 pc->real = PyLong_AsDouble(obj);
384 if (pc->real == -1.0 && PyErr_Occurred()) {
385 *pobj = NULL;
386 return -1;
387 }
388 return 0;
389 }
390 if (PyFloat_Check(obj)) {
391 pc->real = PyFloat_AsDouble(obj);
392 return 0;
393 }
394 Py_INCREF(Py_NotImplemented);
395 *pobj = Py_NotImplemented;
396 return -1;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000397}
398
399
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000400static PyObject *
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000401complex_add(PyObject *v, PyObject *w)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000402{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000403 Py_complex result;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000404 Py_complex a, b;
Christian Heimes587c2bf2008-01-19 16:21:02 +0000405 TO_COMPLEX(v, a);
406 TO_COMPLEX(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000407 PyFPE_START_PROTECT("complex_add", return 0)
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000408 result = c_sum(a, b);
Guido van Rossum45b83911997-03-14 04:32:50 +0000409 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000410 return PyComplex_FromCComplex(result);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000411}
412
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000413static PyObject *
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000414complex_sub(PyObject *v, PyObject *w)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000415{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000416 Py_complex result;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000417 Py_complex a, b;
Christian Heimes587c2bf2008-01-19 16:21:02 +0000418 TO_COMPLEX(v, a);
419 TO_COMPLEX(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000420 PyFPE_START_PROTECT("complex_sub", return 0)
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000421 result = c_diff(a, b);
Guido van Rossum45b83911997-03-14 04:32:50 +0000422 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000423 return PyComplex_FromCComplex(result);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000424}
425
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000426static PyObject *
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000427complex_mul(PyObject *v, PyObject *w)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000428{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000429 Py_complex result;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000430 Py_complex a, b;
Christian Heimes587c2bf2008-01-19 16:21:02 +0000431 TO_COMPLEX(v, a);
432 TO_COMPLEX(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000433 PyFPE_START_PROTECT("complex_mul", return 0)
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000434 result = c_prod(a, b);
Guido van Rossum45b83911997-03-14 04:32:50 +0000435 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000436 return PyComplex_FromCComplex(result);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000437}
438
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000439static PyObject *
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000440complex_div(PyObject *v, PyObject *w)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000441{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000442 Py_complex quot;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000443 Py_complex a, b;
Christian Heimes587c2bf2008-01-19 16:21:02 +0000444 TO_COMPLEX(v, a);
445 TO_COMPLEX(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000446 PyFPE_START_PROTECT("complex_div", return 0)
Guido van Rossum96783941997-05-20 18:21:34 +0000447 errno = 0;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000448 quot = c_quot(a, b);
Guido van Rossum45b83911997-03-14 04:32:50 +0000449 PyFPE_END_PROTECT(quot)
Guido van Rossum96783941997-05-20 18:21:34 +0000450 if (errno == EDOM) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000451 PyErr_SetString(PyExc_ZeroDivisionError, "complex division");
Guido van Rossumf9fca921996-01-12 00:47:05 +0000452 return NULL;
453 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000454 return PyComplex_FromCComplex(quot);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000455}
456
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000457static PyObject *
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000458complex_remainder(PyObject *v, PyObject *w)
Guido van Rossumee09fc11996-09-11 13:55:55 +0000459{
Guido van Rossum46334cd2007-08-01 17:43:15 +0000460 PyErr_SetString(PyExc_TypeError,
461 "can't mod complex numbers.");
462 return NULL;
Guido van Rossumee09fc11996-09-11 13:55:55 +0000463}
464
Guido van Rossumee09fc11996-09-11 13:55:55 +0000465
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000466static PyObject *
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000467complex_divmod(PyObject *v, PyObject *w)
Guido van Rossum3be12e91996-09-12 20:56:18 +0000468{
Guido van Rossum46334cd2007-08-01 17:43:15 +0000469 PyErr_SetString(PyExc_TypeError,
470 "can't take floor or mod of complex number.");
471 return NULL;
Guido van Rossum3be12e91996-09-12 20:56:18 +0000472}
Guido van Rossumf9fca921996-01-12 00:47:05 +0000473
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000474static PyObject *
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000475complex_pow(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000476{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000477 Py_complex p;
478 Py_complex exponent;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000479 long int_exponent;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000480 Py_complex a, b;
Christian Heimes587c2bf2008-01-19 16:21:02 +0000481 TO_COMPLEX(v, a);
482 TO_COMPLEX(w, b);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000483
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000484 if (z != Py_None) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000485 PyErr_SetString(PyExc_ValueError, "complex modulo");
Guido van Rossumf9fca921996-01-12 00:47:05 +0000486 return NULL;
487 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000488 PyFPE_START_PROTECT("complex_pow", return 0)
Guido van Rossum96783941997-05-20 18:21:34 +0000489 errno = 0;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000490 exponent = b;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000491 int_exponent = (long)exponent.real;
492 if (exponent.imag == 0. && exponent.real == int_exponent)
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000493 p = c_powi(a, int_exponent);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000494 else
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000495 p = c_pow(a, exponent);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000496
Guido van Rossum45b83911997-03-14 04:32:50 +0000497 PyFPE_END_PROTECT(p)
Tim Petersbab22be2002-03-22 02:48:46 +0000498 Py_ADJUST_ERANGE2(p.real, p.imag);
499 if (errno == EDOM) {
500 PyErr_SetString(PyExc_ZeroDivisionError,
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000501 "0.0 to a negative or complex power");
Guido van Rossumf9fca921996-01-12 00:47:05 +0000502 return NULL;
503 }
Tim Petersbab22be2002-03-22 02:48:46 +0000504 else if (errno == ERANGE) {
505 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +0000506 "complex exponentiation");
Tim Petersbab22be2002-03-22 02:48:46 +0000507 return NULL;
508 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000509 return PyComplex_FromCComplex(p);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000510}
511
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000512static PyObject *
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000513complex_int_div(PyObject *v, PyObject *w)
Guido van Rossum4668b002001-08-08 05:00:18 +0000514{
Guido van Rossum46334cd2007-08-01 17:43:15 +0000515 PyErr_SetString(PyExc_TypeError,
516 "can't take floor of complex number.");
Guido van Rossum4668b002001-08-08 05:00:18 +0000517 return NULL;
518}
519
520static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000521complex_neg(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000522{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000523 Py_complex neg;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000524 neg.real = -v->cval.real;
525 neg.imag = -v->cval.imag;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000526 return PyComplex_FromCComplex(neg);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000527}
528
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000529static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000530complex_pos(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000531{
Tim Peters2400fa42001-09-12 19:12:49 +0000532 if (PyComplex_CheckExact(v)) {
533 Py_INCREF(v);
534 return (PyObject *)v;
535 }
536 else
537 return PyComplex_FromCComplex(v->cval);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000538}
539
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000540static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000541complex_abs(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000542{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000543 double result;
544 PyFPE_START_PROTECT("complex_abs", return 0)
545 result = hypot(v->cval.real,v->cval.imag);
Guido van Rossum45b83911997-03-14 04:32:50 +0000546 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000547 return PyFloat_FromDouble(result);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000548}
549
550static int
Jack Diederich4dafcc42006-11-28 19:15:13 +0000551complex_bool(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000552{
Guido van Rossum3bbef601999-01-25 19:42:19 +0000553 return v->cval.real != 0.0 || v->cval.imag != 0.0;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000554}
555
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000556static PyObject *
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000557complex_richcompare(PyObject *v, PyObject *w, int op)
558{
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000559 PyObject *res;
Guido van Rossum18a67ba2006-08-21 18:27:07 +0000560 Py_complex i, j;
Christian Heimes587c2bf2008-01-19 16:21:02 +0000561 TO_COMPLEX(v, i);
562 TO_COMPLEX(w, j);
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000563
Guido van Rossum22056422001-09-24 17:52:04 +0000564 if (op != Py_EQ && op != Py_NE) {
Guido van Rossum18a67ba2006-08-21 18:27:07 +0000565 /* XXX Should eventually return NotImplemented */
Guido van Rossum22056422001-09-24 17:52:04 +0000566 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger5d01aa42004-12-19 20:45:20 +0000567 "no ordering relation is defined for complex numbers");
Guido van Rossum22056422001-09-24 17:52:04 +0000568 return NULL;
569 }
570
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000571 if ((i.real == j.real && i.imag == j.imag) == (op == Py_EQ))
572 res = Py_True;
573 else
574 res = Py_False;
575
576 Py_INCREF(res);
577 return res;
578}
579
580static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000581complex_int(PyObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000582{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000583 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger0970dba2003-08-30 23:57:36 +0000584 "can't convert complex to int; use int(abs(z))");
Guido van Rossumd4ab3cd1996-09-11 22:54:37 +0000585 return NULL;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000586}
587
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000588static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000589complex_long(PyObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000590{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000591 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger0970dba2003-08-30 23:57:36 +0000592 "can't convert complex to long; use long(abs(z))");
Guido van Rossumd4ab3cd1996-09-11 22:54:37 +0000593 return NULL;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000594}
595
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000596static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000597complex_float(PyObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000598{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000599 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger0970dba2003-08-30 23:57:36 +0000600 "can't convert complex to float; use abs(z)");
Guido van Rossumd4ab3cd1996-09-11 22:54:37 +0000601 return NULL;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000602}
603
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000604static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000605complex_conjugate(PyObject *self)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000606{
Guido van Rossum926518b1996-08-19 19:30:45 +0000607 Py_complex c;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000608 c = ((PyComplexObject *)self)->cval;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000609 c.imag = -c.imag;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000610 return PyComplex_FromCComplex(c);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000611}
612
Guido van Rossum46334cd2007-08-01 17:43:15 +0000613PyDoc_STRVAR(complex_conjugate_doc,
614"complex.conjugate() -> complex\n"
615"\n"
616"Returns the complex conjugate of its argument. (3-4j).conjugate() == 3+4j.");
617
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000618static PyObject *
619complex_getnewargs(PyComplexObject *v)
620{
Guido van Rossum4eadfa22003-03-02 13:51:47 +0000621 return Py_BuildValue("(D)", &v->cval);
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000622}
623
Guido van Rossumf9fca921996-01-12 00:47:05 +0000624static PyMethodDef complex_methods[] = {
Guido van Rossum46334cd2007-08-01 17:43:15 +0000625 {"conjugate", (PyCFunction)complex_conjugate, METH_NOARGS,
626 complex_conjugate_doc},
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000627 {"__getnewargs__", (PyCFunction)complex_getnewargs, METH_NOARGS},
Guido van Rossumf9fca921996-01-12 00:47:05 +0000628 {NULL, NULL} /* sentinel */
629};
630
Guido van Rossum6f799372001-09-20 20:46:19 +0000631static PyMemberDef complex_members[] = {
Guido van Rossumfa2e4c22002-02-08 21:26:07 +0000632 {"real", T_DOUBLE, offsetof(PyComplexObject, cval.real), READONLY,
Guido van Rossum6f799372001-09-20 20:46:19 +0000633 "the real part of a complex number"},
Guido van Rossumfa2e4c22002-02-08 21:26:07 +0000634 {"imag", T_DOUBLE, offsetof(PyComplexObject, cval.imag), READONLY,
Guido van Rossum6f799372001-09-20 20:46:19 +0000635 "the imaginary part of a complex number"},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000636 {0},
637};
Guido van Rossumf9fca921996-01-12 00:47:05 +0000638
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000639static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000640complex_subtype_from_string(PyTypeObject *type, PyObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000641{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000642 const char *s, *start;
643 char *end;
644 double x=0.0, y=0.0, z;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000645 int got_re=0, got_im=0, got_bracket=0, done=0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000646 int digit_or_dot;
647 int sw_error=0;
648 int sign;
649 char buffer[256]; /* For errors */
Guido van Rossum70e36882001-10-25 18:07:22 +0000650 char s_buffer[256];
Martin v. Löwis18e16552006-02-15 17:27:45 +0000651 Py_ssize_t len;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000652
Neal Norwitzed2b7392007-08-26 04:51:10 +0000653 if (PyUnicode_Check(v)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000654 if (PyUnicode_GET_SIZE(v) >= (Py_ssize_t)sizeof(s_buffer)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000655 PyErr_SetString(PyExc_ValueError,
656 "complex() literal too large to convert");
657 return NULL;
658 }
659 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
660 PyUnicode_GET_SIZE(v),
661 s_buffer,
662 NULL))
663 return NULL;
664 s = s_buffer;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000665 len = strlen(s);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000666 }
667 else if (PyObject_AsCharBuffer(v, &s, &len)) {
668 PyErr_SetString(PyExc_TypeError,
669 "complex() arg is not a string");
670 return NULL;
671 }
672
673 /* position on first nonblank */
674 start = s;
675 while (*s && isspace(Py_CHARMASK(*s)))
676 s++;
Christian Heimes587c2bf2008-01-19 16:21:02 +0000677 if (s[0] == '\0') {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000678 PyErr_SetString(PyExc_ValueError,
679 "complex() arg is an empty string");
680 return NULL;
Christian Heimes587c2bf2008-01-19 16:21:02 +0000681 }
Guido van Rossumd8faa362007-04-27 19:54:29 +0000682 if (s[0] == '(') {
683 /* Skip over possible bracket from repr(). */
684 got_bracket = 1;
685 s++;
686 while (*s && isspace(Py_CHARMASK(*s)))
687 s++;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000688 }
689
690 z = -1.0;
691 sign = 1;
692 do {
693
694 switch (*s) {
695
696 case '\0':
697 if (s-start != len) {
698 PyErr_SetString(
699 PyExc_ValueError,
700 "complex() arg contains a null byte");
701 return NULL;
702 }
703 if(!done) sw_error=1;
704 break;
705
Guido van Rossumd8faa362007-04-27 19:54:29 +0000706 case ')':
707 if (!got_bracket || !(got_re || got_im)) {
708 sw_error=1;
709 break;
710 }
711 got_bracket=0;
712 done=1;
713 s++;
714 while (*s && isspace(Py_CHARMASK(*s)))
715 s++;
716 if (*s) sw_error=1;
717 break;
718
Tim Peters6d6c1a32001-08-02 04:15:00 +0000719 case '-':
720 sign = -1;
721 /* Fallthrough */
722 case '+':
723 if (done) sw_error=1;
724 s++;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000725 if ( *s=='\0'||*s=='+'||*s=='-'||*s==')'||
Tim Peters6d6c1a32001-08-02 04:15:00 +0000726 isspace(Py_CHARMASK(*s)) ) sw_error=1;
727 break;
728
729 case 'J':
730 case 'j':
731 if (got_im || done) {
732 sw_error = 1;
733 break;
734 }
735 if (z<0.0) {
736 y=sign;
737 }
738 else{
739 y=sign*z;
740 }
741 got_im=1;
742 s++;
743 if (*s!='+' && *s!='-' )
744 done=1;
745 break;
746
747 default:
748 if (isspace(Py_CHARMASK(*s))) {
749 while (*s && isspace(Py_CHARMASK(*s)))
750 s++;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000751 if (*s && *s != ')')
Tim Peters6d6c1a32001-08-02 04:15:00 +0000752 sw_error=1;
753 else
754 done = 1;
755 break;
756 }
757 digit_or_dot =
758 (*s=='.' || isdigit(Py_CHARMASK(*s)));
759 if (done||!digit_or_dot) {
760 sw_error=1;
761 break;
762 }
763 errno = 0;
764 PyFPE_START_PROTECT("strtod", return 0)
Martin v. Löwis737ea822004-06-08 18:52:54 +0000765 z = PyOS_ascii_strtod(s, &end) ;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000766 PyFPE_END_PROTECT(z)
767 if (errno != 0) {
Barry Warsaw01d697a2001-11-28 20:50:56 +0000768 PyOS_snprintf(buffer, sizeof(buffer),
Tim Peters6d6c1a32001-08-02 04:15:00 +0000769 "float() out of range: %.150s", s);
770 PyErr_SetString(
771 PyExc_ValueError,
772 buffer);
773 return NULL;
774 }
775 s=end;
776 if (*s=='J' || *s=='j') {
777
778 break;
779 }
780 if (got_re) {
781 sw_error=1;
782 break;
783 }
784
785 /* accept a real part */
786 x=sign*z;
787 got_re=1;
788 if (got_im) done=1;
789 z = -1.0;
790 sign = 1;
791 break;
792
793 } /* end of switch */
794
Tim Peters077f2712002-04-14 22:04:03 +0000795 } while (s - start < len && !sw_error);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000796
Guido van Rossumd8faa362007-04-27 19:54:29 +0000797 if (sw_error || got_bracket) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000798 PyErr_SetString(PyExc_ValueError,
799 "complex() arg is a malformed string");
800 return NULL;
801 }
802
803 return complex_subtype_from_doubles(type, x, y);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000804}
805
Tim Peters6d6c1a32001-08-02 04:15:00 +0000806static PyObject *
807complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
808{
Raymond Hettinger478d47a2002-06-06 15:45:38 +0000809 PyObject *r, *i, *tmp, *f;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000810 PyNumberMethods *nbr, *nbi = NULL;
811 Py_complex cr, ci;
812 int own_r = 0;
Christian Heimes69a79632007-11-28 10:04:30 +0000813 int cr_is_complex = 0;
814 int ci_is_complex = 0;
Raymond Hettinger478d47a2002-06-06 15:45:38 +0000815 static PyObject *complexstr;
Martin v. Löwis15e62742006-02-27 16:46:16 +0000816 static char *kwlist[] = {"real", "imag", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000817
818 r = Py_False;
819 i = NULL;
820 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:complex", kwlist,
821 &r, &i))
822 return NULL;
Raymond Hettinger604cd6a2002-08-29 14:22:51 +0000823
Guido van Rossumd8faa362007-04-27 19:54:29 +0000824 /* Special-case for a single argument when type(arg) is complex. */
Guido van Rossum4eadfa22003-03-02 13:51:47 +0000825 if (PyComplex_CheckExact(r) && i == NULL &&
826 type == &PyComplex_Type) {
Raymond Hettinger604cd6a2002-08-29 14:22:51 +0000827 /* Note that we can't know whether it's safe to return
828 a complex *subclass* instance as-is, hence the restriction
Guido van Rossumd8faa362007-04-27 19:54:29 +0000829 to exact complexes here. If either the input or the
830 output is a complex subclass, it will be handled below
831 as a non-orthogonal vector. */
Raymond Hettinger604cd6a2002-08-29 14:22:51 +0000832 Py_INCREF(r);
833 return r;
834 }
Neal Norwitzed2b7392007-08-26 04:51:10 +0000835 if (PyUnicode_Check(r)) {
Fred Drake526c7a02001-12-13 19:52:22 +0000836 if (i != NULL) {
837 PyErr_SetString(PyExc_TypeError,
838 "complex() can't take second arg"
839 " if first is a string");
840 return NULL;
Christian Heimes587c2bf2008-01-19 16:21:02 +0000841 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000842 return complex_subtype_from_string(type, r);
Fred Drake526c7a02001-12-13 19:52:22 +0000843 }
Neal Norwitzed2b7392007-08-26 04:51:10 +0000844 if (i != NULL && PyUnicode_Check(i)) {
Fred Drake526c7a02001-12-13 19:52:22 +0000845 PyErr_SetString(PyExc_TypeError,
846 "complex() second arg can't be a string");
847 return NULL;
848 }
Tim Peters2400fa42001-09-12 19:12:49 +0000849
Raymond Hettinger478d47a2002-06-06 15:45:38 +0000850 /* XXX Hack to support classes with __complex__ method */
851 if (complexstr == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +0000852 complexstr = PyUnicode_InternFromString("__complex__");
Raymond Hettinger478d47a2002-06-06 15:45:38 +0000853 if (complexstr == NULL)
854 return NULL;
855 }
856 f = PyObject_GetAttr(r, complexstr);
857 if (f == NULL)
858 PyErr_Clear();
859 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000860 PyObject *args = PyTuple_New(0);
Raymond Hettinger478d47a2002-06-06 15:45:38 +0000861 if (args == NULL)
862 return NULL;
863 r = PyEval_CallObject(f, args);
864 Py_DECREF(args);
865 Py_DECREF(f);
866 if (r == NULL)
867 return NULL;
868 own_r = 1;
869 }
Tim Peters2400fa42001-09-12 19:12:49 +0000870 nbr = r->ob_type->tp_as_number;
871 if (i != NULL)
872 nbi = i->ob_type->tp_as_number;
873 if (nbr == NULL || nbr->nb_float == NULL ||
874 ((i != NULL) && (nbi == NULL || nbi->nb_float == NULL))) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000875 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger478d47a2002-06-06 15:45:38 +0000876 "complex() argument must be a string or a number");
Tim Peters465fa3d2003-08-15 01:16:37 +0000877 if (own_r) {
878 Py_DECREF(r);
879 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000880 return NULL;
881 }
Guido van Rossumd8faa362007-04-27 19:54:29 +0000882
883 /* If we get this far, then the "real" and "imag" parts should
884 both be treated as numbers, and the constructor should return a
885 complex number equal to (real + imag*1j).
886
887 Note that we do NOT assume the input to already be in canonical
888 form; the "real" and "imag" parts might themselves be complex
889 numbers, which slightly complicates the code below. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000890 if (PyComplex_Check(r)) {
Tim Peters2400fa42001-09-12 19:12:49 +0000891 /* Note that if r is of a complex subtype, we're only
892 retaining its real & imag parts here, and the return
893 value is (properly) of the builtin complex type. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000894 cr = ((PyComplexObject*)r)->cval;
Christian Heimes69a79632007-11-28 10:04:30 +0000895 cr_is_complex = 1;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000896 if (own_r) {
897 Py_DECREF(r);
898 }
899 }
900 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000901 /* The "real" part really is entirely real, and contributes
902 nothing in the imaginary direction.
903 Just treat it as a double. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000904 tmp = PyNumber_Float(r);
905 if (own_r) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000906 /* r was a newly created complex number, rather
907 than the original "real" argument. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000908 Py_DECREF(r);
909 }
910 if (tmp == NULL)
911 return NULL;
912 if (!PyFloat_Check(tmp)) {
913 PyErr_SetString(PyExc_TypeError,
914 "float(r) didn't return a float");
915 Py_DECREF(tmp);
916 return NULL;
917 }
918 cr.real = PyFloat_AsDouble(tmp);
Christian Heimes587c2bf2008-01-19 16:21:02 +0000919 cr.imag = 0.0; /* Shut up compiler warning */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000920 Py_DECREF(tmp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000921 }
922 if (i == NULL) {
923 ci.real = 0.0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000924 }
Christian Heimes69a79632007-11-28 10:04:30 +0000925 else if (PyComplex_Check(i)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000926 ci = ((PyComplexObject*)i)->cval;
Christian Heimes69a79632007-11-28 10:04:30 +0000927 ci_is_complex = 1;
928 } else {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000929 /* The "imag" part really is entirely imaginary, and
930 contributes nothing in the real direction.
931 Just treat it as a double. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000932 tmp = (*nbi->nb_float)(i);
933 if (tmp == NULL)
934 return NULL;
935 ci.real = PyFloat_AsDouble(tmp);
936 Py_DECREF(tmp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000937 }
Guido van Rossumd8faa362007-04-27 19:54:29 +0000938 /* If the input was in canonical form, then the "real" and "imag"
Christian Heimes69a79632007-11-28 10:04:30 +0000939 parts are real numbers, so that ci.imag and cr.imag are zero.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000940 We need this correction in case they were not real numbers. */
Christian Heimes69a79632007-11-28 10:04:30 +0000941
942 if (ci_is_complex) {
943 cr.real -= ci.imag;
944 }
945 if (cr_is_complex) {
946 ci.real += cr.imag;
947 }
948 return complex_subtype_from_doubles(type, cr.real, ci.real);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000949}
950
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000951PyDoc_STRVAR(complex_doc,
Tim Peters2400fa42001-09-12 19:12:49 +0000952"complex(real[, imag]) -> complex number\n"
953"\n"
954"Create a complex number from a real part and an optional imaginary part.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000955"This is equivalent to (real + imag*1j) where imag defaults to 0.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000956
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000957static PyNumberMethods complex_as_number = {
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000958 (binaryfunc)complex_add, /* nb_add */
959 (binaryfunc)complex_sub, /* nb_subtract */
960 (binaryfunc)complex_mul, /* nb_multiply */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000961 (binaryfunc)complex_remainder, /* nb_remainder */
962 (binaryfunc)complex_divmod, /* nb_divmod */
963 (ternaryfunc)complex_pow, /* nb_power */
964 (unaryfunc)complex_neg, /* nb_negative */
965 (unaryfunc)complex_pos, /* nb_positive */
966 (unaryfunc)complex_abs, /* nb_absolute */
Jack Diederich4dafcc42006-11-28 19:15:13 +0000967 (inquiry)complex_bool, /* nb_bool */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000968 0, /* nb_invert */
969 0, /* nb_lshift */
970 0, /* nb_rshift */
971 0, /* nb_and */
972 0, /* nb_xor */
973 0, /* nb_or */
Neil Schemenauer16c70752007-09-21 20:19:23 +0000974 0, /* nb_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000975 complex_int, /* nb_int */
976 complex_long, /* nb_long */
977 complex_float, /* nb_float */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000978 0, /* nb_oct */
979 0, /* nb_hex */
Guido van Rossum4668b002001-08-08 05:00:18 +0000980 0, /* nb_inplace_add */
981 0, /* nb_inplace_subtract */
982 0, /* nb_inplace_multiply*/
Guido van Rossum4668b002001-08-08 05:00:18 +0000983 0, /* nb_inplace_remainder */
984 0, /* nb_inplace_power */
985 0, /* nb_inplace_lshift */
986 0, /* nb_inplace_rshift */
987 0, /* nb_inplace_and */
988 0, /* nb_inplace_xor */
989 0, /* nb_inplace_or */
990 (binaryfunc)complex_int_div, /* nb_floor_divide */
991 (binaryfunc)complex_div, /* nb_true_divide */
992 0, /* nb_inplace_floor_divide */
993 0, /* nb_inplace_true_divide */
Guido van Rossumf9fca921996-01-12 00:47:05 +0000994};
995
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000996PyTypeObject PyComplex_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000997 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000998 "complex",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000999 sizeof(PyComplexObject),
Guido van Rossumf9fca921996-01-12 00:47:05 +00001000 0,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001001 complex_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00001002 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001003 0, /* tp_getattr */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001004 0, /* tp_setattr */
1005 0, /* tp_compare */
1006 (reprfunc)complex_repr, /* tp_repr */
1007 &complex_as_number, /* tp_as_number */
1008 0, /* tp_as_sequence */
1009 0, /* tp_as_mapping */
1010 (hashfunc)complex_hash, /* tp_hash */
1011 0, /* tp_call */
Tim Peters70695122001-03-11 08:37:29 +00001012 (reprfunc)complex_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001013 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001014 0, /* tp_setattro */
1015 0, /* tp_as_buffer */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001016 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1017 complex_doc, /* tp_doc */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001018 0, /* tp_traverse */
1019 0, /* tp_clear */
1020 complex_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001021 0, /* tp_weaklistoffset */
1022 0, /* tp_iter */
1023 0, /* tp_iternext */
1024 complex_methods, /* tp_methods */
1025 complex_members, /* tp_members */
1026 0, /* tp_getset */
1027 0, /* tp_base */
1028 0, /* tp_dict */
1029 0, /* tp_descr_get */
1030 0, /* tp_descr_set */
1031 0, /* tp_dictoffset */
1032 0, /* tp_init */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001033 PyType_GenericAlloc, /* tp_alloc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001034 complex_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00001035 PyObject_Del, /* tp_free */
Guido van Rossumf9fca921996-01-12 00:47:05 +00001036};
1037
1038#endif