blob: e5e07c1b6aa8d34b9561f880fcec301faebb09c6 [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
Christian Heimesbbe741d2008-03-28 10:53:29 +000011#ifdef HAVE_IEEEFP_H
12#include <ieeefp.h>
13#endif
14
Guido van Rossumf9fca921996-01-12 00:47:05 +000015/* elementary operations on complex numbers */
16
Guido van Rossum9e720e31996-07-21 02:31:35 +000017static Py_complex c_1 = {1., 0.};
Guido van Rossumf9fca921996-01-12 00:47:05 +000018
Tim Peters0f336042001-03-18 08:21:57 +000019Py_complex
20c_sum(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +000021{
Guido van Rossum9e720e31996-07-21 02:31:35 +000022 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000023 r.real = a.real + b.real;
24 r.imag = a.imag + b.imag;
25 return r;
26}
27
Tim Peters0f336042001-03-18 08:21:57 +000028Py_complex
29c_diff(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +000030{
Guido van Rossum9e720e31996-07-21 02:31:35 +000031 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000032 r.real = a.real - b.real;
33 r.imag = a.imag - b.imag;
34 return r;
35}
36
Tim Peters0f336042001-03-18 08:21:57 +000037Py_complex
38c_neg(Py_complex a)
Guido van Rossumf9fca921996-01-12 00:47:05 +000039{
Guido van Rossum9e720e31996-07-21 02:31:35 +000040 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000041 r.real = -a.real;
42 r.imag = -a.imag;
43 return r;
44}
45
Tim Peters0f336042001-03-18 08:21:57 +000046Py_complex
47c_prod(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +000048{
Guido van Rossum9e720e31996-07-21 02:31:35 +000049 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000050 r.real = a.real*b.real - a.imag*b.imag;
51 r.imag = a.real*b.imag + a.imag*b.real;
52 return r;
53}
54
Tim Peters0f336042001-03-18 08:21:57 +000055Py_complex
56c_quot(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +000057{
Tim Peters0f336042001-03-18 08:21:57 +000058 /******************************************************************
59 This was the original algorithm. It's grossly prone to spurious
60 overflow and underflow errors. It also merrily divides by 0 despite
61 checking for that(!). The code still serves a doc purpose here, as
62 the algorithm following is a simple by-cases transformation of this
63 one:
64
Guido van Rossum9e720e31996-07-21 02:31:35 +000065 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000066 double d = b.real*b.real + b.imag*b.imag;
67 if (d == 0.)
Guido van Rossum96783941997-05-20 18:21:34 +000068 errno = EDOM;
Guido van Rossumf9fca921996-01-12 00:47:05 +000069 r.real = (a.real*b.real + a.imag*b.imag)/d;
70 r.imag = (a.imag*b.real - a.real*b.imag)/d;
71 return r;
Tim Peters0f336042001-03-18 08:21:57 +000072 ******************************************************************/
73
74 /* This algorithm is better, and is pretty obvious: first divide the
75 * numerators and denominator by whichever of {b.real, b.imag} has
76 * larger magnitude. The earliest reference I found was to CACM
77 * Algorithm 116 (Complex Division, Robert L. Smith, Stanford
78 * University). As usual, though, we're still ignoring all IEEE
79 * endcases.
80 */
81 Py_complex r; /* the result */
82 const double abs_breal = b.real < 0 ? -b.real : b.real;
83 const double abs_bimag = b.imag < 0 ? -b.imag : b.imag;
84
85 if (abs_breal >= abs_bimag) {
86 /* divide tops and bottom by b.real */
87 if (abs_breal == 0.0) {
88 errno = EDOM;
89 r.real = r.imag = 0.0;
90 }
91 else {
92 const double ratio = b.imag / b.real;
93 const double denom = b.real + b.imag * ratio;
94 r.real = (a.real + a.imag * ratio) / denom;
95 r.imag = (a.imag - a.real * ratio) / denom;
96 }
97 }
98 else {
99 /* divide tops and bottom by b.imag */
100 const double ratio = b.real / b.imag;
101 const double denom = b.real * ratio + b.imag;
102 assert(b.imag != 0.0);
103 r.real = (a.real * ratio + a.imag) / denom;
104 r.imag = (a.imag * ratio - a.real) / denom;
105 }
106 return r;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000107}
108
Tim Peters0f336042001-03-18 08:21:57 +0000109Py_complex
110c_pow(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000111{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000112 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000113 double vabs,len,at,phase;
114 if (b.real == 0. && b.imag == 0.) {
115 r.real = 1.;
116 r.imag = 0.;
117 }
118 else if (a.real == 0. && a.imag == 0.) {
119 if (b.imag != 0. || b.real < 0.)
Tim Petersbab22be2002-03-22 02:48:46 +0000120 errno = EDOM;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000121 r.real = 0.;
122 r.imag = 0.;
123 }
124 else {
125 vabs = hypot(a.real,a.imag);
126 len = pow(vabs,b.real);
Martin v. Löwis387c5472001-09-06 08:16:17 +0000127 at = atan2(a.imag, a.real);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000128 phase = at*b.real;
129 if (b.imag != 0.0) {
130 len /= exp(at*b.imag);
131 phase += b.imag*log(vabs);
132 }
133 r.real = len*cos(phase);
134 r.imag = len*sin(phase);
135 }
136 return r;
137}
138
Tim Peters0f336042001-03-18 08:21:57 +0000139static Py_complex
140c_powu(Py_complex x, long n)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000141{
Guido van Rossum926518b1996-08-19 19:30:45 +0000142 Py_complex r, p;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000143 long mask = 1;
Guido van Rossum926518b1996-08-19 19:30:45 +0000144 r = c_1;
145 p = x;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000146 while (mask > 0 && n >= mask) {
147 if (n & mask)
148 r = c_prod(r,p);
149 mask <<= 1;
150 p = c_prod(p,p);
151 }
152 return r;
153}
154
Tim Peters0f336042001-03-18 08:21:57 +0000155static Py_complex
156c_powi(Py_complex x, long n)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000157{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000158 Py_complex cn;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000159
160 if (n > 100 || n < -100) {
161 cn.real = (double) n;
162 cn.imag = 0.;
163 return c_pow(x,cn);
164 }
165 else if (n > 0)
166 return c_powu(x,n);
167 else
168 return c_quot(c_1,c_powu(x,-n));
169
170}
171
Christian Heimes53876d92008-04-19 00:31:39 +0000172double
173c_abs(Py_complex z)
174{
175 /* sets errno = ERANGE on overflow; otherwise errno = 0 */
176 double result;
177
178 if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
179 /* C99 rules: if either the real or the imaginary part is an
180 infinity, return infinity, even if the other part is a
181 NaN. */
182 if (Py_IS_INFINITY(z.real)) {
183 result = fabs(z.real);
184 errno = 0;
185 return result;
186 }
187 if (Py_IS_INFINITY(z.imag)) {
188 result = fabs(z.imag);
189 errno = 0;
190 return result;
191 }
192 /* either the real or imaginary part is a NaN,
193 and neither is infinite. Result should be NaN. */
194 return Py_NAN;
195 }
196 result = hypot(z.real, z.imag);
197 if (!Py_IS_FINITE(result))
198 errno = ERANGE;
199 else
200 errno = 0;
201 return result;
202}
203
Tim Peters6d6c1a32001-08-02 04:15:00 +0000204static PyObject *
205complex_subtype_from_c_complex(PyTypeObject *type, Py_complex cval)
206{
207 PyObject *op;
208
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000209 op = type->tp_alloc(type, 0);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000210 if (op != NULL)
211 ((PyComplexObject *)op)->cval = cval;
212 return op;
213}
214
Guido van Rossumf9fca921996-01-12 00:47:05 +0000215PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000216PyComplex_FromCComplex(Py_complex cval)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000217{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000218 register PyComplexObject *op;
219
Guido van Rossume3a8e7e2002-08-19 19:26:42 +0000220 /* Inline PyObject_New */
Guido van Rossumb18618d2000-05-03 23:44:39 +0000221 op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject));
Guido van Rossumf9fca921996-01-12 00:47:05 +0000222 if (op == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000223 return PyErr_NoMemory();
Guido van Rossumb18618d2000-05-03 23:44:39 +0000224 PyObject_INIT(op, &PyComplex_Type);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000225 op->cval = cval;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000226 return (PyObject *) op;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000227}
228
Tim Peters6d6c1a32001-08-02 04:15:00 +0000229static PyObject *
230complex_subtype_from_doubles(PyTypeObject *type, double real, double imag)
231{
232 Py_complex c;
233 c.real = real;
234 c.imag = imag;
235 return complex_subtype_from_c_complex(type, c);
236}
237
Guido van Rossumf9fca921996-01-12 00:47:05 +0000238PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000239PyComplex_FromDoubles(double real, double imag)
Guido van Rossum926518b1996-08-19 19:30:45 +0000240{
241 Py_complex c;
242 c.real = real;
243 c.imag = imag;
244 return PyComplex_FromCComplex(c);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000245}
246
247double
Fred Drake4288c802000-07-09 04:36:04 +0000248PyComplex_RealAsDouble(PyObject *op)
Guido van Rossum926518b1996-08-19 19:30:45 +0000249{
250 if (PyComplex_Check(op)) {
251 return ((PyComplexObject *)op)->cval.real;
Fred Drake4288c802000-07-09 04:36:04 +0000252 }
253 else {
Guido van Rossum926518b1996-08-19 19:30:45 +0000254 return PyFloat_AsDouble(op);
255 }
Guido van Rossumf9fca921996-01-12 00:47:05 +0000256}
257
258double
Fred Drake4288c802000-07-09 04:36:04 +0000259PyComplex_ImagAsDouble(PyObject *op)
Guido van Rossum926518b1996-08-19 19:30:45 +0000260{
261 if (PyComplex_Check(op)) {
262 return ((PyComplexObject *)op)->cval.imag;
Fred Drake4288c802000-07-09 04:36:04 +0000263 }
264 else {
Guido van Rossum926518b1996-08-19 19:30:45 +0000265 return 0.0;
266 }
Guido van Rossumf9fca921996-01-12 00:47:05 +0000267}
268
Guido van Rossum9e720e31996-07-21 02:31:35 +0000269Py_complex
Fred Drake4288c802000-07-09 04:36:04 +0000270PyComplex_AsCComplex(PyObject *op)
Guido van Rossum926518b1996-08-19 19:30:45 +0000271{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000272 Py_complex cv;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000273 PyObject *newop = NULL;
274 static PyObject *complex_str = NULL;
275
276 assert(op);
277 /* If op is already of type PyComplex_Type, return its value */
Guido van Rossumcf3d1081996-01-12 01:21:14 +0000278 if (PyComplex_Check(op)) {
279 return ((PyComplexObject *)op)->cval;
Fred Drake4288c802000-07-09 04:36:04 +0000280 }
Guido van Rossumd8faa362007-04-27 19:54:29 +0000281 /* If not, use op's __complex__ method, if it exists */
282
283 /* return -1 on failure */
284 cv.real = -1.;
285 cv.imag = 0.;
Christian Heimesfe82e772008-01-28 02:38:20 +0000286
287 if (complex_str == NULL) {
288 if (!(complex_str = PyUnicode_FromString("__complex__")))
289 return cv;
290 }
291
Guido van Rossumd8faa362007-04-27 19:54:29 +0000292 {
293 PyObject *complexfunc;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000294 complexfunc = _PyType_Lookup(op->ob_type, complex_str);
295 /* complexfunc is a borrowed reference */
296 if (complexfunc) {
297 newop = PyObject_CallFunctionObjArgs(complexfunc, op, NULL);
298 if (!newop)
299 return cv;
300 }
301 }
302
303 if (newop) {
304 if (!PyComplex_Check(newop)) {
305 PyErr_SetString(PyExc_TypeError,
306 "__complex__ should return a complex object");
307 Py_DECREF(newop);
308 return cv;
309 }
310 cv = ((PyComplexObject *)newop)->cval;
311 Py_DECREF(newop);
312 return cv;
313 }
314 /* If neither of the above works, interpret op as a float giving the
315 real part of the result, and fill in the imaginary part as 0. */
Fred Drake4288c802000-07-09 04:36:04 +0000316 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000317 /* PyFloat_AsDouble will return -1 on failure */
Guido van Rossumcf3d1081996-01-12 01:21:14 +0000318 cv.real = PyFloat_AsDouble(op);
Guido van Rossumcf3d1081996-01-12 01:21:14 +0000319 return cv;
Tim Peters70695122001-03-11 08:37:29 +0000320 }
Guido van Rossumcf3d1081996-01-12 01:21:14 +0000321}
322
Guido van Rossumf9fca921996-01-12 00:47:05 +0000323static void
Fred Drake4288c802000-07-09 04:36:04 +0000324complex_dealloc(PyObject *op)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000325{
Guido van Rossum9475a232001-10-05 20:51:39 +0000326 op->ob_type->tp_free(op);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000327}
328
329
Eric Smith0923d1d2009-04-16 20:16:10 +0000330static PyObject *
Eric Smith63376222009-05-05 14:04:18 +0000331complex_format(PyComplexObject *v, int precision, char format_code)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000332{
Mark Dickinsonad476da2009-04-23 19:14:16 +0000333 PyObject *result = NULL;
334 Py_ssize_t len;
Eric Smith0923d1d2009-04-16 20:16:10 +0000335
Mark Dickinsonad476da2009-04-23 19:14:16 +0000336 /* If these are non-NULL, they'll need to be freed. */
337 char *pre = NULL;
338 char *im = NULL;
339 char *buf = NULL;
Eric Smith0923d1d2009-04-16 20:16:10 +0000340
Mark Dickinsonad476da2009-04-23 19:14:16 +0000341 /* These do not need to be freed. re is either an alias
342 for pre or a pointer to a constant. lead and tail
343 are pointers to constants. */
344 char *re = NULL;
345 char *lead = "";
346 char *tail = "";
Eric Smith0923d1d2009-04-16 20:16:10 +0000347
Mark Dickinsonad476da2009-04-23 19:14:16 +0000348 if (v->cval.real == 0. && copysign(1.0, v->cval.real)==1.0) {
349 re = "";
350 im = PyOS_double_to_string(v->cval.imag, format_code,
Eric Smith63376222009-05-05 14:04:18 +0000351 precision, 0, NULL);
Mark Dickinsonad476da2009-04-23 19:14:16 +0000352 if (!im) {
353 PyErr_NoMemory();
354 goto done;
355 }
356 } else {
357 /* Format imaginary part with sign, real part without */
358 pre = PyOS_double_to_string(v->cval.real, format_code,
Eric Smith63376222009-05-05 14:04:18 +0000359 precision, 0, NULL);
Mark Dickinsonad476da2009-04-23 19:14:16 +0000360 if (!pre) {
361 PyErr_NoMemory();
362 goto done;
363 }
364 re = pre;
Eric Smith0923d1d2009-04-16 20:16:10 +0000365
Mark Dickinsonad476da2009-04-23 19:14:16 +0000366 im = PyOS_double_to_string(v->cval.imag, format_code,
Eric Smith63376222009-05-05 14:04:18 +0000367 precision, Py_DTSF_SIGN, NULL);
Mark Dickinsonad476da2009-04-23 19:14:16 +0000368 if (!im) {
369 PyErr_NoMemory();
370 goto done;
371 }
372 lead = "(";
373 tail = ")";
374 }
375 /* Alloc the final buffer. Add one for the "j" in the format string,
376 and one for the trailing zero. */
377 len = strlen(lead) + strlen(re) + strlen(im) + strlen(tail) + 2;
378 buf = PyMem_Malloc(len);
379 if (!buf) {
380 PyErr_NoMemory();
381 goto done;
382 }
383 PyOS_snprintf(buf, len, "%s%s%sj%s", lead, re, im, tail);
384 result = PyUnicode_FromString(buf);
385 done:
386 PyMem_Free(im);
387 PyMem_Free(pre);
388 PyMem_Free(buf);
Eric Smith0923d1d2009-04-16 20:16:10 +0000389
Mark Dickinsonad476da2009-04-23 19:14:16 +0000390 return result;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000391}
392
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000393static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000394complex_repr(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000395{
Eric Smith63376222009-05-05 14:04:18 +0000396 return complex_format(v, 0, 'r');
Tim Peters70695122001-03-11 08:37:29 +0000397}
398
399static PyObject *
400complex_str(PyComplexObject *v)
401{
Eric Smith63376222009-05-05 14:04:18 +0000402 return complex_format(v, PyFloat_STR_PRECISION, 'g');
Guido van Rossumf9fca921996-01-12 00:47:05 +0000403}
404
Guido van Rossumf9fca921996-01-12 00:47:05 +0000405static long
Fred Drake4288c802000-07-09 04:36:04 +0000406complex_hash(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000407{
Tim Peters39dce292000-08-15 03:34:48 +0000408 long hashreal, hashimag, combined;
409 hashreal = _Py_HashDouble(v->cval.real);
410 if (hashreal == -1)
411 return -1;
412 hashimag = _Py_HashDouble(v->cval.imag);
413 if (hashimag == -1)
414 return -1;
415 /* Note: if the imaginary part is 0, hashimag is 0 now,
416 * so the following returns hashreal unchanged. This is
417 * important because numbers of different types that
418 * compare equal must have the same hash value, so that
419 * hash(x + 0*j) must equal hash(x).
420 */
421 combined = hashreal + 1000003 * hashimag;
422 if (combined == -1)
423 combined = -2;
424 return combined;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000425}
426
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000427/* This macro may return! */
428#define TO_COMPLEX(obj, c) \
429 if (PyComplex_Check(obj)) \
430 c = ((PyComplexObject *)(obj))->cval; \
431 else if (to_complex(&(obj), &(c)) < 0) \
432 return (obj)
433
434static int
435to_complex(PyObject **pobj, Py_complex *pc)
436{
Christian Heimes587c2bf2008-01-19 16:21:02 +0000437 PyObject *obj = *pobj;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000438
Christian Heimes587c2bf2008-01-19 16:21:02 +0000439 pc->real = pc->imag = 0.0;
440 if (PyLong_Check(obj)) {
441 pc->real = PyLong_AsDouble(obj);
442 if (pc->real == -1.0 && PyErr_Occurred()) {
443 *pobj = NULL;
444 return -1;
445 }
446 return 0;
447 }
448 if (PyFloat_Check(obj)) {
449 pc->real = PyFloat_AsDouble(obj);
450 return 0;
451 }
452 Py_INCREF(Py_NotImplemented);
453 *pobj = Py_NotImplemented;
454 return -1;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000455}
456
457
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000458static PyObject *
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000459complex_add(PyObject *v, PyObject *w)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000460{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000461 Py_complex result;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000462 Py_complex a, b;
Christian Heimes587c2bf2008-01-19 16:21:02 +0000463 TO_COMPLEX(v, a);
464 TO_COMPLEX(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000465 PyFPE_START_PROTECT("complex_add", return 0)
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000466 result = c_sum(a, b);
Guido van Rossum45b83911997-03-14 04:32:50 +0000467 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000468 return PyComplex_FromCComplex(result);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000469}
470
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000471static PyObject *
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000472complex_sub(PyObject *v, PyObject *w)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000473{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000474 Py_complex result;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000475 Py_complex a, b;
Christian Heimes587c2bf2008-01-19 16:21:02 +0000476 TO_COMPLEX(v, a);
477 TO_COMPLEX(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000478 PyFPE_START_PROTECT("complex_sub", return 0)
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000479 result = c_diff(a, b);
Guido van Rossum45b83911997-03-14 04:32:50 +0000480 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000481 return PyComplex_FromCComplex(result);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000482}
483
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000484static PyObject *
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000485complex_mul(PyObject *v, PyObject *w)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000486{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000487 Py_complex result;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000488 Py_complex a, b;
Christian Heimes587c2bf2008-01-19 16:21:02 +0000489 TO_COMPLEX(v, a);
490 TO_COMPLEX(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000491 PyFPE_START_PROTECT("complex_mul", return 0)
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000492 result = c_prod(a, b);
Guido van Rossum45b83911997-03-14 04:32:50 +0000493 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000494 return PyComplex_FromCComplex(result);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000495}
496
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000497static PyObject *
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000498complex_div(PyObject *v, PyObject *w)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000499{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000500 Py_complex quot;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000501 Py_complex a, b;
Christian Heimes587c2bf2008-01-19 16:21:02 +0000502 TO_COMPLEX(v, a);
503 TO_COMPLEX(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000504 PyFPE_START_PROTECT("complex_div", return 0)
Guido van Rossum96783941997-05-20 18:21:34 +0000505 errno = 0;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000506 quot = c_quot(a, b);
Guido van Rossum45b83911997-03-14 04:32:50 +0000507 PyFPE_END_PROTECT(quot)
Guido van Rossum96783941997-05-20 18:21:34 +0000508 if (errno == EDOM) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000509 PyErr_SetString(PyExc_ZeroDivisionError, "complex division");
Guido van Rossumf9fca921996-01-12 00:47:05 +0000510 return NULL;
511 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000512 return PyComplex_FromCComplex(quot);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000513}
514
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000515static PyObject *
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000516complex_remainder(PyObject *v, PyObject *w)
Guido van Rossumee09fc11996-09-11 13:55:55 +0000517{
Guido van Rossum46334cd2007-08-01 17:43:15 +0000518 PyErr_SetString(PyExc_TypeError,
519 "can't mod complex numbers.");
520 return NULL;
Guido van Rossumee09fc11996-09-11 13:55:55 +0000521}
522
Guido van Rossumee09fc11996-09-11 13:55:55 +0000523
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000524static PyObject *
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000525complex_divmod(PyObject *v, PyObject *w)
Guido van Rossum3be12e91996-09-12 20:56:18 +0000526{
Guido van Rossum46334cd2007-08-01 17:43:15 +0000527 PyErr_SetString(PyExc_TypeError,
528 "can't take floor or mod of complex number.");
529 return NULL;
Guido van Rossum3be12e91996-09-12 20:56:18 +0000530}
Guido van Rossumf9fca921996-01-12 00:47:05 +0000531
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000532static PyObject *
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000533complex_pow(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000534{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000535 Py_complex p;
536 Py_complex exponent;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000537 long int_exponent;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000538 Py_complex a, b;
Christian Heimes587c2bf2008-01-19 16:21:02 +0000539 TO_COMPLEX(v, a);
540 TO_COMPLEX(w, b);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000541
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000542 if (z != Py_None) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000543 PyErr_SetString(PyExc_ValueError, "complex modulo");
Guido van Rossumf9fca921996-01-12 00:47:05 +0000544 return NULL;
545 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000546 PyFPE_START_PROTECT("complex_pow", return 0)
Guido van Rossum96783941997-05-20 18:21:34 +0000547 errno = 0;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000548 exponent = b;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000549 int_exponent = (long)exponent.real;
550 if (exponent.imag == 0. && exponent.real == int_exponent)
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000551 p = c_powi(a, int_exponent);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000552 else
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000553 p = c_pow(a, exponent);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000554
Guido van Rossum45b83911997-03-14 04:32:50 +0000555 PyFPE_END_PROTECT(p)
Tim Petersbab22be2002-03-22 02:48:46 +0000556 Py_ADJUST_ERANGE2(p.real, p.imag);
557 if (errno == EDOM) {
558 PyErr_SetString(PyExc_ZeroDivisionError,
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000559 "0.0 to a negative or complex power");
Guido van Rossumf9fca921996-01-12 00:47:05 +0000560 return NULL;
561 }
Tim Petersbab22be2002-03-22 02:48:46 +0000562 else if (errno == ERANGE) {
563 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +0000564 "complex exponentiation");
Tim Petersbab22be2002-03-22 02:48:46 +0000565 return NULL;
566 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000567 return PyComplex_FromCComplex(p);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000568}
569
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000570static PyObject *
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000571complex_int_div(PyObject *v, PyObject *w)
Guido van Rossum4668b002001-08-08 05:00:18 +0000572{
Guido van Rossum46334cd2007-08-01 17:43:15 +0000573 PyErr_SetString(PyExc_TypeError,
574 "can't take floor of complex number.");
Guido van Rossum4668b002001-08-08 05:00:18 +0000575 return NULL;
576}
577
578static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000579complex_neg(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000580{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000581 Py_complex neg;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000582 neg.real = -v->cval.real;
583 neg.imag = -v->cval.imag;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000584 return PyComplex_FromCComplex(neg);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000585}
586
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000587static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000588complex_pos(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000589{
Tim Peters2400fa42001-09-12 19:12:49 +0000590 if (PyComplex_CheckExact(v)) {
591 Py_INCREF(v);
592 return (PyObject *)v;
593 }
594 else
595 return PyComplex_FromCComplex(v->cval);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000596}
597
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000598static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000599complex_abs(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000600{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000601 double result;
Christian Heimes53876d92008-04-19 00:31:39 +0000602
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000603 PyFPE_START_PROTECT("complex_abs", return 0)
Christian Heimes53876d92008-04-19 00:31:39 +0000604 result = c_abs(v->cval);
Guido van Rossum45b83911997-03-14 04:32:50 +0000605 PyFPE_END_PROTECT(result)
Christian Heimes53876d92008-04-19 00:31:39 +0000606
607 if (errno == ERANGE) {
608 PyErr_SetString(PyExc_OverflowError,
609 "absolute value too large");
610 return NULL;
611 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000612 return PyFloat_FromDouble(result);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000613}
614
615static int
Jack Diederich4dafcc42006-11-28 19:15:13 +0000616complex_bool(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000617{
Guido van Rossum3bbef601999-01-25 19:42:19 +0000618 return v->cval.real != 0.0 || v->cval.imag != 0.0;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000619}
620
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000621static PyObject *
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000622complex_richcompare(PyObject *v, PyObject *w, int op)
623{
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000624 PyObject *res;
Guido van Rossum18a67ba2006-08-21 18:27:07 +0000625 Py_complex i, j;
Christian Heimes587c2bf2008-01-19 16:21:02 +0000626 TO_COMPLEX(v, i);
627 TO_COMPLEX(w, j);
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000628
Guido van Rossum22056422001-09-24 17:52:04 +0000629 if (op != Py_EQ && op != Py_NE) {
Guido van Rossum18a67ba2006-08-21 18:27:07 +0000630 /* XXX Should eventually return NotImplemented */
Guido van Rossum22056422001-09-24 17:52:04 +0000631 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger5d01aa42004-12-19 20:45:20 +0000632 "no ordering relation is defined for complex numbers");
Guido van Rossum22056422001-09-24 17:52:04 +0000633 return NULL;
634 }
635
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000636 if ((i.real == j.real && i.imag == j.imag) == (op == Py_EQ))
637 res = Py_True;
638 else
639 res = Py_False;
640
641 Py_INCREF(res);
642 return res;
643}
644
645static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000646complex_int(PyObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000647{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000648 PyErr_SetString(PyExc_TypeError,
Mark Dickinson578cc742009-05-17 10:40:10 +0000649 "can't convert complex to int");
Guido van Rossumd4ab3cd1996-09-11 22:54:37 +0000650 return NULL;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000651}
652
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000653static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000654complex_float(PyObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000655{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000656 PyErr_SetString(PyExc_TypeError,
Mark Dickinson578cc742009-05-17 10:40:10 +0000657 "can't convert complex to float");
Guido van Rossumd4ab3cd1996-09-11 22:54:37 +0000658 return NULL;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000659}
660
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000661static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000662complex_conjugate(PyObject *self)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000663{
Guido van Rossum926518b1996-08-19 19:30:45 +0000664 Py_complex c;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000665 c = ((PyComplexObject *)self)->cval;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000666 c.imag = -c.imag;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000667 return PyComplex_FromCComplex(c);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000668}
669
Guido van Rossum46334cd2007-08-01 17:43:15 +0000670PyDoc_STRVAR(complex_conjugate_doc,
671"complex.conjugate() -> complex\n"
672"\n"
673"Returns the complex conjugate of its argument. (3-4j).conjugate() == 3+4j.");
674
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000675static PyObject *
676complex_getnewargs(PyComplexObject *v)
677{
Georg Brandl0c77a822008-06-10 16:37:50 +0000678 Py_complex c = v->cval;
679 return Py_BuildValue("(dd)", c.real, c.imag);
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000680}
681
Eric Smith58a42242009-04-30 01:00:33 +0000682PyDoc_STRVAR(complex__format__doc,
683"complex.__format__() -> str\n"
684"\n"
685"Converts to a string according to format_spec.");
686
687static PyObject *
688complex__format__(PyObject* self, PyObject* args)
689{
690 PyObject *format_spec;
691
692 if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
693 return NULL;
694 return _PyComplex_FormatAdvanced(self,
695 PyUnicode_AS_UNICODE(format_spec),
696 PyUnicode_GET_SIZE(format_spec));
697}
698
Christian Heimes53876d92008-04-19 00:31:39 +0000699#if 0
700static PyObject *
701complex_is_finite(PyObject *self)
702{
703 Py_complex c;
704 c = ((PyComplexObject *)self)->cval;
705 return PyBool_FromLong((long)(Py_IS_FINITE(c.real) &&
706 Py_IS_FINITE(c.imag)));
707}
708
709PyDoc_STRVAR(complex_is_finite_doc,
710"complex.is_finite() -> bool\n"
711"\n"
712"Returns True if the real and the imaginary part is finite.");
713#endif
714
Guido van Rossumf9fca921996-01-12 00:47:05 +0000715static PyMethodDef complex_methods[] = {
Guido van Rossum46334cd2007-08-01 17:43:15 +0000716 {"conjugate", (PyCFunction)complex_conjugate, METH_NOARGS,
717 complex_conjugate_doc},
Christian Heimes53876d92008-04-19 00:31:39 +0000718#if 0
719 {"is_finite", (PyCFunction)complex_is_finite, METH_NOARGS,
720 complex_is_finite_doc},
721#endif
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000722 {"__getnewargs__", (PyCFunction)complex_getnewargs, METH_NOARGS},
Eric Smith58a42242009-04-30 01:00:33 +0000723 {"__format__", (PyCFunction)complex__format__,
724 METH_VARARGS, complex__format__doc},
Guido van Rossumf9fca921996-01-12 00:47:05 +0000725 {NULL, NULL} /* sentinel */
726};
727
Guido van Rossum6f799372001-09-20 20:46:19 +0000728static PyMemberDef complex_members[] = {
Guido van Rossumfa2e4c22002-02-08 21:26:07 +0000729 {"real", T_DOUBLE, offsetof(PyComplexObject, cval.real), READONLY,
Guido van Rossum6f799372001-09-20 20:46:19 +0000730 "the real part of a complex number"},
Guido van Rossumfa2e4c22002-02-08 21:26:07 +0000731 {"imag", T_DOUBLE, offsetof(PyComplexObject, cval.imag), READONLY,
Guido van Rossum6f799372001-09-20 20:46:19 +0000732 "the imaginary part of a complex number"},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000733 {0},
734};
Guido van Rossumf9fca921996-01-12 00:47:05 +0000735
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000736static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000737complex_subtype_from_string(PyTypeObject *type, PyObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000738{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000739 const char *s, *start;
740 char *end;
741 double x=0.0, y=0.0, z;
Mark Dickinsonad476da2009-04-23 19:14:16 +0000742 int got_bracket=0;
Mark Dickinsonf9724882009-10-26 21:51:18 +0000743 char *s_buffer = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000744 Py_ssize_t len;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000745
Neal Norwitzed2b7392007-08-26 04:51:10 +0000746 if (PyUnicode_Check(v)) {
Mark Dickinsonf9724882009-10-26 21:51:18 +0000747 s_buffer = (char *)PyMem_MALLOC(PyUnicode_GET_SIZE(v) + 1);
748 if (s_buffer == NULL)
749 return PyErr_NoMemory();
Tim Peters6d6c1a32001-08-02 04:15:00 +0000750 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
751 PyUnicode_GET_SIZE(v),
752 s_buffer,
Mark Dickinson1daebdf2009-10-26 22:05:06 +0000753 NULL))
754 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000755 s = s_buffer;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000756 len = strlen(s);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000757 }
758 else if (PyObject_AsCharBuffer(v, &s, &len)) {
759 PyErr_SetString(PyExc_TypeError,
760 "complex() arg is not a string");
761 return NULL;
762 }
763
764 /* position on first nonblank */
765 start = s;
Mark Dickinsonaa77d262009-05-03 21:07:13 +0000766 while (Py_ISSPACE(*s))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000767 s++;
Mark Dickinsonad476da2009-04-23 19:14:16 +0000768 if (*s == '(') {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000769 /* Skip over possible bracket from repr(). */
770 got_bracket = 1;
771 s++;
Mark Dickinsonaa77d262009-05-03 21:07:13 +0000772 while (Py_ISSPACE(*s))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000773 s++;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000774 }
775
Mark Dickinson6649fa42009-04-24 13:25:20 +0000776 /* a valid complex string usually takes one of the three forms:
777
778 <float> - real part only
779 <float>j - imaginary part only
780 <float><signed-float>j - real and imaginary parts
781
782 where <float> represents any numeric string that's accepted by the
783 float constructor (including 'nan', 'inf', 'infinity', etc.), and
784 <signed-float> is any string of the form <float> whose first
785 character is '+' or '-'.
786
787 For backwards compatibility, the extra forms
788
789 <float><sign>j
790 <sign>j
791 j
792
793 are also accepted, though support for these forms may be removed from
794 a future version of Python.
795 */
796
797 /* first look for forms starting with <float> */
Mark Dickinson6b1e43b2009-05-20 18:41:04 +0000798 z = PyOS_string_to_double(s, &end, NULL);
Mark Dickinson725bfd82009-05-03 20:33:40 +0000799 if (z == -1.0 && PyErr_Occurred()) {
800 if (PyErr_ExceptionMatches(PyExc_ValueError))
801 PyErr_Clear();
802 else
Mark Dickinson1daebdf2009-10-26 22:05:06 +0000803 goto error;
Mark Dickinson725bfd82009-05-03 20:33:40 +0000804 }
Mark Dickinson6649fa42009-04-24 13:25:20 +0000805 if (end != s) {
806 /* all 4 forms starting with <float> land here */
807 s = end;
808 if (*s == '+' || *s == '-') {
809 /* <float><signed-float>j | <float><sign>j */
810 x = z;
Mark Dickinson6b1e43b2009-05-20 18:41:04 +0000811 y = PyOS_string_to_double(s, &end, NULL);
Mark Dickinson725bfd82009-05-03 20:33:40 +0000812 if (y == -1.0 && PyErr_Occurred()) {
813 if (PyErr_ExceptionMatches(PyExc_ValueError))
814 PyErr_Clear();
815 else
Mark Dickinson1daebdf2009-10-26 22:05:06 +0000816 goto error;
Mark Dickinson725bfd82009-05-03 20:33:40 +0000817 }
Mark Dickinson6649fa42009-04-24 13:25:20 +0000818 if (end != s)
819 /* <float><signed-float>j */
820 s = end;
821 else {
822 /* <float><sign>j */
823 y = *s == '+' ? 1.0 : -1.0;
824 s++;
825 }
826 if (!(*s == 'j' || *s == 'J'))
827 goto parse_error;
828 s++;
829 }
830 else if (*s == 'j' || *s == 'J') {
831 /* <float>j */
832 s++;
833 y = z;
834 }
835 else
836 /* <float> */
837 x = z;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000838 }
Mark Dickinson6649fa42009-04-24 13:25:20 +0000839 else {
840 /* not starting with <float>; must be <sign>j or j */
841 if (*s == '+' || *s == '-') {
842 /* <sign>j */
843 y = *s == '+' ? 1.0 : -1.0;
844 s++;
845 }
846 else
847 /* j */
848 y = 1.0;
849 if (!(*s == 'j' || *s == 'J'))
850 goto parse_error;
Mark Dickinsonad476da2009-04-23 19:14:16 +0000851 s++;
Mark Dickinsonad476da2009-04-23 19:14:16 +0000852 }
Mark Dickinsonad476da2009-04-23 19:14:16 +0000853
854 /* trailing whitespace and closing bracket */
Mark Dickinsonaa77d262009-05-03 21:07:13 +0000855 while (Py_ISSPACE(*s))
Mark Dickinsonad476da2009-04-23 19:14:16 +0000856 s++;
Mark Dickinson6649fa42009-04-24 13:25:20 +0000857 if (got_bracket) {
858 /* if there was an opening parenthesis, then the corresponding
859 closing parenthesis should be right here */
860 if (*s != ')')
861 goto parse_error;
Mark Dickinsonad476da2009-04-23 19:14:16 +0000862 s++;
Mark Dickinsonaa77d262009-05-03 21:07:13 +0000863 while (Py_ISSPACE(*s))
Mark Dickinson6649fa42009-04-24 13:25:20 +0000864 s++;
Mark Dickinsonad476da2009-04-23 19:14:16 +0000865 }
Mark Dickinson6649fa42009-04-24 13:25:20 +0000866
Mark Dickinsonad476da2009-04-23 19:14:16 +0000867 /* we should now be at the end of the string */
Mark Dickinson6649fa42009-04-24 13:25:20 +0000868 if (s-start != len)
869 goto parse_error;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000870
Mark Dickinsonf9724882009-10-26 21:51:18 +0000871 if (s_buffer)
872 PyMem_FREE(s_buffer);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000873 return complex_subtype_from_doubles(type, x, y);
Mark Dickinsonad476da2009-04-23 19:14:16 +0000874
Mark Dickinson6649fa42009-04-24 13:25:20 +0000875 parse_error:
Mark Dickinsonad476da2009-04-23 19:14:16 +0000876 PyErr_SetString(PyExc_ValueError,
877 "complex() arg is a malformed string");
Mark Dickinson1daebdf2009-10-26 22:05:06 +0000878 error:
879 if (s_buffer)
880 PyMem_FREE(s_buffer);
Mark Dickinsonad476da2009-04-23 19:14:16 +0000881 return NULL;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000882}
883
Tim Peters6d6c1a32001-08-02 04:15:00 +0000884static PyObject *
885complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
886{
Raymond Hettinger478d47a2002-06-06 15:45:38 +0000887 PyObject *r, *i, *tmp, *f;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000888 PyNumberMethods *nbr, *nbi = NULL;
889 Py_complex cr, ci;
890 int own_r = 0;
Christian Heimes69a79632007-11-28 10:04:30 +0000891 int cr_is_complex = 0;
892 int ci_is_complex = 0;
Raymond Hettinger478d47a2002-06-06 15:45:38 +0000893 static PyObject *complexstr;
Martin v. Löwis15e62742006-02-27 16:46:16 +0000894 static char *kwlist[] = {"real", "imag", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000895
896 r = Py_False;
897 i = NULL;
898 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:complex", kwlist,
899 &r, &i))
900 return NULL;
Raymond Hettinger604cd6a2002-08-29 14:22:51 +0000901
Guido van Rossumd8faa362007-04-27 19:54:29 +0000902 /* Special-case for a single argument when type(arg) is complex. */
Guido van Rossum4eadfa22003-03-02 13:51:47 +0000903 if (PyComplex_CheckExact(r) && i == NULL &&
904 type == &PyComplex_Type) {
Raymond Hettinger604cd6a2002-08-29 14:22:51 +0000905 /* Note that we can't know whether it's safe to return
906 a complex *subclass* instance as-is, hence the restriction
Guido van Rossumd8faa362007-04-27 19:54:29 +0000907 to exact complexes here. If either the input or the
908 output is a complex subclass, it will be handled below
909 as a non-orthogonal vector. */
Raymond Hettinger604cd6a2002-08-29 14:22:51 +0000910 Py_INCREF(r);
911 return r;
912 }
Neal Norwitzed2b7392007-08-26 04:51:10 +0000913 if (PyUnicode_Check(r)) {
Fred Drake526c7a02001-12-13 19:52:22 +0000914 if (i != NULL) {
915 PyErr_SetString(PyExc_TypeError,
916 "complex() can't take second arg"
917 " if first is a string");
918 return NULL;
Christian Heimes587c2bf2008-01-19 16:21:02 +0000919 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000920 return complex_subtype_from_string(type, r);
Fred Drake526c7a02001-12-13 19:52:22 +0000921 }
Neal Norwitzed2b7392007-08-26 04:51:10 +0000922 if (i != NULL && PyUnicode_Check(i)) {
Fred Drake526c7a02001-12-13 19:52:22 +0000923 PyErr_SetString(PyExc_TypeError,
924 "complex() second arg can't be a string");
925 return NULL;
926 }
Tim Peters2400fa42001-09-12 19:12:49 +0000927
Raymond Hettinger478d47a2002-06-06 15:45:38 +0000928 /* XXX Hack to support classes with __complex__ method */
929 if (complexstr == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +0000930 complexstr = PyUnicode_InternFromString("__complex__");
Raymond Hettinger478d47a2002-06-06 15:45:38 +0000931 if (complexstr == NULL)
932 return NULL;
933 }
934 f = PyObject_GetAttr(r, complexstr);
935 if (f == NULL)
936 PyErr_Clear();
937 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000938 PyObject *args = PyTuple_New(0);
Raymond Hettinger478d47a2002-06-06 15:45:38 +0000939 if (args == NULL)
940 return NULL;
941 r = PyEval_CallObject(f, args);
942 Py_DECREF(args);
943 Py_DECREF(f);
944 if (r == NULL)
945 return NULL;
946 own_r = 1;
947 }
Tim Peters2400fa42001-09-12 19:12:49 +0000948 nbr = r->ob_type->tp_as_number;
949 if (i != NULL)
950 nbi = i->ob_type->tp_as_number;
951 if (nbr == NULL || nbr->nb_float == NULL ||
952 ((i != NULL) && (nbi == NULL || nbi->nb_float == NULL))) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000953 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger478d47a2002-06-06 15:45:38 +0000954 "complex() argument must be a string or a number");
Tim Peters465fa3d2003-08-15 01:16:37 +0000955 if (own_r) {
956 Py_DECREF(r);
957 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000958 return NULL;
959 }
Guido van Rossumd8faa362007-04-27 19:54:29 +0000960
961 /* If we get this far, then the "real" and "imag" parts should
962 both be treated as numbers, and the constructor should return a
963 complex number equal to (real + imag*1j).
964
965 Note that we do NOT assume the input to already be in canonical
966 form; the "real" and "imag" parts might themselves be complex
967 numbers, which slightly complicates the code below. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000968 if (PyComplex_Check(r)) {
Tim Peters2400fa42001-09-12 19:12:49 +0000969 /* Note that if r is of a complex subtype, we're only
970 retaining its real & imag parts here, and the return
971 value is (properly) of the builtin complex type. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000972 cr = ((PyComplexObject*)r)->cval;
Christian Heimes69a79632007-11-28 10:04:30 +0000973 cr_is_complex = 1;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000974 if (own_r) {
975 Py_DECREF(r);
976 }
977 }
978 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000979 /* The "real" part really is entirely real, and contributes
980 nothing in the imaginary direction.
981 Just treat it as a double. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000982 tmp = PyNumber_Float(r);
983 if (own_r) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000984 /* r was a newly created complex number, rather
985 than the original "real" argument. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000986 Py_DECREF(r);
987 }
988 if (tmp == NULL)
989 return NULL;
990 if (!PyFloat_Check(tmp)) {
991 PyErr_SetString(PyExc_TypeError,
992 "float(r) didn't return a float");
993 Py_DECREF(tmp);
994 return NULL;
995 }
996 cr.real = PyFloat_AsDouble(tmp);
Christian Heimes587c2bf2008-01-19 16:21:02 +0000997 cr.imag = 0.0; /* Shut up compiler warning */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000998 Py_DECREF(tmp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000999 }
1000 if (i == NULL) {
1001 ci.real = 0.0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001002 }
Christian Heimes69a79632007-11-28 10:04:30 +00001003 else if (PyComplex_Check(i)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001004 ci = ((PyComplexObject*)i)->cval;
Christian Heimes69a79632007-11-28 10:04:30 +00001005 ci_is_complex = 1;
1006 } else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001007 /* The "imag" part really is entirely imaginary, and
1008 contributes nothing in the real direction.
1009 Just treat it as a double. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001010 tmp = (*nbi->nb_float)(i);
1011 if (tmp == NULL)
1012 return NULL;
1013 ci.real = PyFloat_AsDouble(tmp);
1014 Py_DECREF(tmp);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001015 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001016 /* If the input was in canonical form, then the "real" and "imag"
Christian Heimes69a79632007-11-28 10:04:30 +00001017 parts are real numbers, so that ci.imag and cr.imag are zero.
Guido van Rossumd8faa362007-04-27 19:54:29 +00001018 We need this correction in case they were not real numbers. */
Christian Heimes69a79632007-11-28 10:04:30 +00001019
1020 if (ci_is_complex) {
1021 cr.real -= ci.imag;
1022 }
1023 if (cr_is_complex) {
1024 ci.real += cr.imag;
1025 }
1026 return complex_subtype_from_doubles(type, cr.real, ci.real);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001027}
1028
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001029PyDoc_STRVAR(complex_doc,
Tim Peters2400fa42001-09-12 19:12:49 +00001030"complex(real[, imag]) -> complex number\n"
1031"\n"
1032"Create a complex number from a real part and an optional imaginary part.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001033"This is equivalent to (real + imag*1j) where imag defaults to 0.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001034
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001035static PyNumberMethods complex_as_number = {
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001036 (binaryfunc)complex_add, /* nb_add */
1037 (binaryfunc)complex_sub, /* nb_subtract */
1038 (binaryfunc)complex_mul, /* nb_multiply */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001039 (binaryfunc)complex_remainder, /* nb_remainder */
1040 (binaryfunc)complex_divmod, /* nb_divmod */
1041 (ternaryfunc)complex_pow, /* nb_power */
1042 (unaryfunc)complex_neg, /* nb_negative */
1043 (unaryfunc)complex_pos, /* nb_positive */
1044 (unaryfunc)complex_abs, /* nb_absolute */
Jack Diederich4dafcc42006-11-28 19:15:13 +00001045 (inquiry)complex_bool, /* nb_bool */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001046 0, /* nb_invert */
1047 0, /* nb_lshift */
1048 0, /* nb_rshift */
1049 0, /* nb_and */
1050 0, /* nb_xor */
1051 0, /* nb_or */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001052 complex_int, /* nb_int */
Mark Dickinson8055afd2009-01-17 10:04:45 +00001053 0, /* nb_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001054 complex_float, /* nb_float */
Guido van Rossum4668b002001-08-08 05:00:18 +00001055 0, /* nb_inplace_add */
1056 0, /* nb_inplace_subtract */
1057 0, /* nb_inplace_multiply*/
Guido van Rossum4668b002001-08-08 05:00:18 +00001058 0, /* nb_inplace_remainder */
1059 0, /* nb_inplace_power */
1060 0, /* nb_inplace_lshift */
1061 0, /* nb_inplace_rshift */
1062 0, /* nb_inplace_and */
1063 0, /* nb_inplace_xor */
1064 0, /* nb_inplace_or */
1065 (binaryfunc)complex_int_div, /* nb_floor_divide */
1066 (binaryfunc)complex_div, /* nb_true_divide */
1067 0, /* nb_inplace_floor_divide */
1068 0, /* nb_inplace_true_divide */
Guido van Rossumf9fca921996-01-12 00:47:05 +00001069};
1070
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001071PyTypeObject PyComplex_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001072 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumf9fca921996-01-12 00:47:05 +00001073 "complex",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001074 sizeof(PyComplexObject),
Guido van Rossumf9fca921996-01-12 00:47:05 +00001075 0,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001076 complex_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00001077 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001078 0, /* tp_getattr */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001079 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001080 0, /* tp_reserved */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001081 (reprfunc)complex_repr, /* tp_repr */
1082 &complex_as_number, /* tp_as_number */
1083 0, /* tp_as_sequence */
1084 0, /* tp_as_mapping */
1085 (hashfunc)complex_hash, /* tp_hash */
1086 0, /* tp_call */
Tim Peters70695122001-03-11 08:37:29 +00001087 (reprfunc)complex_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001088 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001089 0, /* tp_setattro */
1090 0, /* tp_as_buffer */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001091 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1092 complex_doc, /* tp_doc */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001093 0, /* tp_traverse */
1094 0, /* tp_clear */
1095 complex_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001096 0, /* tp_weaklistoffset */
1097 0, /* tp_iter */
1098 0, /* tp_iternext */
1099 complex_methods, /* tp_methods */
1100 complex_members, /* tp_members */
1101 0, /* tp_getset */
1102 0, /* tp_base */
1103 0, /* tp_dict */
1104 0, /* tp_descr_get */
1105 0, /* tp_descr_set */
1106 0, /* tp_dictoffset */
1107 0, /* tp_init */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001108 PyType_GenericAlloc, /* tp_alloc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001109 complex_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00001110 PyObject_Del, /* tp_free */
Guido van Rossumf9fca921996-01-12 00:47:05 +00001111};