blob: 0e55bc323c3f4e5d03f87412c3ac433978e7bcc0 [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 Rossumf9fca921996-01-12 00:47:05 +000011/* elementary operations on complex numbers */
12
Guido van Rossum9e720e31996-07-21 02:31:35 +000013static Py_complex c_1 = {1., 0.};
Guido van Rossumf9fca921996-01-12 00:47:05 +000014
Tim Peters0f336042001-03-18 08:21:57 +000015Py_complex
16c_sum(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +000017{
Guido van Rossum9e720e31996-07-21 02:31:35 +000018 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000019 r.real = a.real + b.real;
20 r.imag = a.imag + b.imag;
21 return r;
22}
23
Tim Peters0f336042001-03-18 08:21:57 +000024Py_complex
25c_diff(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +000026{
Guido van Rossum9e720e31996-07-21 02:31:35 +000027 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000028 r.real = a.real - b.real;
29 r.imag = a.imag - b.imag;
30 return r;
31}
32
Tim Peters0f336042001-03-18 08:21:57 +000033Py_complex
34c_neg(Py_complex a)
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;
38 r.imag = -a.imag;
39 return r;
40}
41
Tim Peters0f336042001-03-18 08:21:57 +000042Py_complex
43c_prod(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 - a.imag*b.imag;
47 r.imag = a.real*b.imag + a.imag*b.real;
48 return r;
49}
50
Tim Peters0f336042001-03-18 08:21:57 +000051Py_complex
52c_quot(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +000053{
Tim Peters0f336042001-03-18 08:21:57 +000054 /******************************************************************
55 This was the original algorithm. It's grossly prone to spurious
56 overflow and underflow errors. It also merrily divides by 0 despite
57 checking for that(!). The code still serves a doc purpose here, as
58 the algorithm following is a simple by-cases transformation of this
59 one:
60
Guido van Rossum9e720e31996-07-21 02:31:35 +000061 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000062 double d = b.real*b.real + b.imag*b.imag;
63 if (d == 0.)
Guido van Rossum96783941997-05-20 18:21:34 +000064 errno = EDOM;
Guido van Rossumf9fca921996-01-12 00:47:05 +000065 r.real = (a.real*b.real + a.imag*b.imag)/d;
66 r.imag = (a.imag*b.real - a.real*b.imag)/d;
67 return r;
Tim Peters0f336042001-03-18 08:21:57 +000068 ******************************************************************/
69
70 /* This algorithm is better, and is pretty obvious: first divide the
71 * numerators and denominator by whichever of {b.real, b.imag} has
72 * larger magnitude. The earliest reference I found was to CACM
73 * Algorithm 116 (Complex Division, Robert L. Smith, Stanford
74 * University). As usual, though, we're still ignoring all IEEE
75 * endcases.
76 */
77 Py_complex r; /* the result */
78 const double abs_breal = b.real < 0 ? -b.real : b.real;
79 const double abs_bimag = b.imag < 0 ? -b.imag : b.imag;
80
81 if (abs_breal >= abs_bimag) {
82 /* divide tops and bottom by b.real */
83 if (abs_breal == 0.0) {
84 errno = EDOM;
85 r.real = r.imag = 0.0;
86 }
87 else {
88 const double ratio = b.imag / b.real;
89 const double denom = b.real + b.imag * ratio;
90 r.real = (a.real + a.imag * ratio) / denom;
91 r.imag = (a.imag - a.real * ratio) / denom;
92 }
93 }
94 else {
95 /* divide tops and bottom by b.imag */
96 const double ratio = b.real / b.imag;
97 const double denom = b.real * ratio + b.imag;
98 assert(b.imag != 0.0);
99 r.real = (a.real * ratio + a.imag) / denom;
100 r.imag = (a.imag * ratio - a.real) / denom;
101 }
102 return r;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000103}
104
Tim Peters0f336042001-03-18 08:21:57 +0000105Py_complex
106c_pow(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000107{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000108 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000109 double vabs,len,at,phase;
110 if (b.real == 0. && b.imag == 0.) {
111 r.real = 1.;
112 r.imag = 0.;
113 }
114 else if (a.real == 0. && a.imag == 0.) {
115 if (b.imag != 0. || b.real < 0.)
Tim Petersbab22be2002-03-22 02:48:46 +0000116 errno = EDOM;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000117 r.real = 0.;
118 r.imag = 0.;
119 }
120 else {
121 vabs = hypot(a.real,a.imag);
122 len = pow(vabs,b.real);
Martin v. Löwis387c5472001-09-06 08:16:17 +0000123 at = atan2(a.imag, a.real);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000124 phase = at*b.real;
125 if (b.imag != 0.0) {
126 len /= exp(at*b.imag);
127 phase += b.imag*log(vabs);
128 }
129 r.real = len*cos(phase);
130 r.imag = len*sin(phase);
131 }
132 return r;
133}
134
Tim Peters0f336042001-03-18 08:21:57 +0000135static Py_complex
136c_powu(Py_complex x, long n)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000137{
Guido van Rossum926518b1996-08-19 19:30:45 +0000138 Py_complex r, p;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000139 long mask = 1;
Guido van Rossum926518b1996-08-19 19:30:45 +0000140 r = c_1;
141 p = x;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000142 while (mask > 0 && n >= mask) {
143 if (n & mask)
144 r = c_prod(r,p);
145 mask <<= 1;
146 p = c_prod(p,p);
147 }
148 return r;
149}
150
Tim Peters0f336042001-03-18 08:21:57 +0000151static Py_complex
152c_powi(Py_complex x, long n)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000153{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000154 Py_complex cn;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000155
156 if (n > 100 || n < -100) {
157 cn.real = (double) n;
158 cn.imag = 0.;
159 return c_pow(x,cn);
160 }
161 else if (n > 0)
162 return c_powu(x,n);
163 else
164 return c_quot(c_1,c_powu(x,-n));
165
166}
167
Christian Heimes53876d92008-04-19 00:31:39 +0000168double
169c_abs(Py_complex z)
170{
171 /* sets errno = ERANGE on overflow; otherwise errno = 0 */
172 double result;
173
174 if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
175 /* C99 rules: if either the real or the imaginary part is an
176 infinity, return infinity, even if the other part is a
177 NaN. */
178 if (Py_IS_INFINITY(z.real)) {
179 result = fabs(z.real);
180 errno = 0;
181 return result;
182 }
183 if (Py_IS_INFINITY(z.imag)) {
184 result = fabs(z.imag);
185 errno = 0;
186 return result;
187 }
188 /* either the real or imaginary part is a NaN,
189 and neither is infinite. Result should be NaN. */
190 return Py_NAN;
191 }
192 result = hypot(z.real, z.imag);
193 if (!Py_IS_FINITE(result))
194 errno = ERANGE;
195 else
196 errno = 0;
197 return result;
198}
199
Tim Peters6d6c1a32001-08-02 04:15:00 +0000200static PyObject *
201complex_subtype_from_c_complex(PyTypeObject *type, Py_complex cval)
202{
203 PyObject *op;
204
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000205 op = type->tp_alloc(type, 0);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000206 if (op != NULL)
207 ((PyComplexObject *)op)->cval = cval;
208 return op;
209}
210
Guido van Rossumf9fca921996-01-12 00:47:05 +0000211PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000212PyComplex_FromCComplex(Py_complex cval)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000213{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000214 register PyComplexObject *op;
215
Guido van Rossume3a8e7e2002-08-19 19:26:42 +0000216 /* Inline PyObject_New */
Guido van Rossumb18618d2000-05-03 23:44:39 +0000217 op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject));
Guido van Rossumf9fca921996-01-12 00:47:05 +0000218 if (op == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000219 return PyErr_NoMemory();
Guido van Rossumb18618d2000-05-03 23:44:39 +0000220 PyObject_INIT(op, &PyComplex_Type);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000221 op->cval = cval;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000222 return (PyObject *) op;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000223}
224
Tim Peters6d6c1a32001-08-02 04:15:00 +0000225static PyObject *
226complex_subtype_from_doubles(PyTypeObject *type, double real, double imag)
227{
228 Py_complex c;
229 c.real = real;
230 c.imag = imag;
231 return complex_subtype_from_c_complex(type, c);
232}
233
Guido van Rossumf9fca921996-01-12 00:47:05 +0000234PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000235PyComplex_FromDoubles(double real, double imag)
Guido van Rossum926518b1996-08-19 19:30:45 +0000236{
237 Py_complex c;
238 c.real = real;
239 c.imag = imag;
240 return PyComplex_FromCComplex(c);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000241}
242
243double
Fred Drake4288c802000-07-09 04:36:04 +0000244PyComplex_RealAsDouble(PyObject *op)
Guido van Rossum926518b1996-08-19 19:30:45 +0000245{
246 if (PyComplex_Check(op)) {
247 return ((PyComplexObject *)op)->cval.real;
Fred Drake4288c802000-07-09 04:36:04 +0000248 }
249 else {
Guido van Rossum926518b1996-08-19 19:30:45 +0000250 return PyFloat_AsDouble(op);
251 }
Guido van Rossumf9fca921996-01-12 00:47:05 +0000252}
253
254double
Fred Drake4288c802000-07-09 04:36:04 +0000255PyComplex_ImagAsDouble(PyObject *op)
Guido van Rossum926518b1996-08-19 19:30:45 +0000256{
257 if (PyComplex_Check(op)) {
258 return ((PyComplexObject *)op)->cval.imag;
Fred Drake4288c802000-07-09 04:36:04 +0000259 }
260 else {
Guido van Rossum926518b1996-08-19 19:30:45 +0000261 return 0.0;
262 }
Guido van Rossumf9fca921996-01-12 00:47:05 +0000263}
264
Benjamin Petersonaea44282010-01-04 01:10:28 +0000265static PyObject *
266try_complex_special_method(PyObject *op) {
267 PyObject *f;
268 static PyObject *complexstr;
269
270 f = _PyObject_LookupSpecial(op, "__complex__", &complexstr);
271 if (f) {
272 PyObject *res = PyObject_CallFunctionObjArgs(f, NULL);
273 Py_DECREF(f);
274 return res;
275 }
276 return NULL;
277}
278
Guido van Rossum9e720e31996-07-21 02:31:35 +0000279Py_complex
Fred Drake4288c802000-07-09 04:36:04 +0000280PyComplex_AsCComplex(PyObject *op)
Guido van Rossum926518b1996-08-19 19:30:45 +0000281{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000282 Py_complex cv;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000283 PyObject *newop = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000284
285 assert(op);
286 /* If op is already of type PyComplex_Type, return its value */
Guido van Rossumcf3d1081996-01-12 01:21:14 +0000287 if (PyComplex_Check(op)) {
288 return ((PyComplexObject *)op)->cval;
Fred Drake4288c802000-07-09 04:36:04 +0000289 }
Guido van Rossumd8faa362007-04-27 19:54:29 +0000290 /* If not, use op's __complex__ method, if it exists */
291
292 /* return -1 on failure */
293 cv.real = -1.;
294 cv.imag = 0.;
Christian Heimesfe82e772008-01-28 02:38:20 +0000295
Benjamin Petersonaea44282010-01-04 01:10:28 +0000296 newop = try_complex_special_method(op);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000297
298 if (newop) {
299 if (!PyComplex_Check(newop)) {
300 PyErr_SetString(PyExc_TypeError,
301 "__complex__ should return a complex object");
302 Py_DECREF(newop);
303 return cv;
304 }
305 cv = ((PyComplexObject *)newop)->cval;
306 Py_DECREF(newop);
307 return cv;
308 }
Benjamin Petersonaea44282010-01-04 01:10:28 +0000309 else if (PyErr_Occurred()) {
310 return cv;
311 }
Guido van Rossumd8faa362007-04-27 19:54:29 +0000312 /* If neither of the above works, interpret op as a float giving the
313 real part of the result, and fill in the imaginary part as 0. */
Fred Drake4288c802000-07-09 04:36:04 +0000314 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000315 /* PyFloat_AsDouble will return -1 on failure */
Guido van Rossumcf3d1081996-01-12 01:21:14 +0000316 cv.real = PyFloat_AsDouble(op);
Guido van Rossumcf3d1081996-01-12 01:21:14 +0000317 return cv;
Tim Peters70695122001-03-11 08:37:29 +0000318 }
Guido van Rossumcf3d1081996-01-12 01:21:14 +0000319}
320
Guido van Rossumf9fca921996-01-12 00:47:05 +0000321static void
Fred Drake4288c802000-07-09 04:36:04 +0000322complex_dealloc(PyObject *op)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000323{
Guido van Rossum9475a232001-10-05 20:51:39 +0000324 op->ob_type->tp_free(op);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000325}
326
327
Eric Smith0923d1d2009-04-16 20:16:10 +0000328static PyObject *
Eric Smith63376222009-05-05 14:04:18 +0000329complex_format(PyComplexObject *v, int precision, char format_code)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000330{
Mark Dickinsonad476da2009-04-23 19:14:16 +0000331 PyObject *result = NULL;
332 Py_ssize_t len;
Eric Smith0923d1d2009-04-16 20:16:10 +0000333
Mark Dickinsonad476da2009-04-23 19:14:16 +0000334 /* If these are non-NULL, they'll need to be freed. */
335 char *pre = NULL;
336 char *im = NULL;
337 char *buf = NULL;
Eric Smith0923d1d2009-04-16 20:16:10 +0000338
Mark Dickinsonad476da2009-04-23 19:14:16 +0000339 /* These do not need to be freed. re is either an alias
340 for pre or a pointer to a constant. lead and tail
341 are pointers to constants. */
342 char *re = NULL;
343 char *lead = "";
344 char *tail = "";
Eric Smith0923d1d2009-04-16 20:16:10 +0000345
Mark Dickinsonad476da2009-04-23 19:14:16 +0000346 if (v->cval.real == 0. && copysign(1.0, v->cval.real)==1.0) {
347 re = "";
348 im = PyOS_double_to_string(v->cval.imag, format_code,
Eric Smith63376222009-05-05 14:04:18 +0000349 precision, 0, NULL);
Mark Dickinsonad476da2009-04-23 19:14:16 +0000350 if (!im) {
351 PyErr_NoMemory();
352 goto done;
353 }
354 } else {
355 /* Format imaginary part with sign, real part without */
356 pre = PyOS_double_to_string(v->cval.real, format_code,
Eric Smith63376222009-05-05 14:04:18 +0000357 precision, 0, NULL);
Mark Dickinsonad476da2009-04-23 19:14:16 +0000358 if (!pre) {
359 PyErr_NoMemory();
360 goto done;
361 }
362 re = pre;
Eric Smith0923d1d2009-04-16 20:16:10 +0000363
Mark Dickinsonad476da2009-04-23 19:14:16 +0000364 im = PyOS_double_to_string(v->cval.imag, format_code,
Eric Smith63376222009-05-05 14:04:18 +0000365 precision, Py_DTSF_SIGN, NULL);
Mark Dickinsonad476da2009-04-23 19:14:16 +0000366 if (!im) {
367 PyErr_NoMemory();
368 goto done;
369 }
370 lead = "(";
371 tail = ")";
372 }
373 /* Alloc the final buffer. Add one for the "j" in the format string,
374 and one for the trailing zero. */
375 len = strlen(lead) + strlen(re) + strlen(im) + strlen(tail) + 2;
376 buf = PyMem_Malloc(len);
377 if (!buf) {
378 PyErr_NoMemory();
379 goto done;
380 }
381 PyOS_snprintf(buf, len, "%s%s%sj%s", lead, re, im, tail);
382 result = PyUnicode_FromString(buf);
383 done:
384 PyMem_Free(im);
385 PyMem_Free(pre);
386 PyMem_Free(buf);
Eric Smith0923d1d2009-04-16 20:16:10 +0000387
Mark Dickinsonad476da2009-04-23 19:14:16 +0000388 return result;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000389}
390
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000391static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000392complex_repr(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000393{
Eric Smith63376222009-05-05 14:04:18 +0000394 return complex_format(v, 0, 'r');
Tim Peters70695122001-03-11 08:37:29 +0000395}
396
397static PyObject *
398complex_str(PyComplexObject *v)
399{
Eric Smith63376222009-05-05 14:04:18 +0000400 return complex_format(v, PyFloat_STR_PRECISION, 'g');
Guido van Rossumf9fca921996-01-12 00:47:05 +0000401}
402
Guido van Rossumf9fca921996-01-12 00:47:05 +0000403static long
Fred Drake4288c802000-07-09 04:36:04 +0000404complex_hash(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000405{
Tim Peters39dce292000-08-15 03:34:48 +0000406 long hashreal, hashimag, combined;
407 hashreal = _Py_HashDouble(v->cval.real);
408 if (hashreal == -1)
409 return -1;
410 hashimag = _Py_HashDouble(v->cval.imag);
411 if (hashimag == -1)
412 return -1;
413 /* Note: if the imaginary part is 0, hashimag is 0 now,
414 * so the following returns hashreal unchanged. This is
415 * important because numbers of different types that
416 * compare equal must have the same hash value, so that
417 * hash(x + 0*j) must equal hash(x).
418 */
419 combined = hashreal + 1000003 * hashimag;
420 if (combined == -1)
421 combined = -2;
422 return combined;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000423}
424
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000425/* This macro may return! */
426#define TO_COMPLEX(obj, c) \
427 if (PyComplex_Check(obj)) \
428 c = ((PyComplexObject *)(obj))->cval; \
429 else if (to_complex(&(obj), &(c)) < 0) \
430 return (obj)
431
432static int
433to_complex(PyObject **pobj, Py_complex *pc)
434{
Christian Heimes587c2bf2008-01-19 16:21:02 +0000435 PyObject *obj = *pobj;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000436
Christian Heimes587c2bf2008-01-19 16:21:02 +0000437 pc->real = pc->imag = 0.0;
438 if (PyLong_Check(obj)) {
439 pc->real = PyLong_AsDouble(obj);
440 if (pc->real == -1.0 && PyErr_Occurred()) {
441 *pobj = NULL;
442 return -1;
443 }
444 return 0;
445 }
446 if (PyFloat_Check(obj)) {
447 pc->real = PyFloat_AsDouble(obj);
448 return 0;
449 }
450 Py_INCREF(Py_NotImplemented);
451 *pobj = Py_NotImplemented;
452 return -1;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000453}
454
455
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000456static PyObject *
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000457complex_add(PyObject *v, PyObject *w)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000458{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000459 Py_complex result;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000460 Py_complex a, b;
Christian Heimes587c2bf2008-01-19 16:21:02 +0000461 TO_COMPLEX(v, a);
462 TO_COMPLEX(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000463 PyFPE_START_PROTECT("complex_add", return 0)
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000464 result = c_sum(a, b);
Guido van Rossum45b83911997-03-14 04:32:50 +0000465 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000466 return PyComplex_FromCComplex(result);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000467}
468
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000469static PyObject *
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000470complex_sub(PyObject *v, PyObject *w)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000471{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000472 Py_complex result;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000473 Py_complex a, b;
Christian Heimes587c2bf2008-01-19 16:21:02 +0000474 TO_COMPLEX(v, a);
475 TO_COMPLEX(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000476 PyFPE_START_PROTECT("complex_sub", return 0)
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000477 result = c_diff(a, b);
Guido van Rossum45b83911997-03-14 04:32:50 +0000478 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000479 return PyComplex_FromCComplex(result);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000480}
481
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000482static PyObject *
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000483complex_mul(PyObject *v, PyObject *w)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000484{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000485 Py_complex result;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000486 Py_complex a, b;
Christian Heimes587c2bf2008-01-19 16:21:02 +0000487 TO_COMPLEX(v, a);
488 TO_COMPLEX(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000489 PyFPE_START_PROTECT("complex_mul", return 0)
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000490 result = c_prod(a, b);
Guido van Rossum45b83911997-03-14 04:32:50 +0000491 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000492 return PyComplex_FromCComplex(result);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000493}
494
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000495static PyObject *
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000496complex_div(PyObject *v, PyObject *w)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000497{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000498 Py_complex quot;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000499 Py_complex a, b;
Christian Heimes587c2bf2008-01-19 16:21:02 +0000500 TO_COMPLEX(v, a);
501 TO_COMPLEX(w, b);
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000502 PyFPE_START_PROTECT("complex_div", return 0)
Guido van Rossum96783941997-05-20 18:21:34 +0000503 errno = 0;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000504 quot = c_quot(a, b);
Guido van Rossum45b83911997-03-14 04:32:50 +0000505 PyFPE_END_PROTECT(quot)
Guido van Rossum96783941997-05-20 18:21:34 +0000506 if (errno == EDOM) {
Ezio Melotti50363262010-02-22 16:34:50 +0000507 PyErr_SetString(PyExc_ZeroDivisionError, "complex division by zero");
Guido van Rossumf9fca921996-01-12 00:47:05 +0000508 return NULL;
509 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000510 return PyComplex_FromCComplex(quot);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000511}
512
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000513static PyObject *
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000514complex_remainder(PyObject *v, PyObject *w)
Guido van Rossumee09fc11996-09-11 13:55:55 +0000515{
Guido van Rossum46334cd2007-08-01 17:43:15 +0000516 PyErr_SetString(PyExc_TypeError,
517 "can't mod complex numbers.");
518 return NULL;
Guido van Rossumee09fc11996-09-11 13:55:55 +0000519}
520
Guido van Rossumee09fc11996-09-11 13:55:55 +0000521
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000522static PyObject *
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000523complex_divmod(PyObject *v, PyObject *w)
Guido van Rossum3be12e91996-09-12 20:56:18 +0000524{
Guido van Rossum46334cd2007-08-01 17:43:15 +0000525 PyErr_SetString(PyExc_TypeError,
526 "can't take floor or mod of complex number.");
527 return NULL;
Guido van Rossum3be12e91996-09-12 20:56:18 +0000528}
Guido van Rossumf9fca921996-01-12 00:47:05 +0000529
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000530static PyObject *
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000531complex_pow(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000532{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000533 Py_complex p;
534 Py_complex exponent;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000535 long int_exponent;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000536 Py_complex a, b;
Christian Heimes587c2bf2008-01-19 16:21:02 +0000537 TO_COMPLEX(v, a);
538 TO_COMPLEX(w, b);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000539
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000540 if (z != Py_None) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000541 PyErr_SetString(PyExc_ValueError, "complex modulo");
Guido van Rossumf9fca921996-01-12 00:47:05 +0000542 return NULL;
543 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000544 PyFPE_START_PROTECT("complex_pow", return 0)
Guido van Rossum96783941997-05-20 18:21:34 +0000545 errno = 0;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000546 exponent = b;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000547 int_exponent = (long)exponent.real;
548 if (exponent.imag == 0. && exponent.real == int_exponent)
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000549 p = c_powi(a, int_exponent);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000550 else
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000551 p = c_pow(a, exponent);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000552
Guido van Rossum45b83911997-03-14 04:32:50 +0000553 PyFPE_END_PROTECT(p)
Tim Petersbab22be2002-03-22 02:48:46 +0000554 Py_ADJUST_ERANGE2(p.real, p.imag);
555 if (errno == EDOM) {
556 PyErr_SetString(PyExc_ZeroDivisionError,
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000557 "0.0 to a negative or complex power");
Guido van Rossumf9fca921996-01-12 00:47:05 +0000558 return NULL;
559 }
Tim Petersbab22be2002-03-22 02:48:46 +0000560 else if (errno == ERANGE) {
561 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +0000562 "complex exponentiation");
Tim Petersbab22be2002-03-22 02:48:46 +0000563 return NULL;
564 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000565 return PyComplex_FromCComplex(p);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000566}
567
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000568static PyObject *
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000569complex_int_div(PyObject *v, PyObject *w)
Guido van Rossum4668b002001-08-08 05:00:18 +0000570{
Guido van Rossum46334cd2007-08-01 17:43:15 +0000571 PyErr_SetString(PyExc_TypeError,
572 "can't take floor of complex number.");
Guido van Rossum4668b002001-08-08 05:00:18 +0000573 return NULL;
574}
575
576static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000577complex_neg(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000578{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000579 Py_complex neg;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000580 neg.real = -v->cval.real;
581 neg.imag = -v->cval.imag;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000582 return PyComplex_FromCComplex(neg);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000583}
584
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000585static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000586complex_pos(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000587{
Tim Peters2400fa42001-09-12 19:12:49 +0000588 if (PyComplex_CheckExact(v)) {
589 Py_INCREF(v);
590 return (PyObject *)v;
591 }
592 else
593 return PyComplex_FromCComplex(v->cval);
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_abs(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000598{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000599 double result;
Christian Heimes53876d92008-04-19 00:31:39 +0000600
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000601 PyFPE_START_PROTECT("complex_abs", return 0)
Christian Heimes53876d92008-04-19 00:31:39 +0000602 result = c_abs(v->cval);
Guido van Rossum45b83911997-03-14 04:32:50 +0000603 PyFPE_END_PROTECT(result)
Christian Heimes53876d92008-04-19 00:31:39 +0000604
605 if (errno == ERANGE) {
606 PyErr_SetString(PyExc_OverflowError,
607 "absolute value too large");
608 return NULL;
609 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000610 return PyFloat_FromDouble(result);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000611}
612
613static int
Jack Diederich4dafcc42006-11-28 19:15:13 +0000614complex_bool(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000615{
Guido van Rossum3bbef601999-01-25 19:42:19 +0000616 return v->cval.real != 0.0 || v->cval.imag != 0.0;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000617}
618
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000619static PyObject *
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000620complex_richcompare(PyObject *v, PyObject *w, int op)
621{
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000622 PyObject *res;
Guido van Rossum18a67ba2006-08-21 18:27:07 +0000623 Py_complex i, j;
Christian Heimes587c2bf2008-01-19 16:21:02 +0000624 TO_COMPLEX(v, i);
625 TO_COMPLEX(w, j);
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000626
Guido van Rossum22056422001-09-24 17:52:04 +0000627 if (op != Py_EQ && op != Py_NE) {
Mark Dickinsonf673f0c2010-03-13 09:48:39 +0000628 Py_INCREF(Py_NotImplemented);
629 return Py_NotImplemented;
Guido van Rossum22056422001-09-24 17:52:04 +0000630 }
631
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000632 if ((i.real == j.real && i.imag == j.imag) == (op == Py_EQ))
633 res = Py_True;
634 else
635 res = Py_False;
636
637 Py_INCREF(res);
638 return res;
639}
640
641static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000642complex_int(PyObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000643{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000644 PyErr_SetString(PyExc_TypeError,
Mark Dickinson578cc742009-05-17 10:40:10 +0000645 "can't convert complex to int");
Guido van Rossumd4ab3cd1996-09-11 22:54:37 +0000646 return NULL;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000647}
648
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000649static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000650complex_float(PyObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000651{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000652 PyErr_SetString(PyExc_TypeError,
Mark Dickinson578cc742009-05-17 10:40:10 +0000653 "can't convert complex to float");
Guido van Rossumd4ab3cd1996-09-11 22:54:37 +0000654 return NULL;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000655}
656
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000657static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000658complex_conjugate(PyObject *self)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000659{
Guido van Rossum926518b1996-08-19 19:30:45 +0000660 Py_complex c;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000661 c = ((PyComplexObject *)self)->cval;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000662 c.imag = -c.imag;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000663 return PyComplex_FromCComplex(c);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000664}
665
Guido van Rossum46334cd2007-08-01 17:43:15 +0000666PyDoc_STRVAR(complex_conjugate_doc,
667"complex.conjugate() -> complex\n"
668"\n"
669"Returns the complex conjugate of its argument. (3-4j).conjugate() == 3+4j.");
670
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000671static PyObject *
672complex_getnewargs(PyComplexObject *v)
673{
Georg Brandl0c77a822008-06-10 16:37:50 +0000674 Py_complex c = v->cval;
675 return Py_BuildValue("(dd)", c.real, c.imag);
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000676}
677
Eric Smith58a42242009-04-30 01:00:33 +0000678PyDoc_STRVAR(complex__format__doc,
679"complex.__format__() -> str\n"
680"\n"
681"Converts to a string according to format_spec.");
682
683static PyObject *
684complex__format__(PyObject* self, PyObject* args)
685{
686 PyObject *format_spec;
687
688 if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
689 return NULL;
690 return _PyComplex_FormatAdvanced(self,
691 PyUnicode_AS_UNICODE(format_spec),
692 PyUnicode_GET_SIZE(format_spec));
693}
694
Christian Heimes53876d92008-04-19 00:31:39 +0000695#if 0
696static PyObject *
697complex_is_finite(PyObject *self)
698{
699 Py_complex c;
700 c = ((PyComplexObject *)self)->cval;
701 return PyBool_FromLong((long)(Py_IS_FINITE(c.real) &&
702 Py_IS_FINITE(c.imag)));
703}
704
705PyDoc_STRVAR(complex_is_finite_doc,
706"complex.is_finite() -> bool\n"
707"\n"
708"Returns True if the real and the imaginary part is finite.");
709#endif
710
Guido van Rossumf9fca921996-01-12 00:47:05 +0000711static PyMethodDef complex_methods[] = {
Guido van Rossum46334cd2007-08-01 17:43:15 +0000712 {"conjugate", (PyCFunction)complex_conjugate, METH_NOARGS,
713 complex_conjugate_doc},
Christian Heimes53876d92008-04-19 00:31:39 +0000714#if 0
715 {"is_finite", (PyCFunction)complex_is_finite, METH_NOARGS,
716 complex_is_finite_doc},
717#endif
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000718 {"__getnewargs__", (PyCFunction)complex_getnewargs, METH_NOARGS},
Eric Smith58a42242009-04-30 01:00:33 +0000719 {"__format__", (PyCFunction)complex__format__,
720 METH_VARARGS, complex__format__doc},
Guido van Rossumf9fca921996-01-12 00:47:05 +0000721 {NULL, NULL} /* sentinel */
722};
723
Guido van Rossum6f799372001-09-20 20:46:19 +0000724static PyMemberDef complex_members[] = {
Guido van Rossumfa2e4c22002-02-08 21:26:07 +0000725 {"real", T_DOUBLE, offsetof(PyComplexObject, cval.real), READONLY,
Guido van Rossum6f799372001-09-20 20:46:19 +0000726 "the real part of a complex number"},
Guido van Rossumfa2e4c22002-02-08 21:26:07 +0000727 {"imag", T_DOUBLE, offsetof(PyComplexObject, cval.imag), READONLY,
Guido van Rossum6f799372001-09-20 20:46:19 +0000728 "the imaginary part of a complex number"},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000729 {0},
730};
Guido van Rossumf9fca921996-01-12 00:47:05 +0000731
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000732static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000733complex_subtype_from_string(PyTypeObject *type, PyObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000734{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000735 const char *s, *start;
736 char *end;
737 double x=0.0, y=0.0, z;
Mark Dickinsonad476da2009-04-23 19:14:16 +0000738 int got_bracket=0;
Mark Dickinsonf9724882009-10-26 21:51:18 +0000739 char *s_buffer = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000740 Py_ssize_t len;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000741
Neal Norwitzed2b7392007-08-26 04:51:10 +0000742 if (PyUnicode_Check(v)) {
Mark Dickinsonf9724882009-10-26 21:51:18 +0000743 s_buffer = (char *)PyMem_MALLOC(PyUnicode_GET_SIZE(v) + 1);
744 if (s_buffer == NULL)
745 return PyErr_NoMemory();
Tim Peters6d6c1a32001-08-02 04:15:00 +0000746 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
747 PyUnicode_GET_SIZE(v),
748 s_buffer,
Mark Dickinson1daebdf2009-10-26 22:05:06 +0000749 NULL))
750 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000751 s = s_buffer;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000752 len = strlen(s);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000753 }
754 else if (PyObject_AsCharBuffer(v, &s, &len)) {
755 PyErr_SetString(PyExc_TypeError,
756 "complex() arg is not a string");
757 return NULL;
758 }
759
760 /* position on first nonblank */
761 start = s;
Mark Dickinsonaa77d262009-05-03 21:07:13 +0000762 while (Py_ISSPACE(*s))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000763 s++;
Mark Dickinsonad476da2009-04-23 19:14:16 +0000764 if (*s == '(') {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000765 /* Skip over possible bracket from repr(). */
766 got_bracket = 1;
767 s++;
Mark Dickinsonaa77d262009-05-03 21:07:13 +0000768 while (Py_ISSPACE(*s))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000769 s++;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000770 }
771
Mark Dickinson6649fa42009-04-24 13:25:20 +0000772 /* a valid complex string usually takes one of the three forms:
773
774 <float> - real part only
775 <float>j - imaginary part only
776 <float><signed-float>j - real and imaginary parts
777
778 where <float> represents any numeric string that's accepted by the
779 float constructor (including 'nan', 'inf', 'infinity', etc.), and
780 <signed-float> is any string of the form <float> whose first
781 character is '+' or '-'.
782
783 For backwards compatibility, the extra forms
784
785 <float><sign>j
786 <sign>j
787 j
788
789 are also accepted, though support for these forms may be removed from
790 a future version of Python.
791 */
792
793 /* first look for forms starting with <float> */
Mark Dickinson6b1e43b2009-05-20 18:41:04 +0000794 z = PyOS_string_to_double(s, &end, NULL);
Mark Dickinson725bfd82009-05-03 20:33:40 +0000795 if (z == -1.0 && PyErr_Occurred()) {
796 if (PyErr_ExceptionMatches(PyExc_ValueError))
797 PyErr_Clear();
798 else
Mark Dickinson1daebdf2009-10-26 22:05:06 +0000799 goto error;
Mark Dickinson725bfd82009-05-03 20:33:40 +0000800 }
Mark Dickinson6649fa42009-04-24 13:25:20 +0000801 if (end != s) {
802 /* all 4 forms starting with <float> land here */
803 s = end;
804 if (*s == '+' || *s == '-') {
805 /* <float><signed-float>j | <float><sign>j */
806 x = z;
Mark Dickinson6b1e43b2009-05-20 18:41:04 +0000807 y = PyOS_string_to_double(s, &end, NULL);
Mark Dickinson725bfd82009-05-03 20:33:40 +0000808 if (y == -1.0 && PyErr_Occurred()) {
809 if (PyErr_ExceptionMatches(PyExc_ValueError))
810 PyErr_Clear();
811 else
Mark Dickinson1daebdf2009-10-26 22:05:06 +0000812 goto error;
Mark Dickinson725bfd82009-05-03 20:33:40 +0000813 }
Mark Dickinson6649fa42009-04-24 13:25:20 +0000814 if (end != s)
815 /* <float><signed-float>j */
816 s = end;
817 else {
818 /* <float><sign>j */
819 y = *s == '+' ? 1.0 : -1.0;
820 s++;
821 }
822 if (!(*s == 'j' || *s == 'J'))
823 goto parse_error;
824 s++;
825 }
826 else if (*s == 'j' || *s == 'J') {
827 /* <float>j */
828 s++;
829 y = z;
830 }
831 else
832 /* <float> */
833 x = z;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000834 }
Mark Dickinson6649fa42009-04-24 13:25:20 +0000835 else {
836 /* not starting with <float>; must be <sign>j or j */
837 if (*s == '+' || *s == '-') {
838 /* <sign>j */
839 y = *s == '+' ? 1.0 : -1.0;
840 s++;
841 }
842 else
843 /* j */
844 y = 1.0;
845 if (!(*s == 'j' || *s == 'J'))
846 goto parse_error;
Mark Dickinsonad476da2009-04-23 19:14:16 +0000847 s++;
Mark Dickinsonad476da2009-04-23 19:14:16 +0000848 }
Mark Dickinsonad476da2009-04-23 19:14:16 +0000849
850 /* trailing whitespace and closing bracket */
Mark Dickinsonaa77d262009-05-03 21:07:13 +0000851 while (Py_ISSPACE(*s))
Mark Dickinsonad476da2009-04-23 19:14:16 +0000852 s++;
Mark Dickinson6649fa42009-04-24 13:25:20 +0000853 if (got_bracket) {
854 /* if there was an opening parenthesis, then the corresponding
855 closing parenthesis should be right here */
856 if (*s != ')')
857 goto parse_error;
Mark Dickinsonad476da2009-04-23 19:14:16 +0000858 s++;
Mark Dickinsonaa77d262009-05-03 21:07:13 +0000859 while (Py_ISSPACE(*s))
Mark Dickinson6649fa42009-04-24 13:25:20 +0000860 s++;
Mark Dickinsonad476da2009-04-23 19:14:16 +0000861 }
Mark Dickinson6649fa42009-04-24 13:25:20 +0000862
Mark Dickinsonad476da2009-04-23 19:14:16 +0000863 /* we should now be at the end of the string */
Mark Dickinson6649fa42009-04-24 13:25:20 +0000864 if (s-start != len)
865 goto parse_error;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000866
Mark Dickinsonf9724882009-10-26 21:51:18 +0000867 if (s_buffer)
868 PyMem_FREE(s_buffer);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000869 return complex_subtype_from_doubles(type, x, y);
Mark Dickinsonad476da2009-04-23 19:14:16 +0000870
Mark Dickinson6649fa42009-04-24 13:25:20 +0000871 parse_error:
Mark Dickinsonad476da2009-04-23 19:14:16 +0000872 PyErr_SetString(PyExc_ValueError,
873 "complex() arg is a malformed string");
Mark Dickinson1daebdf2009-10-26 22:05:06 +0000874 error:
875 if (s_buffer)
876 PyMem_FREE(s_buffer);
Mark Dickinsonad476da2009-04-23 19:14:16 +0000877 return NULL;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000878}
879
Tim Peters6d6c1a32001-08-02 04:15:00 +0000880static PyObject *
881complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
882{
Benjamin Petersonaea44282010-01-04 01:10:28 +0000883 PyObject *r, *i, *tmp;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000884 PyNumberMethods *nbr, *nbi = NULL;
885 Py_complex cr, ci;
886 int own_r = 0;
Christian Heimes69a79632007-11-28 10:04:30 +0000887 int cr_is_complex = 0;
888 int ci_is_complex = 0;
Martin v. Löwis15e62742006-02-27 16:46:16 +0000889 static char *kwlist[] = {"real", "imag", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000890
891 r = Py_False;
892 i = NULL;
893 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:complex", kwlist,
894 &r, &i))
895 return NULL;
Raymond Hettinger604cd6a2002-08-29 14:22:51 +0000896
Guido van Rossumd8faa362007-04-27 19:54:29 +0000897 /* Special-case for a single argument when type(arg) is complex. */
Guido van Rossum4eadfa22003-03-02 13:51:47 +0000898 if (PyComplex_CheckExact(r) && i == NULL &&
899 type == &PyComplex_Type) {
Raymond Hettinger604cd6a2002-08-29 14:22:51 +0000900 /* Note that we can't know whether it's safe to return
901 a complex *subclass* instance as-is, hence the restriction
Guido van Rossumd8faa362007-04-27 19:54:29 +0000902 to exact complexes here. If either the input or the
903 output is a complex subclass, it will be handled below
904 as a non-orthogonal vector. */
Raymond Hettinger604cd6a2002-08-29 14:22:51 +0000905 Py_INCREF(r);
906 return r;
907 }
Neal Norwitzed2b7392007-08-26 04:51:10 +0000908 if (PyUnicode_Check(r)) {
Fred Drake526c7a02001-12-13 19:52:22 +0000909 if (i != NULL) {
910 PyErr_SetString(PyExc_TypeError,
911 "complex() can't take second arg"
912 " if first is a string");
913 return NULL;
Christian Heimes587c2bf2008-01-19 16:21:02 +0000914 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000915 return complex_subtype_from_string(type, r);
Fred Drake526c7a02001-12-13 19:52:22 +0000916 }
Neal Norwitzed2b7392007-08-26 04:51:10 +0000917 if (i != NULL && PyUnicode_Check(i)) {
Fred Drake526c7a02001-12-13 19:52:22 +0000918 PyErr_SetString(PyExc_TypeError,
919 "complex() second arg can't be a string");
920 return NULL;
921 }
Tim Peters2400fa42001-09-12 19:12:49 +0000922
Benjamin Petersonaea44282010-01-04 01:10:28 +0000923 tmp = try_complex_special_method(r);
924 if (tmp) {
925 r = tmp;
Raymond Hettinger478d47a2002-06-06 15:45:38 +0000926 own_r = 1;
927 }
Benjamin Petersonaea44282010-01-04 01:10:28 +0000928 else if (PyErr_Occurred()) {
929 return NULL;
930 }
931
Tim Peters2400fa42001-09-12 19:12:49 +0000932 nbr = r->ob_type->tp_as_number;
933 if (i != NULL)
934 nbi = i->ob_type->tp_as_number;
935 if (nbr == NULL || nbr->nb_float == NULL ||
936 ((i != NULL) && (nbi == NULL || nbi->nb_float == NULL))) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000937 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger478d47a2002-06-06 15:45:38 +0000938 "complex() argument must be a string or a number");
Tim Peters465fa3d2003-08-15 01:16:37 +0000939 if (own_r) {
940 Py_DECREF(r);
941 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000942 return NULL;
943 }
Guido van Rossumd8faa362007-04-27 19:54:29 +0000944
945 /* If we get this far, then the "real" and "imag" parts should
946 both be treated as numbers, and the constructor should return a
947 complex number equal to (real + imag*1j).
948
949 Note that we do NOT assume the input to already be in canonical
950 form; the "real" and "imag" parts might themselves be complex
951 numbers, which slightly complicates the code below. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000952 if (PyComplex_Check(r)) {
Tim Peters2400fa42001-09-12 19:12:49 +0000953 /* Note that if r is of a complex subtype, we're only
954 retaining its real & imag parts here, and the return
955 value is (properly) of the builtin complex type. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000956 cr = ((PyComplexObject*)r)->cval;
Christian Heimes69a79632007-11-28 10:04:30 +0000957 cr_is_complex = 1;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000958 if (own_r) {
959 Py_DECREF(r);
960 }
961 }
962 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000963 /* The "real" part really is entirely real, and contributes
964 nothing in the imaginary direction.
965 Just treat it as a double. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000966 tmp = PyNumber_Float(r);
967 if (own_r) {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000968 /* r was a newly created complex number, rather
969 than the original "real" argument. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000970 Py_DECREF(r);
971 }
972 if (tmp == NULL)
973 return NULL;
974 if (!PyFloat_Check(tmp)) {
975 PyErr_SetString(PyExc_TypeError,
976 "float(r) didn't return a float");
977 Py_DECREF(tmp);
978 return NULL;
979 }
980 cr.real = PyFloat_AsDouble(tmp);
Christian Heimes587c2bf2008-01-19 16:21:02 +0000981 cr.imag = 0.0; /* Shut up compiler warning */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000982 Py_DECREF(tmp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000983 }
984 if (i == NULL) {
985 ci.real = 0.0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000986 }
Christian Heimes69a79632007-11-28 10:04:30 +0000987 else if (PyComplex_Check(i)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000988 ci = ((PyComplexObject*)i)->cval;
Christian Heimes69a79632007-11-28 10:04:30 +0000989 ci_is_complex = 1;
990 } else {
Guido van Rossumd8faa362007-04-27 19:54:29 +0000991 /* The "imag" part really is entirely imaginary, and
992 contributes nothing in the real direction.
993 Just treat it as a double. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000994 tmp = (*nbi->nb_float)(i);
995 if (tmp == NULL)
996 return NULL;
997 ci.real = PyFloat_AsDouble(tmp);
998 Py_DECREF(tmp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000999 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001000 /* If the input was in canonical form, then the "real" and "imag"
Christian Heimes69a79632007-11-28 10:04:30 +00001001 parts are real numbers, so that ci.imag and cr.imag are zero.
Guido van Rossumd8faa362007-04-27 19:54:29 +00001002 We need this correction in case they were not real numbers. */
Christian Heimes69a79632007-11-28 10:04:30 +00001003
1004 if (ci_is_complex) {
1005 cr.real -= ci.imag;
1006 }
1007 if (cr_is_complex) {
1008 ci.real += cr.imag;
1009 }
1010 return complex_subtype_from_doubles(type, cr.real, ci.real);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001011}
1012
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001013PyDoc_STRVAR(complex_doc,
Tim Peters2400fa42001-09-12 19:12:49 +00001014"complex(real[, imag]) -> complex number\n"
1015"\n"
1016"Create a complex number from a real part and an optional imaginary part.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001017"This is equivalent to (real + imag*1j) where imag defaults to 0.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001018
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001019static PyNumberMethods complex_as_number = {
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001020 (binaryfunc)complex_add, /* nb_add */
1021 (binaryfunc)complex_sub, /* nb_subtract */
1022 (binaryfunc)complex_mul, /* nb_multiply */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001023 (binaryfunc)complex_remainder, /* nb_remainder */
1024 (binaryfunc)complex_divmod, /* nb_divmod */
1025 (ternaryfunc)complex_pow, /* nb_power */
1026 (unaryfunc)complex_neg, /* nb_negative */
1027 (unaryfunc)complex_pos, /* nb_positive */
1028 (unaryfunc)complex_abs, /* nb_absolute */
Jack Diederich4dafcc42006-11-28 19:15:13 +00001029 (inquiry)complex_bool, /* nb_bool */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001030 0, /* nb_invert */
1031 0, /* nb_lshift */
1032 0, /* nb_rshift */
1033 0, /* nb_and */
1034 0, /* nb_xor */
1035 0, /* nb_or */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001036 complex_int, /* nb_int */
Mark Dickinson8055afd2009-01-17 10:04:45 +00001037 0, /* nb_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001038 complex_float, /* nb_float */
Guido van Rossum4668b002001-08-08 05:00:18 +00001039 0, /* nb_inplace_add */
1040 0, /* nb_inplace_subtract */
1041 0, /* nb_inplace_multiply*/
Guido van Rossum4668b002001-08-08 05:00:18 +00001042 0, /* nb_inplace_remainder */
1043 0, /* nb_inplace_power */
1044 0, /* nb_inplace_lshift */
1045 0, /* nb_inplace_rshift */
1046 0, /* nb_inplace_and */
1047 0, /* nb_inplace_xor */
1048 0, /* nb_inplace_or */
1049 (binaryfunc)complex_int_div, /* nb_floor_divide */
1050 (binaryfunc)complex_div, /* nb_true_divide */
1051 0, /* nb_inplace_floor_divide */
1052 0, /* nb_inplace_true_divide */
Guido van Rossumf9fca921996-01-12 00:47:05 +00001053};
1054
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001055PyTypeObject PyComplex_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001056 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumf9fca921996-01-12 00:47:05 +00001057 "complex",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001058 sizeof(PyComplexObject),
Guido van Rossumf9fca921996-01-12 00:47:05 +00001059 0,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001060 complex_dealloc, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +00001061 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001062 0, /* tp_getattr */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001063 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001064 0, /* tp_reserved */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001065 (reprfunc)complex_repr, /* tp_repr */
1066 &complex_as_number, /* tp_as_number */
1067 0, /* tp_as_sequence */
1068 0, /* tp_as_mapping */
1069 (hashfunc)complex_hash, /* tp_hash */
1070 0, /* tp_call */
Tim Peters70695122001-03-11 08:37:29 +00001071 (reprfunc)complex_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001072 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001073 0, /* tp_setattro */
1074 0, /* tp_as_buffer */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001075 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1076 complex_doc, /* tp_doc */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001077 0, /* tp_traverse */
1078 0, /* tp_clear */
1079 complex_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001080 0, /* tp_weaklistoffset */
1081 0, /* tp_iter */
1082 0, /* tp_iternext */
1083 complex_methods, /* tp_methods */
1084 complex_members, /* tp_members */
1085 0, /* tp_getset */
1086 0, /* tp_base */
1087 0, /* tp_dict */
1088 0, /* tp_descr_get */
1089 0, /* tp_descr_set */
1090 0, /* tp_dictoffset */
1091 0, /* tp_init */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001092 PyType_GenericAlloc, /* tp_alloc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001093 complex_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00001094 PyObject_Del, /* tp_free */
Guido van Rossumf9fca921996-01-12 00:47:05 +00001095};