blob: 7f40ed624248cf78b82eb376a286195868f0602e [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
Neal Norwitz9fdfaaf2008-03-28 05:34:59 +000011#ifdef HAVE_IEEEFP_H
12#include <ieeefp.h>
13#endif
14
Guido van Rossum65ce6de2002-06-13 17:07:07 +000015#ifndef WITHOUT_COMPLEX
16
Tim Peters70695122001-03-11 08:37:29 +000017/* Precisions used by repr() and str(), respectively.
18
19 The repr() precision (17 significant decimal digits) is the minimal number
20 that is guaranteed to have enough precision so that if the number is read
21 back in the exact same binary value is recreated. This is true for IEEE
22 floating point by design, and also happens to work for all other modern
23 hardware.
24
25 The str() precision is chosen so that in most cases, the rounding noise
26 created by various operations is suppressed, while giving plenty of
27 precision for practical use.
28*/
29
30#define PREC_REPR 17
31#define PREC_STR 12
Guido van Rossumf9fca921996-01-12 00:47:05 +000032
33/* elementary operations on complex numbers */
34
Guido van Rossum9e720e31996-07-21 02:31:35 +000035static Py_complex c_1 = {1., 0.};
Guido van Rossumf9fca921996-01-12 00:47:05 +000036
Tim Peters0f336042001-03-18 08:21:57 +000037Py_complex
38c_sum(Py_complex a, Py_complex b)
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 + b.real;
42 r.imag = a.imag + b.imag;
43 return r;
44}
45
Tim Peters0f336042001-03-18 08:21:57 +000046Py_complex
47c_diff(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;
51 r.imag = a.imag - b.imag;
52 return r;
53}
54
Tim Peters0f336042001-03-18 08:21:57 +000055Py_complex
56c_neg(Py_complex a)
Guido van Rossumf9fca921996-01-12 00:47:05 +000057{
Guido van Rossum9e720e31996-07-21 02:31:35 +000058 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000059 r.real = -a.real;
60 r.imag = -a.imag;
61 return r;
62}
63
Tim Peters0f336042001-03-18 08:21:57 +000064Py_complex
65c_prod(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +000066{
Guido van Rossum9e720e31996-07-21 02:31:35 +000067 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000068 r.real = a.real*b.real - a.imag*b.imag;
69 r.imag = a.real*b.imag + a.imag*b.real;
70 return r;
71}
72
Tim Peters0f336042001-03-18 08:21:57 +000073Py_complex
74c_quot(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +000075{
Tim Peters0f336042001-03-18 08:21:57 +000076 /******************************************************************
77 This was the original algorithm. It's grossly prone to spurious
78 overflow and underflow errors. It also merrily divides by 0 despite
79 checking for that(!). The code still serves a doc purpose here, as
80 the algorithm following is a simple by-cases transformation of this
81 one:
82
Guido van Rossum9e720e31996-07-21 02:31:35 +000083 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000084 double d = b.real*b.real + b.imag*b.imag;
85 if (d == 0.)
Guido van Rossum96783941997-05-20 18:21:34 +000086 errno = EDOM;
Guido van Rossumf9fca921996-01-12 00:47:05 +000087 r.real = (a.real*b.real + a.imag*b.imag)/d;
88 r.imag = (a.imag*b.real - a.real*b.imag)/d;
89 return r;
Tim Peters0f336042001-03-18 08:21:57 +000090 ******************************************************************/
91
92 /* This algorithm is better, and is pretty obvious: first divide the
93 * numerators and denominator by whichever of {b.real, b.imag} has
94 * larger magnitude. The earliest reference I found was to CACM
95 * Algorithm 116 (Complex Division, Robert L. Smith, Stanford
96 * University). As usual, though, we're still ignoring all IEEE
97 * endcases.
98 */
99 Py_complex r; /* the result */
100 const double abs_breal = b.real < 0 ? -b.real : b.real;
101 const double abs_bimag = b.imag < 0 ? -b.imag : b.imag;
102
103 if (abs_breal >= abs_bimag) {
104 /* divide tops and bottom by b.real */
105 if (abs_breal == 0.0) {
106 errno = EDOM;
107 r.real = r.imag = 0.0;
108 }
109 else {
110 const double ratio = b.imag / b.real;
111 const double denom = b.real + b.imag * ratio;
112 r.real = (a.real + a.imag * ratio) / denom;
113 r.imag = (a.imag - a.real * ratio) / denom;
114 }
115 }
116 else {
117 /* divide tops and bottom by b.imag */
118 const double ratio = b.real / b.imag;
119 const double denom = b.real * ratio + b.imag;
120 assert(b.imag != 0.0);
121 r.real = (a.real * ratio + a.imag) / denom;
122 r.imag = (a.imag * ratio - a.real) / denom;
123 }
124 return r;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000125}
126
Tim Peters0f336042001-03-18 08:21:57 +0000127Py_complex
128c_pow(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000129{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000130 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000131 double vabs,len,at,phase;
132 if (b.real == 0. && b.imag == 0.) {
133 r.real = 1.;
134 r.imag = 0.;
135 }
136 else if (a.real == 0. && a.imag == 0.) {
137 if (b.imag != 0. || b.real < 0.)
Tim Petersbab22be2002-03-22 02:48:46 +0000138 errno = EDOM;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000139 r.real = 0.;
140 r.imag = 0.;
141 }
142 else {
143 vabs = hypot(a.real,a.imag);
144 len = pow(vabs,b.real);
Martin v. Löwis387c5472001-09-06 08:16:17 +0000145 at = atan2(a.imag, a.real);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000146 phase = at*b.real;
147 if (b.imag != 0.0) {
148 len /= exp(at*b.imag);
149 phase += b.imag*log(vabs);
150 }
151 r.real = len*cos(phase);
152 r.imag = len*sin(phase);
153 }
154 return r;
155}
156
Tim Peters0f336042001-03-18 08:21:57 +0000157static Py_complex
158c_powu(Py_complex x, long n)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000159{
Guido van Rossum926518b1996-08-19 19:30:45 +0000160 Py_complex r, p;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000161 long mask = 1;
Guido van Rossum926518b1996-08-19 19:30:45 +0000162 r = c_1;
163 p = x;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000164 while (mask > 0 && n >= mask) {
165 if (n & mask)
166 r = c_prod(r,p);
167 mask <<= 1;
168 p = c_prod(p,p);
169 }
170 return r;
171}
172
Tim Peters0f336042001-03-18 08:21:57 +0000173static Py_complex
174c_powi(Py_complex x, long n)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000175{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000176 Py_complex cn;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000177
178 if (n > 100 || n < -100) {
179 cn.real = (double) n;
180 cn.imag = 0.;
181 return c_pow(x,cn);
182 }
183 else if (n > 0)
184 return c_powu(x,n);
185 else
186 return c_quot(c_1,c_powu(x,-n));
187
188}
189
Tim Peters6d6c1a32001-08-02 04:15:00 +0000190static PyObject *
191complex_subtype_from_c_complex(PyTypeObject *type, Py_complex cval)
192{
193 PyObject *op;
194
Georg Brandl6b50c632006-06-01 08:27:32 +0000195 op = type->tp_alloc(type, 0);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000196 if (op != NULL)
197 ((PyComplexObject *)op)->cval = cval;
198 return op;
199}
200
Guido van Rossumf9fca921996-01-12 00:47:05 +0000201PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000202PyComplex_FromCComplex(Py_complex cval)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000203{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000204 register PyComplexObject *op;
205
Guido van Rossume3a8e7e2002-08-19 19:26:42 +0000206 /* Inline PyObject_New */
Guido van Rossumb18618d2000-05-03 23:44:39 +0000207 op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject));
Guido van Rossumf9fca921996-01-12 00:47:05 +0000208 if (op == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000209 return PyErr_NoMemory();
Guido van Rossumb18618d2000-05-03 23:44:39 +0000210 PyObject_INIT(op, &PyComplex_Type);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000211 op->cval = cval;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000212 return (PyObject *) op;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000213}
214
Tim Peters6d6c1a32001-08-02 04:15:00 +0000215static PyObject *
216complex_subtype_from_doubles(PyTypeObject *type, double real, double imag)
217{
218 Py_complex c;
219 c.real = real;
220 c.imag = imag;
221 return complex_subtype_from_c_complex(type, c);
222}
223
Guido van Rossumf9fca921996-01-12 00:47:05 +0000224PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000225PyComplex_FromDoubles(double real, double imag)
Guido van Rossum926518b1996-08-19 19:30:45 +0000226{
227 Py_complex c;
228 c.real = real;
229 c.imag = imag;
230 return PyComplex_FromCComplex(c);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000231}
232
233double
Fred Drake4288c802000-07-09 04:36:04 +0000234PyComplex_RealAsDouble(PyObject *op)
Guido van Rossum926518b1996-08-19 19:30:45 +0000235{
236 if (PyComplex_Check(op)) {
237 return ((PyComplexObject *)op)->cval.real;
Fred Drake4288c802000-07-09 04:36:04 +0000238 }
239 else {
Guido van Rossum926518b1996-08-19 19:30:45 +0000240 return PyFloat_AsDouble(op);
241 }
Guido van Rossumf9fca921996-01-12 00:47:05 +0000242}
243
244double
Fred Drake4288c802000-07-09 04:36:04 +0000245PyComplex_ImagAsDouble(PyObject *op)
Guido van Rossum926518b1996-08-19 19:30:45 +0000246{
247 if (PyComplex_Check(op)) {
248 return ((PyComplexObject *)op)->cval.imag;
Fred Drake4288c802000-07-09 04:36:04 +0000249 }
250 else {
Guido van Rossum926518b1996-08-19 19:30:45 +0000251 return 0.0;
252 }
Guido van Rossumf9fca921996-01-12 00:47:05 +0000253}
254
Guido van Rossum9e720e31996-07-21 02:31:35 +0000255Py_complex
Fred Drake4288c802000-07-09 04:36:04 +0000256PyComplex_AsCComplex(PyObject *op)
Guido van Rossum926518b1996-08-19 19:30:45 +0000257{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000258 Py_complex cv;
Georg Brandl2b869942007-03-17 16:08:45 +0000259 PyObject *newop = NULL;
260 static PyObject *complex_str = NULL;
261
262 assert(op);
263 /* If op is already of type PyComplex_Type, return its value */
Guido van Rossumcf3d1081996-01-12 01:21:14 +0000264 if (PyComplex_Check(op)) {
265 return ((PyComplexObject *)op)->cval;
Fred Drake4288c802000-07-09 04:36:04 +0000266 }
Georg Brandl2b869942007-03-17 16:08:45 +0000267 /* If not, use op's __complex__ method, if it exists */
Christian Heimesd7e1b2b2008-01-28 02:07:53 +0000268
Georg Brandl2b869942007-03-17 16:08:45 +0000269 /* return -1 on failure */
270 cv.real = -1.;
271 cv.imag = 0.;
Christian Heimesd7e1b2b2008-01-28 02:07:53 +0000272
273 if (complex_str == NULL) {
274 if (!(complex_str = PyString_InternFromString("__complex__")))
275 return cv;
276 }
Georg Brandl2b869942007-03-17 16:08:45 +0000277
278 if (PyInstance_Check(op)) {
279 /* this can go away in python 3000 */
Christian Heimesd7e1b2b2008-01-28 02:07:53 +0000280 if (PyObject_HasAttr(op, complex_str)) {
Georg Brandl2b869942007-03-17 16:08:45 +0000281 newop = PyObject_CallMethod(op, "__complex__", NULL);
282 if (!newop)
283 return cv;
284 }
285 /* else try __float__ */
286 } else {
287 PyObject *complexfunc;
Georg Brandl2b869942007-03-17 16:08:45 +0000288 complexfunc = _PyType_Lookup(op->ob_type, complex_str);
289 /* complexfunc is a borrowed reference */
290 if (complexfunc) {
291 newop = PyObject_CallFunctionObjArgs(complexfunc, op, NULL);
292 if (!newop)
293 return cv;
294 }
295 }
296
297 if (newop) {
298 if (!PyComplex_Check(newop)) {
299 PyErr_SetString(PyExc_TypeError,
300 "__complex__ should return a complex object");
301 Py_DECREF(newop);
302 return cv;
303 }
304 cv = ((PyComplexObject *)newop)->cval;
305 Py_DECREF(newop);
306 return cv;
307 }
308 /* If neither of the above works, interpret op as a float giving the
309 real part of the result, and fill in the imaginary part as 0. */
Fred Drake4288c802000-07-09 04:36:04 +0000310 else {
Georg Brandl2b869942007-03-17 16:08:45 +0000311 /* PyFloat_AsDouble will return -1 on failure */
Guido van Rossumcf3d1081996-01-12 01:21:14 +0000312 cv.real = PyFloat_AsDouble(op);
Guido van Rossumcf3d1081996-01-12 01:21:14 +0000313 return cv;
Tim Peters70695122001-03-11 08:37:29 +0000314 }
Guido van Rossumcf3d1081996-01-12 01:21:14 +0000315}
316
Guido van Rossumf9fca921996-01-12 00:47:05 +0000317static void
Fred Drake4288c802000-07-09 04:36:04 +0000318complex_dealloc(PyObject *op)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000319{
Guido van Rossum9475a232001-10-05 20:51:39 +0000320 op->ob_type->tp_free(op);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000321}
322
323
Guido van Rossum363078a1996-05-24 20:45:01 +0000324static void
Barry Warsaw01d697a2001-11-28 20:50:56 +0000325complex_to_buf(char *buf, int bufsz, PyComplexObject *v, int precision)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000326{
Martin v. Löwis737ea822004-06-08 18:52:54 +0000327 char format[32];
328 if (v->cval.real == 0.) {
Christian Heimes2f0da532008-02-15 06:57:08 +0000329 if (!Py_IS_FINITE(v->cval.imag)) {
330 if (Py_IS_NAN(v->cval.imag))
331 strncpy(buf, "nan*j", 6);
332 /* else if (copysign(1, v->cval.imag) == 1) */
333 else if (v->cval.imag > 0)
334 strncpy(buf, "inf*j", 6);
335 else
336 strncpy(buf, "-inf*j", 7);
337 }
338 else {
339 PyOS_snprintf(format, sizeof(format), "%%.%ig", precision);
340 PyOS_ascii_formatd(buf, bufsz - 1, format, v->cval.imag);
341 strncat(buf, "j", 1);
342 }
Martin v. Löwis737ea822004-06-08 18:52:54 +0000343 } else {
344 char re[64], im[64];
Georg Brandlc404ff22005-09-16 06:42:26 +0000345 /* Format imaginary part with sign, real part without */
Christian Heimes2f0da532008-02-15 06:57:08 +0000346 if (!Py_IS_FINITE(v->cval.real)) {
347 if (Py_IS_NAN(v->cval.real))
348 strncpy(re, "nan", 4);
349 /* else if (copysign(1, v->cval.real) == 1) */
350 else if (v->cval.real > 0)
351 strncpy(re, "inf", 4);
352 else
353 strncpy(re, "-inf", 5);
354 }
355 else {
356 PyOS_snprintf(format, sizeof(format), "%%.%ig", precision);
357 PyOS_ascii_formatd(re, sizeof(re), format, v->cval.real);
358 }
359 if (!Py_IS_FINITE(v->cval.imag)) {
360 if (Py_IS_NAN(v->cval.imag))
361 strncpy(im, "+nan*", 6);
362 /* else if (copysign(1, v->cval.imag) == 1) */
363 else if (v->cval.imag > 0)
364 strncpy(im, "+inf*", 6);
365 else
366 strncpy(im, "-inf*", 6);
367 }
368 else {
369 PyOS_snprintf(format, sizeof(format), "%%+.%ig", precision);
370 PyOS_ascii_formatd(im, sizeof(im), format, v->cval.imag);
371 }
Georg Brandlc404ff22005-09-16 06:42:26 +0000372 PyOS_snprintf(buf, bufsz, "(%s%sj)", re, im);
Martin v. Löwis737ea822004-06-08 18:52:54 +0000373 }
Guido van Rossumf9fca921996-01-12 00:47:05 +0000374}
375
376static int
Fred Drake4288c802000-07-09 04:36:04 +0000377complex_print(PyComplexObject *v, FILE *fp, int flags)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000378{
379 char buf[100];
Barry Warsaw01d697a2001-11-28 20:50:56 +0000380 complex_to_buf(buf, sizeof(buf), v,
Tim Peters70695122001-03-11 08:37:29 +0000381 (flags & Py_PRINT_RAW) ? PREC_STR : PREC_REPR);
Brett Cannon01531592007-09-17 03:28:34 +0000382 Py_BEGIN_ALLOW_THREADS
Guido van Rossumf9fca921996-01-12 00:47:05 +0000383 fputs(buf, fp);
Brett Cannon01531592007-09-17 03:28:34 +0000384 Py_END_ALLOW_THREADS
Guido van Rossumf9fca921996-01-12 00:47:05 +0000385 return 0;
386}
387
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000388static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000389complex_repr(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000390{
391 char buf[100];
Barry Warsaw01d697a2001-11-28 20:50:56 +0000392 complex_to_buf(buf, sizeof(buf), v, PREC_REPR);
Tim Peters70695122001-03-11 08:37:29 +0000393 return PyString_FromString(buf);
394}
395
396static PyObject *
397complex_str(PyComplexObject *v)
398{
399 char buf[100];
Barry Warsaw01d697a2001-11-28 20:50:56 +0000400 complex_to_buf(buf, sizeof(buf), v, PREC_STR);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000401 return PyString_FromString(buf);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000402}
403
Guido van Rossumf9fca921996-01-12 00:47:05 +0000404static long
Fred Drake4288c802000-07-09 04:36:04 +0000405complex_hash(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000406{
Tim Peters39dce292000-08-15 03:34:48 +0000407 long hashreal, hashimag, combined;
408 hashreal = _Py_HashDouble(v->cval.real);
409 if (hashreal == -1)
410 return -1;
411 hashimag = _Py_HashDouble(v->cval.imag);
412 if (hashimag == -1)
413 return -1;
414 /* Note: if the imaginary part is 0, hashimag is 0 now,
415 * so the following returns hashreal unchanged. This is
416 * important because numbers of different types that
417 * compare equal must have the same hash value, so that
418 * hash(x + 0*j) must equal hash(x).
419 */
420 combined = hashreal + 1000003 * hashimag;
421 if (combined == -1)
422 combined = -2;
423 return combined;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000424}
425
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000426/* This macro may return! */
427#define TO_COMPLEX(obj, c) \
428 if (PyComplex_Check(obj)) \
429 c = ((PyComplexObject *)(obj))->cval; \
430 else if (to_complex(&(obj), &(c)) < 0) \
431 return (obj)
432
433static int
434to_complex(PyObject **pobj, Py_complex *pc)
435{
436 PyObject *obj = *pobj;
437
438 pc->real = pc->imag = 0.0;
439 if (PyInt_Check(obj)) {
440 pc->real = PyInt_AS_LONG(obj);
441 return 0;
442 }
443 if (PyLong_Check(obj)) {
444 pc->real = PyLong_AsDouble(obj);
445 if (pc->real == -1.0 && PyErr_Occurred()) {
446 *pobj = NULL;
447 return -1;
448 }
449 return 0;
450 }
451 if (PyFloat_Check(obj)) {
452 pc->real = PyFloat_AsDouble(obj);
453 return 0;
454 }
455 Py_INCREF(Py_NotImplemented);
456 *pobj = Py_NotImplemented;
457 return -1;
458}
459
460
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000461static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000462complex_add(PyComplexObject *v, PyComplexObject *w)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000463{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000464 Py_complex result;
465 PyFPE_START_PROTECT("complex_add", return 0)
466 result = c_sum(v->cval,w->cval);
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 *
Fred Drake4288c802000-07-09 04:36:04 +0000472complex_sub(PyComplexObject *v, PyComplexObject *w)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000473{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000474 Py_complex result;
475 PyFPE_START_PROTECT("complex_sub", return 0)
476 result = c_diff(v->cval,w->cval);
Guido van Rossum45b83911997-03-14 04:32:50 +0000477 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000478 return PyComplex_FromCComplex(result);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000479}
480
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000481static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000482complex_mul(PyComplexObject *v, PyComplexObject *w)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000483{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000484 Py_complex result;
485 PyFPE_START_PROTECT("complex_mul", return 0)
486 result = c_prod(v->cval,w->cval);
Guido van Rossum45b83911997-03-14 04:32:50 +0000487 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000488 return PyComplex_FromCComplex(result);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000489}
490
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000491static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000492complex_div(PyComplexObject *v, PyComplexObject *w)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000493{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000494 Py_complex quot;
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000495 PyFPE_START_PROTECT("complex_div", return 0)
Guido van Rossum96783941997-05-20 18:21:34 +0000496 errno = 0;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000497 quot = c_quot(v->cval,w->cval);
Guido van Rossum45b83911997-03-14 04:32:50 +0000498 PyFPE_END_PROTECT(quot)
Guido van Rossum96783941997-05-20 18:21:34 +0000499 if (errno == EDOM) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000500 PyErr_SetString(PyExc_ZeroDivisionError, "complex division");
Guido van Rossumf9fca921996-01-12 00:47:05 +0000501 return NULL;
502 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000503 return PyComplex_FromCComplex(quot);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000504}
505
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000506static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000507complex_classic_div(PyComplexObject *v, PyComplexObject *w)
508{
509 Py_complex quot;
510
Guido van Rossum1832de42001-09-04 03:51:09 +0000511 if (Py_DivisionWarningFlag >= 2 &&
Guido van Rossum393661d2001-08-31 17:40:15 +0000512 PyErr_Warn(PyExc_DeprecationWarning,
513 "classic complex division") < 0)
514 return NULL;
515
516 PyFPE_START_PROTECT("complex_classic_div", return 0)
517 errno = 0;
518 quot = c_quot(v->cval,w->cval);
519 PyFPE_END_PROTECT(quot)
520 if (errno == EDOM) {
521 PyErr_SetString(PyExc_ZeroDivisionError, "complex division");
522 return NULL;
523 }
524 return PyComplex_FromCComplex(quot);
525}
526
527static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000528complex_remainder(PyComplexObject *v, PyComplexObject *w)
Guido van Rossumee09fc11996-09-11 13:55:55 +0000529{
Georg Brandl96f21842008-01-19 10:18:07 +0000530 Py_complex div, mod;
Guido van Rossum69cf3c72002-04-15 12:39:12 +0000531
532 if (PyErr_Warn(PyExc_DeprecationWarning,
533 "complex divmod(), // and % are deprecated") < 0)
534 return NULL;
535
Guido van Rossum96783941997-05-20 18:21:34 +0000536 errno = 0;
Guido van Rossum3be12e91996-09-12 20:56:18 +0000537 div = c_quot(v->cval,w->cval); /* The raw divisor value. */
Guido van Rossum96783941997-05-20 18:21:34 +0000538 if (errno == EDOM) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000539 PyErr_SetString(PyExc_ZeroDivisionError, "complex remainder");
Guido van Rossum3be12e91996-09-12 20:56:18 +0000540 return NULL;
541 }
542 div.real = floor(div.real); /* Use the floor of the real part. */
543 div.imag = 0.0;
544 mod = c_diff(v->cval, c_prod(w->cval, div));
545
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000546 return PyComplex_FromCComplex(mod);
Guido van Rossumee09fc11996-09-11 13:55:55 +0000547}
548
Guido van Rossumee09fc11996-09-11 13:55:55 +0000549
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000550static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000551complex_divmod(PyComplexObject *v, PyComplexObject *w)
Guido van Rossum3be12e91996-09-12 20:56:18 +0000552{
Georg Brandl96f21842008-01-19 10:18:07 +0000553 Py_complex div, mod;
Guido van Rossum3be12e91996-09-12 20:56:18 +0000554 PyObject *d, *m, *z;
Guido van Rossum9ec4c782002-04-15 01:41:56 +0000555
556 if (PyErr_Warn(PyExc_DeprecationWarning,
Guido van Rossum69cf3c72002-04-15 12:39:12 +0000557 "complex divmod(), // and % are deprecated") < 0)
Guido van Rossum9ec4c782002-04-15 01:41:56 +0000558 return NULL;
559
Guido van Rossum96783941997-05-20 18:21:34 +0000560 errno = 0;
Guido van Rossum3be12e91996-09-12 20:56:18 +0000561 div = c_quot(v->cval,w->cval); /* The raw divisor value. */
Guido van Rossum96783941997-05-20 18:21:34 +0000562 if (errno == EDOM) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000563 PyErr_SetString(PyExc_ZeroDivisionError, "complex divmod()");
Guido van Rossum3be12e91996-09-12 20:56:18 +0000564 return NULL;
565 }
566 div.real = floor(div.real); /* Use the floor of the real part. */
567 div.imag = 0.0;
568 mod = c_diff(v->cval, c_prod(w->cval, div));
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000569 d = PyComplex_FromCComplex(div);
570 m = PyComplex_FromCComplex(mod);
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000571 z = PyTuple_Pack(2, d, m);
Guido van Rossum3be12e91996-09-12 20:56:18 +0000572 Py_XDECREF(d);
573 Py_XDECREF(m);
574 return z;
575}
Guido van Rossumf9fca921996-01-12 00:47:05 +0000576
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000577static PyObject *
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000578complex_pow(PyObject *v, PyObject *w, PyObject *z)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000579{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000580 Py_complex p;
581 Py_complex exponent;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000582 long int_exponent;
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000583 Py_complex a, b;
Georg Brandl96f21842008-01-19 10:18:07 +0000584 TO_COMPLEX(v, a);
585 TO_COMPLEX(w, b);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000586
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000587 if (z!=Py_None) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000588 PyErr_SetString(PyExc_ValueError, "complex modulo");
Guido van Rossumf9fca921996-01-12 00:47:05 +0000589 return NULL;
590 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000591 PyFPE_START_PROTECT("complex_pow", return 0)
Guido van Rossum96783941997-05-20 18:21:34 +0000592 errno = 0;
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000593 exponent = b;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000594 int_exponent = (long)exponent.real;
595 if (exponent.imag == 0. && exponent.real == int_exponent)
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000596 p = c_powi(a,int_exponent);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000597 else
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000598 p = c_pow(a,exponent);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000599
Guido van Rossum45b83911997-03-14 04:32:50 +0000600 PyFPE_END_PROTECT(p)
Tim Petersbab22be2002-03-22 02:48:46 +0000601 Py_ADJUST_ERANGE2(p.real, p.imag);
602 if (errno == EDOM) {
603 PyErr_SetString(PyExc_ZeroDivisionError,
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000604 "0.0 to a negative or complex power");
Guido van Rossumf9fca921996-01-12 00:47:05 +0000605 return NULL;
606 }
Tim Petersbab22be2002-03-22 02:48:46 +0000607 else if (errno == ERANGE) {
608 PyErr_SetString(PyExc_OverflowError,
Neal Norwitz0593de32007-03-09 05:59:01 +0000609 "complex exponentiation");
Tim Petersbab22be2002-03-22 02:48:46 +0000610 return NULL;
611 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000612 return PyComplex_FromCComplex(p);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000613}
614
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000615static PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +0000616complex_int_div(PyComplexObject *v, PyComplexObject *w)
617{
618 PyObject *t, *r;
619
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000620 if (PyErr_Warn(PyExc_DeprecationWarning,
621 "complex divmod(), // and % are deprecated") < 0)
622 return NULL;
623
Guido van Rossum4668b002001-08-08 05:00:18 +0000624 t = complex_divmod(v, w);
625 if (t != NULL) {
626 r = PyTuple_GET_ITEM(t, 0);
627 Py_INCREF(r);
628 Py_DECREF(t);
629 return r;
630 }
631 return NULL;
632}
633
634static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000635complex_neg(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000636{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000637 Py_complex neg;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000638 neg.real = -v->cval.real;
639 neg.imag = -v->cval.imag;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000640 return PyComplex_FromCComplex(neg);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000641}
642
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000643static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000644complex_pos(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000645{
Tim Peters2400fa42001-09-12 19:12:49 +0000646 if (PyComplex_CheckExact(v)) {
647 Py_INCREF(v);
648 return (PyObject *)v;
649 }
650 else
651 return PyComplex_FromCComplex(v->cval);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000652}
653
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000654static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000655complex_abs(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000656{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000657 double result;
658 PyFPE_START_PROTECT("complex_abs", return 0)
659 result = hypot(v->cval.real,v->cval.imag);
Guido van Rossum45b83911997-03-14 04:32:50 +0000660 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000661 return PyFloat_FromDouble(result);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000662}
663
664static int
Fred Drake4288c802000-07-09 04:36:04 +0000665complex_nonzero(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000666{
Guido van Rossum3bbef601999-01-25 19:42:19 +0000667 return v->cval.real != 0.0 || v->cval.imag != 0.0;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000668}
669
670static int
Fred Drake4288c802000-07-09 04:36:04 +0000671complex_coerce(PyObject **pv, PyObject **pw)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000672{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000673 Py_complex cval;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000674 cval.imag = 0.;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000675 if (PyInt_Check(*pw)) {
676 cval.real = (double)PyInt_AsLong(*pw);
677 *pw = PyComplex_FromCComplex(cval);
678 Py_INCREF(*pv);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000679 return 0;
680 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000681 else if (PyLong_Check(*pw)) {
682 cval.real = PyLong_AsDouble(*pw);
Tim Peters9fffa3e2001-09-04 05:14:19 +0000683 if (cval.real == -1.0 && PyErr_Occurred())
684 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000685 *pw = PyComplex_FromCComplex(cval);
686 Py_INCREF(*pv);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000687 return 0;
688 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000689 else if (PyFloat_Check(*pw)) {
690 cval.real = PyFloat_AsDouble(*pw);
691 *pw = PyComplex_FromCComplex(cval);
692 Py_INCREF(*pv);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000693 return 0;
694 }
Guido van Rossum63805962001-09-19 01:13:10 +0000695 else if (PyComplex_Check(*pw)) {
696 Py_INCREF(*pv);
697 Py_INCREF(*pw);
698 return 0;
699 }
Guido van Rossumf9fca921996-01-12 00:47:05 +0000700 return 1; /* Can't do it */
701}
702
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000703static PyObject *
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000704complex_richcompare(PyObject *v, PyObject *w, int op)
705{
706 int c;
707 Py_complex i, j;
708 PyObject *res;
709
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000710 c = PyNumber_CoerceEx(&v, &w);
711 if (c < 0)
712 return NULL;
713 if (c > 0) {
714 Py_INCREF(Py_NotImplemented);
715 return Py_NotImplemented;
716 }
Guido van Rossum2ed6bf82001-09-27 20:30:07 +0000717 /* Make sure both arguments are complex. */
718 if (!(PyComplex_Check(v) && PyComplex_Check(w))) {
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000719 Py_DECREF(v);
720 Py_DECREF(w);
721 Py_INCREF(Py_NotImplemented);
722 return Py_NotImplemented;
723 }
724
725 i = ((PyComplexObject *)v)->cval;
726 j = ((PyComplexObject *)w)->cval;
727 Py_DECREF(v);
728 Py_DECREF(w);
729
Guido van Rossum22056422001-09-24 17:52:04 +0000730 if (op != Py_EQ && op != Py_NE) {
731 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger5d01aa42004-12-19 20:45:20 +0000732 "no ordering relation is defined for complex numbers");
Guido van Rossum22056422001-09-24 17:52:04 +0000733 return NULL;
734 }
735
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000736 if ((i.real == j.real && i.imag == j.imag) == (op == Py_EQ))
737 res = Py_True;
738 else
739 res = Py_False;
740
741 Py_INCREF(res);
742 return res;
743}
744
745static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000746complex_int(PyObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000747{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000748 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger0970dba2003-08-30 23:57:36 +0000749 "can't convert complex to int; use int(abs(z))");
Guido van Rossumd4ab3cd1996-09-11 22:54:37 +0000750 return NULL;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000751}
752
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000753static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000754complex_long(PyObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000755{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000756 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger0970dba2003-08-30 23:57:36 +0000757 "can't convert complex to long; use long(abs(z))");
Guido van Rossumd4ab3cd1996-09-11 22:54:37 +0000758 return NULL;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000759}
760
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000761static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000762complex_float(PyObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000763{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000764 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger0970dba2003-08-30 23:57:36 +0000765 "can't convert complex to float; use abs(z)");
Guido van Rossumd4ab3cd1996-09-11 22:54:37 +0000766 return NULL;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000767}
768
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000769static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000770complex_conjugate(PyObject *self)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000771{
Guido van Rossum926518b1996-08-19 19:30:45 +0000772 Py_complex c;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000773 c = ((PyComplexObject *)self)->cval;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000774 c.imag = -c.imag;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000775 return PyComplex_FromCComplex(c);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000776}
777
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000778PyDoc_STRVAR(complex_conjugate_doc,
779"complex.conjugate() -> complex\n"
780"\n"
781"Returns the complex conjugate of its argument. (3-4j).conjugate() == 3+4j.");
782
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000783static PyObject *
784complex_getnewargs(PyComplexObject *v)
785{
Guido van Rossum4eadfa22003-03-02 13:51:47 +0000786 return Py_BuildValue("(D)", &v->cval);
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000787}
788
Guido van Rossumf9fca921996-01-12 00:47:05 +0000789static PyMethodDef complex_methods[] = {
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000790 {"conjugate", (PyCFunction)complex_conjugate, METH_NOARGS,
791 complex_conjugate_doc},
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000792 {"__getnewargs__", (PyCFunction)complex_getnewargs, METH_NOARGS},
Guido van Rossumf9fca921996-01-12 00:47:05 +0000793 {NULL, NULL} /* sentinel */
794};
795
Guido van Rossum6f799372001-09-20 20:46:19 +0000796static PyMemberDef complex_members[] = {
Guido van Rossumfa2e4c22002-02-08 21:26:07 +0000797 {"real", T_DOUBLE, offsetof(PyComplexObject, cval.real), READONLY,
Guido van Rossum6f799372001-09-20 20:46:19 +0000798 "the real part of a complex number"},
Guido van Rossumfa2e4c22002-02-08 21:26:07 +0000799 {"imag", T_DOUBLE, offsetof(PyComplexObject, cval.imag), READONLY,
Guido van Rossum6f799372001-09-20 20:46:19 +0000800 "the imaginary part of a complex number"},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000801 {0},
802};
Guido van Rossumf9fca921996-01-12 00:47:05 +0000803
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000804static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000805complex_subtype_from_string(PyTypeObject *type, PyObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000806{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000807 const char *s, *start;
808 char *end;
809 double x=0.0, y=0.0, z;
Collin Wintere38051d2007-03-09 20:33:07 +0000810 int got_re=0, got_im=0, got_bracket=0, done=0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000811 int digit_or_dot;
812 int sw_error=0;
813 int sign;
814 char buffer[256]; /* For errors */
Guido van Rossum70e36882001-10-25 18:07:22 +0000815#ifdef Py_USING_UNICODE
816 char s_buffer[256];
817#endif
Martin v. Löwis18e16552006-02-15 17:27:45 +0000818 Py_ssize_t len;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000819
820 if (PyString_Check(v)) {
821 s = PyString_AS_STRING(v);
822 len = PyString_GET_SIZE(v);
823 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000824#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +0000825 else if (PyUnicode_Check(v)) {
Skip Montanaro429433b2006-04-18 00:35:43 +0000826 if (PyUnicode_GET_SIZE(v) >= (Py_ssize_t)sizeof(s_buffer)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000827 PyErr_SetString(PyExc_ValueError,
828 "complex() literal too large to convert");
829 return NULL;
830 }
831 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
832 PyUnicode_GET_SIZE(v),
833 s_buffer,
834 NULL))
835 return NULL;
836 s = s_buffer;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000837 len = strlen(s);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000838 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000839#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +0000840 else if (PyObject_AsCharBuffer(v, &s, &len)) {
841 PyErr_SetString(PyExc_TypeError,
842 "complex() arg is not a string");
843 return NULL;
844 }
845
846 /* position on first nonblank */
847 start = s;
848 while (*s && isspace(Py_CHARMASK(*s)))
849 s++;
Georg Brandl96f21842008-01-19 10:18:07 +0000850 if (s[0] == '\0') {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000851 PyErr_SetString(PyExc_ValueError,
852 "complex() arg is an empty string");
853 return NULL;
Georg Brandl96f21842008-01-19 10:18:07 +0000854 }
Collin Wintere38051d2007-03-09 20:33:07 +0000855 if (s[0] == '(') {
856 /* Skip over possible bracket from repr(). */
857 got_bracket = 1;
858 s++;
859 while (*s && isspace(Py_CHARMASK(*s)))
860 s++;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000861 }
862
863 z = -1.0;
864 sign = 1;
865 do {
866
867 switch (*s) {
868
869 case '\0':
870 if (s-start != len) {
871 PyErr_SetString(
872 PyExc_ValueError,
873 "complex() arg contains a null byte");
874 return NULL;
875 }
876 if(!done) sw_error=1;
877 break;
878
Collin Wintere38051d2007-03-09 20:33:07 +0000879 case ')':
880 if (!got_bracket || !(got_re || got_im)) {
881 sw_error=1;
882 break;
883 }
884 got_bracket=0;
885 done=1;
886 s++;
887 while (*s && isspace(Py_CHARMASK(*s)))
888 s++;
889 if (*s) sw_error=1;
890 break;
891
Tim Peters6d6c1a32001-08-02 04:15:00 +0000892 case '-':
893 sign = -1;
894 /* Fallthrough */
895 case '+':
896 if (done) sw_error=1;
897 s++;
Collin Wintere38051d2007-03-09 20:33:07 +0000898 if ( *s=='\0'||*s=='+'||*s=='-'||*s==')'||
Tim Peters6d6c1a32001-08-02 04:15:00 +0000899 isspace(Py_CHARMASK(*s)) ) sw_error=1;
900 break;
901
902 case 'J':
903 case 'j':
904 if (got_im || done) {
905 sw_error = 1;
906 break;
907 }
908 if (z<0.0) {
909 y=sign;
910 }
911 else{
912 y=sign*z;
913 }
914 got_im=1;
915 s++;
916 if (*s!='+' && *s!='-' )
917 done=1;
918 break;
919
920 default:
921 if (isspace(Py_CHARMASK(*s))) {
922 while (*s && isspace(Py_CHARMASK(*s)))
923 s++;
Collin Wintere38051d2007-03-09 20:33:07 +0000924 if (*s && *s != ')')
Tim Peters6d6c1a32001-08-02 04:15:00 +0000925 sw_error=1;
926 else
927 done = 1;
928 break;
929 }
930 digit_or_dot =
931 (*s=='.' || isdigit(Py_CHARMASK(*s)));
932 if (done||!digit_or_dot) {
933 sw_error=1;
934 break;
935 }
936 errno = 0;
937 PyFPE_START_PROTECT("strtod", return 0)
Martin v. Löwis737ea822004-06-08 18:52:54 +0000938 z = PyOS_ascii_strtod(s, &end) ;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000939 PyFPE_END_PROTECT(z)
940 if (errno != 0) {
Barry Warsaw01d697a2001-11-28 20:50:56 +0000941 PyOS_snprintf(buffer, sizeof(buffer),
Tim Peters6d6c1a32001-08-02 04:15:00 +0000942 "float() out of range: %.150s", s);
943 PyErr_SetString(
944 PyExc_ValueError,
945 buffer);
946 return NULL;
947 }
948 s=end;
949 if (*s=='J' || *s=='j') {
950
951 break;
952 }
953 if (got_re) {
954 sw_error=1;
955 break;
956 }
957
958 /* accept a real part */
959 x=sign*z;
960 got_re=1;
961 if (got_im) done=1;
962 z = -1.0;
963 sign = 1;
964 break;
965
966 } /* end of switch */
967
Tim Peters077f2712002-04-14 22:04:03 +0000968 } while (s - start < len && !sw_error);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000969
Collin Wintere38051d2007-03-09 20:33:07 +0000970 if (sw_error || got_bracket) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000971 PyErr_SetString(PyExc_ValueError,
972 "complex() arg is a malformed string");
973 return NULL;
974 }
975
976 return complex_subtype_from_doubles(type, x, y);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000977}
978
Tim Peters6d6c1a32001-08-02 04:15:00 +0000979static PyObject *
980complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
981{
Raymond Hettinger478d47a2002-06-06 15:45:38 +0000982 PyObject *r, *i, *tmp, *f;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000983 PyNumberMethods *nbr, *nbi = NULL;
984 Py_complex cr, ci;
985 int own_r = 0;
Guido van Rossum715ec182007-11-27 22:38:36 +0000986 int cr_is_complex = 0;
987 int ci_is_complex = 0;
Raymond Hettinger478d47a2002-06-06 15:45:38 +0000988 static PyObject *complexstr;
Martin v. Löwis15e62742006-02-27 16:46:16 +0000989 static char *kwlist[] = {"real", "imag", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000990
991 r = Py_False;
992 i = NULL;
993 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:complex", kwlist,
994 &r, &i))
995 return NULL;
Raymond Hettinger604cd6a2002-08-29 14:22:51 +0000996
Georg Brandl8f032cb2007-03-13 07:57:51 +0000997 /* Special-case for a single argument when type(arg) is complex. */
Guido van Rossum4eadfa22003-03-02 13:51:47 +0000998 if (PyComplex_CheckExact(r) && i == NULL &&
999 type == &PyComplex_Type) {
Raymond Hettinger604cd6a2002-08-29 14:22:51 +00001000 /* Note that we can't know whether it's safe to return
1001 a complex *subclass* instance as-is, hence the restriction
Georg Brandl8f032cb2007-03-13 07:57:51 +00001002 to exact complexes here. If either the input or the
1003 output is a complex subclass, it will be handled below
1004 as a non-orthogonal vector. */
Raymond Hettinger604cd6a2002-08-29 14:22:51 +00001005 Py_INCREF(r);
1006 return r;
1007 }
Fred Drake526c7a02001-12-13 19:52:22 +00001008 if (PyString_Check(r) || PyUnicode_Check(r)) {
1009 if (i != NULL) {
1010 PyErr_SetString(PyExc_TypeError,
1011 "complex() can't take second arg"
1012 " if first is a string");
1013 return NULL;
Georg Brandl96f21842008-01-19 10:18:07 +00001014 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001015 return complex_subtype_from_string(type, r);
Fred Drake526c7a02001-12-13 19:52:22 +00001016 }
1017 if (i != NULL && (PyString_Check(i) || PyUnicode_Check(i))) {
1018 PyErr_SetString(PyExc_TypeError,
1019 "complex() second arg can't be a string");
1020 return NULL;
1021 }
Tim Peters2400fa42001-09-12 19:12:49 +00001022
Raymond Hettinger478d47a2002-06-06 15:45:38 +00001023 /* XXX Hack to support classes with __complex__ method */
1024 if (complexstr == NULL) {
1025 complexstr = PyString_InternFromString("__complex__");
1026 if (complexstr == NULL)
1027 return NULL;
1028 }
1029 f = PyObject_GetAttr(r, complexstr);
1030 if (f == NULL)
1031 PyErr_Clear();
1032 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001033 PyObject *args = PyTuple_New(0);
Raymond Hettinger478d47a2002-06-06 15:45:38 +00001034 if (args == NULL)
1035 return NULL;
1036 r = PyEval_CallObject(f, args);
1037 Py_DECREF(args);
1038 Py_DECREF(f);
1039 if (r == NULL)
1040 return NULL;
1041 own_r = 1;
1042 }
Tim Peters2400fa42001-09-12 19:12:49 +00001043 nbr = r->ob_type->tp_as_number;
1044 if (i != NULL)
1045 nbi = i->ob_type->tp_as_number;
1046 if (nbr == NULL || nbr->nb_float == NULL ||
1047 ((i != NULL) && (nbi == NULL || nbi->nb_float == NULL))) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001048 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger478d47a2002-06-06 15:45:38 +00001049 "complex() argument must be a string or a number");
Tim Peters465fa3d2003-08-15 01:16:37 +00001050 if (own_r) {
1051 Py_DECREF(r);
1052 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001053 return NULL;
1054 }
Georg Brandl8f032cb2007-03-13 07:57:51 +00001055
1056 /* If we get this far, then the "real" and "imag" parts should
1057 both be treated as numbers, and the constructor should return a
1058 complex number equal to (real + imag*1j).
1059
1060 Note that we do NOT assume the input to already be in canonical
1061 form; the "real" and "imag" parts might themselves be complex
1062 numbers, which slightly complicates the code below. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001063 if (PyComplex_Check(r)) {
Tim Peters2400fa42001-09-12 19:12:49 +00001064 /* Note that if r is of a complex subtype, we're only
1065 retaining its real & imag parts here, and the return
1066 value is (properly) of the builtin complex type. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001067 cr = ((PyComplexObject*)r)->cval;
Guido van Rossum715ec182007-11-27 22:38:36 +00001068 cr_is_complex = 1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001069 if (own_r) {
1070 Py_DECREF(r);
1071 }
1072 }
1073 else {
Georg Brandl8f032cb2007-03-13 07:57:51 +00001074 /* The "real" part really is entirely real, and contributes
1075 nothing in the imaginary direction.
1076 Just treat it as a double. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001077 tmp = PyNumber_Float(r);
1078 if (own_r) {
Georg Brandl8f032cb2007-03-13 07:57:51 +00001079 /* r was a newly created complex number, rather
1080 than the original "real" argument. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001081 Py_DECREF(r);
1082 }
1083 if (tmp == NULL)
1084 return NULL;
1085 if (!PyFloat_Check(tmp)) {
1086 PyErr_SetString(PyExc_TypeError,
1087 "float(r) didn't return a float");
1088 Py_DECREF(tmp);
1089 return NULL;
1090 }
1091 cr.real = PyFloat_AsDouble(tmp);
Georg Brandl96f21842008-01-19 10:18:07 +00001092 cr.imag = 0.0; /* Shut up compiler warning */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001093 Py_DECREF(tmp);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001094 }
1095 if (i == NULL) {
1096 ci.real = 0.0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001097 }
Guido van Rossum715ec182007-11-27 22:38:36 +00001098 else if (PyComplex_Check(i)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001099 ci = ((PyComplexObject*)i)->cval;
Guido van Rossum715ec182007-11-27 22:38:36 +00001100 ci_is_complex = 1;
1101 } else {
Georg Brandl8f032cb2007-03-13 07:57:51 +00001102 /* The "imag" part really is entirely imaginary, and
1103 contributes nothing in the real direction.
1104 Just treat it as a double. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001105 tmp = (*nbi->nb_float)(i);
1106 if (tmp == NULL)
1107 return NULL;
1108 ci.real = PyFloat_AsDouble(tmp);
1109 Py_DECREF(tmp);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001110 }
Georg Brandl8f032cb2007-03-13 07:57:51 +00001111 /* If the input was in canonical form, then the "real" and "imag"
Guido van Rossum715ec182007-11-27 22:38:36 +00001112 parts are real numbers, so that ci.imag and cr.imag are zero.
Georg Brandl8f032cb2007-03-13 07:57:51 +00001113 We need this correction in case they were not real numbers. */
Guido van Rossum715ec182007-11-27 22:38:36 +00001114
1115 if (ci_is_complex) {
1116 cr.real -= ci.imag;
1117 }
1118 if (cr_is_complex) {
1119 ci.real += cr.imag;
1120 }
1121 return complex_subtype_from_doubles(type, cr.real, ci.real);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001122}
1123
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001124PyDoc_STRVAR(complex_doc,
Tim Peters2400fa42001-09-12 19:12:49 +00001125"complex(real[, imag]) -> complex number\n"
1126"\n"
1127"Create a complex number from a real part and an optional imaginary part.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001128"This is equivalent to (real + imag*1j) where imag defaults to 0.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001129
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001130static PyNumberMethods complex_as_number = {
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001131 (binaryfunc)complex_add, /* nb_add */
1132 (binaryfunc)complex_sub, /* nb_subtract */
1133 (binaryfunc)complex_mul, /* nb_multiply */
Guido van Rossum393661d2001-08-31 17:40:15 +00001134 (binaryfunc)complex_classic_div, /* nb_divide */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001135 (binaryfunc)complex_remainder, /* nb_remainder */
1136 (binaryfunc)complex_divmod, /* nb_divmod */
1137 (ternaryfunc)complex_pow, /* nb_power */
1138 (unaryfunc)complex_neg, /* nb_negative */
1139 (unaryfunc)complex_pos, /* nb_positive */
1140 (unaryfunc)complex_abs, /* nb_absolute */
1141 (inquiry)complex_nonzero, /* nb_nonzero */
1142 0, /* nb_invert */
1143 0, /* nb_lshift */
1144 0, /* nb_rshift */
1145 0, /* nb_and */
1146 0, /* nb_xor */
1147 0, /* nb_or */
Georg Brandl347b3002006-03-30 11:57:00 +00001148 complex_coerce, /* nb_coerce */
1149 complex_int, /* nb_int */
1150 complex_long, /* nb_long */
1151 complex_float, /* nb_float */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001152 0, /* nb_oct */
1153 0, /* nb_hex */
Guido van Rossum4668b002001-08-08 05:00:18 +00001154 0, /* nb_inplace_add */
1155 0, /* nb_inplace_subtract */
1156 0, /* nb_inplace_multiply*/
1157 0, /* nb_inplace_divide */
1158 0, /* nb_inplace_remainder */
1159 0, /* nb_inplace_power */
1160 0, /* nb_inplace_lshift */
1161 0, /* nb_inplace_rshift */
1162 0, /* nb_inplace_and */
1163 0, /* nb_inplace_xor */
1164 0, /* nb_inplace_or */
1165 (binaryfunc)complex_int_div, /* nb_floor_divide */
1166 (binaryfunc)complex_div, /* nb_true_divide */
1167 0, /* nb_inplace_floor_divide */
1168 0, /* nb_inplace_true_divide */
Guido van Rossumf9fca921996-01-12 00:47:05 +00001169};
1170
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001171PyTypeObject PyComplex_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001172 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossumf9fca921996-01-12 00:47:05 +00001173 "complex",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001174 sizeof(PyComplexObject),
Guido van Rossumf9fca921996-01-12 00:47:05 +00001175 0,
Georg Brandl347b3002006-03-30 11:57:00 +00001176 complex_dealloc, /* tp_dealloc */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001177 (printfunc)complex_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001178 0, /* tp_getattr */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001179 0, /* tp_setattr */
1180 0, /* tp_compare */
1181 (reprfunc)complex_repr, /* tp_repr */
1182 &complex_as_number, /* tp_as_number */
1183 0, /* tp_as_sequence */
1184 0, /* tp_as_mapping */
1185 (hashfunc)complex_hash, /* tp_hash */
1186 0, /* tp_call */
Tim Peters70695122001-03-11 08:37:29 +00001187 (reprfunc)complex_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001188 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001189 0, /* tp_setattro */
1190 0, /* tp_as_buffer */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001191 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1192 complex_doc, /* tp_doc */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001193 0, /* tp_traverse */
1194 0, /* tp_clear */
1195 complex_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001196 0, /* tp_weaklistoffset */
1197 0, /* tp_iter */
1198 0, /* tp_iternext */
1199 complex_methods, /* tp_methods */
1200 complex_members, /* tp_members */
1201 0, /* tp_getset */
1202 0, /* tp_base */
1203 0, /* tp_dict */
1204 0, /* tp_descr_get */
1205 0, /* tp_descr_set */
1206 0, /* tp_dictoffset */
1207 0, /* tp_init */
Georg Brandl6b50c632006-06-01 08:27:32 +00001208 PyType_GenericAlloc, /* tp_alloc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001209 complex_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00001210 PyObject_Del, /* tp_free */
Guido van Rossumf9fca921996-01-12 00:47:05 +00001211};
1212
1213#endif