blob: 4948a21fb0c0ef01b82811f86a25e9f890b3e65b [file] [log] [blame]
Guido van Rossum96783941997-05-20 18:21:34 +00001
Guido van Rossumf9fca921996-01-12 00:47:05 +00002/* Complex object implementation */
3
4/* Borrows heavily from floatobject.c */
5
Guido van Rossum96783941997-05-20 18:21:34 +00006/* Submitted by Jim Hugunin */
7
Guido van Rossumc0b618a1997-05-02 03:12:38 +00008#include "Python.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +00009#include "structmember.h"
Guido van Rossumf9fca921996-01-12 00:47:05 +000010
Guido van Rossum65ce6de2002-06-13 17:07:07 +000011#ifndef WITHOUT_COMPLEX
12
Guido van Rossumf9fca921996-01-12 00:47:05 +000013/* elementary operations on complex numbers */
14
Guido van Rossum9e720e31996-07-21 02:31:35 +000015static Py_complex c_1 = {1., 0.};
Guido van Rossumf9fca921996-01-12 00:47:05 +000016
Tim Peters0f336042001-03-18 08:21:57 +000017Py_complex
18c_sum(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +000019{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000020 Py_complex r;
21 r.real = a.real + b.real;
22 r.imag = a.imag + b.imag;
23 return r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000024}
25
Tim Peters0f336042001-03-18 08:21:57 +000026Py_complex
27c_diff(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +000028{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000029 Py_complex r;
30 r.real = a.real - b.real;
31 r.imag = a.imag - b.imag;
32 return r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000033}
34
Tim Peters0f336042001-03-18 08:21:57 +000035Py_complex
36c_neg(Py_complex a)
Guido van Rossumf9fca921996-01-12 00:47:05 +000037{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000038 Py_complex r;
39 r.real = -a.real;
40 r.imag = -a.imag;
41 return r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000042}
43
Tim Peters0f336042001-03-18 08:21:57 +000044Py_complex
45c_prod(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +000046{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000047 Py_complex r;
48 r.real = a.real*b.real - a.imag*b.imag;
49 r.imag = a.real*b.imag + a.imag*b.real;
50 return r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000051}
52
Tim Peters0f336042001-03-18 08:21:57 +000053Py_complex
54c_quot(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +000055{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000056 /******************************************************************
57 This was the original algorithm. It's grossly prone to spurious
58 overflow and underflow errors. It also merrily divides by 0 despite
59 checking for that(!). The code still serves a doc purpose here, as
60 the algorithm following is a simple by-cases transformation of this
61 one:
Tim Peters0f336042001-03-18 08:21:57 +000062
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000063 Py_complex r;
64 double d = b.real*b.real + b.imag*b.imag;
65 if (d == 0.)
66 errno = EDOM;
67 r.real = (a.real*b.real + a.imag*b.imag)/d;
68 r.imag = (a.imag*b.real - a.real*b.imag)/d;
69 return r;
70 ******************************************************************/
Tim Peters0f336042001-03-18 08:21:57 +000071
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000072 /* This algorithm is better, and is pretty obvious: first divide the
73 * numerators and denominator by whichever of {b.real, b.imag} has
74 * larger magnitude. The earliest reference I found was to CACM
75 * Algorithm 116 (Complex Division, Robert L. Smith, Stanford
76 * University). As usual, though, we're still ignoring all IEEE
77 * endcases.
78 */
79 Py_complex r; /* the result */
80 const double abs_breal = b.real < 0 ? -b.real : b.real;
81 const double abs_bimag = b.imag < 0 ? -b.imag : b.imag;
Tim Peters0f336042001-03-18 08:21:57 +000082
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000083 if (abs_breal >= abs_bimag) {
84 /* divide tops and bottom by b.real */
85 if (abs_breal == 0.0) {
86 errno = EDOM;
87 r.real = r.imag = 0.0;
88 }
89 else {
90 const double ratio = b.imag / b.real;
91 const double denom = b.real + b.imag * ratio;
92 r.real = (a.real + a.imag * ratio) / denom;
93 r.imag = (a.imag - a.real * ratio) / denom;
94 }
95 }
96 else {
97 /* divide tops and bottom by b.imag */
98 const double ratio = b.real / b.imag;
99 const double denom = b.real * ratio + b.imag;
100 assert(b.imag != 0.0);
101 r.real = (a.real * ratio + a.imag) / denom;
102 r.imag = (a.imag * ratio - a.real) / denom;
103 }
104 return r;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000105}
106
Tim Peters0f336042001-03-18 08:21:57 +0000107Py_complex
108c_pow(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000109{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000110 Py_complex r;
111 double vabs,len,at,phase;
112 if (b.real == 0. && b.imag == 0.) {
113 r.real = 1.;
114 r.imag = 0.;
115 }
116 else if (a.real == 0. && a.imag == 0.) {
117 if (b.imag != 0. || b.real < 0.)
118 errno = EDOM;
119 r.real = 0.;
120 r.imag = 0.;
121 }
122 else {
123 vabs = hypot(a.real,a.imag);
124 len = pow(vabs,b.real);
125 at = atan2(a.imag, a.real);
126 phase = at*b.real;
127 if (b.imag != 0.0) {
128 len /= exp(at*b.imag);
129 phase += b.imag*log(vabs);
130 }
131 r.real = len*cos(phase);
132 r.imag = len*sin(phase);
133 }
134 return r;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000135}
136
Tim Peters0f336042001-03-18 08:21:57 +0000137static Py_complex
138c_powu(Py_complex x, long n)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000139{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000140 Py_complex r, p;
141 long mask = 1;
142 r = c_1;
143 p = x;
144 while (mask > 0 && n >= mask) {
145 if (n & mask)
146 r = c_prod(r,p);
147 mask <<= 1;
148 p = c_prod(p,p);
149 }
150 return r;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000151}
152
Tim Peters0f336042001-03-18 08:21:57 +0000153static Py_complex
154c_powi(Py_complex x, long n)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000155{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000156 Py_complex cn;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000157
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000158 if (n > 100 || n < -100) {
159 cn.real = (double) n;
160 cn.imag = 0.;
161 return c_pow(x,cn);
162 }
163 else if (n > 0)
164 return c_powu(x,n);
165 else
166 return c_quot(c_1,c_powu(x,-n));
Guido van Rossumf9fca921996-01-12 00:47:05 +0000167
168}
169
Christian Heimes53876d92008-04-19 00:31:39 +0000170double
171c_abs(Py_complex z)
172{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000173 /* sets errno = ERANGE on overflow; otherwise errno = 0 */
174 double result;
Christian Heimes53876d92008-04-19 00:31:39 +0000175
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000176 if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
177 /* C99 rules: if either the real or the imaginary part is an
178 infinity, return infinity, even if the other part is a
179 NaN. */
180 if (Py_IS_INFINITY(z.real)) {
181 result = fabs(z.real);
182 errno = 0;
183 return result;
184 }
185 if (Py_IS_INFINITY(z.imag)) {
186 result = fabs(z.imag);
187 errno = 0;
188 return result;
189 }
190 /* either the real or imaginary part is a NaN,
191 and neither is infinite. Result should be NaN. */
192 return Py_NAN;
193 }
194 result = hypot(z.real, z.imag);
195 if (!Py_IS_FINITE(result))
196 errno = ERANGE;
197 else
198 errno = 0;
199 return result;
Christian Heimes53876d92008-04-19 00:31:39 +0000200}
201
Tim Peters6d6c1a32001-08-02 04:15:00 +0000202static PyObject *
203complex_subtype_from_c_complex(PyTypeObject *type, Py_complex cval)
204{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000205 PyObject *op;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000206
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000207 op = type->tp_alloc(type, 0);
208 if (op != NULL)
209 ((PyComplexObject *)op)->cval = cval;
210 return op;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000211}
212
Guido van Rossumf9fca921996-01-12 00:47:05 +0000213PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000214PyComplex_FromCComplex(Py_complex cval)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000215{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000216 register PyComplexObject *op;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000217
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000218 /* Inline PyObject_New */
219 op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject));
220 if (op == NULL)
221 return PyErr_NoMemory();
222 PyObject_INIT(op, &PyComplex_Type);
223 op->cval = cval;
224 return (PyObject *) op;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000225}
226
Tim Peters6d6c1a32001-08-02 04:15:00 +0000227static PyObject *
228complex_subtype_from_doubles(PyTypeObject *type, double real, double imag)
229{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000230 Py_complex c;
231 c.real = real;
232 c.imag = imag;
233 return complex_subtype_from_c_complex(type, c);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000234}
235
Guido van Rossumf9fca921996-01-12 00:47:05 +0000236PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000237PyComplex_FromDoubles(double real, double imag)
Guido van Rossum926518b1996-08-19 19:30:45 +0000238{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000239 Py_complex c;
240 c.real = real;
241 c.imag = imag;
242 return PyComplex_FromCComplex(c);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000243}
244
245double
Fred Drake4288c802000-07-09 04:36:04 +0000246PyComplex_RealAsDouble(PyObject *op)
Guido van Rossum926518b1996-08-19 19:30:45 +0000247{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000248 if (PyComplex_Check(op)) {
249 return ((PyComplexObject *)op)->cval.real;
250 }
251 else {
252 return PyFloat_AsDouble(op);
253 }
Guido van Rossumf9fca921996-01-12 00:47:05 +0000254}
255
256double
Fred Drake4288c802000-07-09 04:36:04 +0000257PyComplex_ImagAsDouble(PyObject *op)
Guido van Rossum926518b1996-08-19 19:30:45 +0000258{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000259 if (PyComplex_Check(op)) {
260 return ((PyComplexObject *)op)->cval.imag;
261 }
262 else {
263 return 0.0;
264 }
Guido van Rossumf9fca921996-01-12 00:47:05 +0000265}
266
Guido van Rossum9e720e31996-07-21 02:31:35 +0000267Py_complex
Fred Drake4288c802000-07-09 04:36:04 +0000268PyComplex_AsCComplex(PyObject *op)
Guido van Rossum926518b1996-08-19 19:30:45 +0000269{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000270 Py_complex cv;
271 PyObject *newop = NULL;
272 static PyObject *complex_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000273
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000274 assert(op);
275 /* If op is already of type PyComplex_Type, return its value */
276 if (PyComplex_Check(op)) {
277 return ((PyComplexObject *)op)->cval;
278 }
279 /* If not, use op's __complex__ method, if it exists */
Christian Heimesfe82e772008-01-28 02:38:20 +0000280
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000281 /* return -1 on failure */
282 cv.real = -1.;
283 cv.imag = 0.;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000284
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000285 if (complex_str == NULL) {
286 if (!(complex_str = PyUnicode_FromString("__complex__")))
287 return cv;
288 }
289
290 {
291 PyObject *complexfunc;
292 complexfunc = _PyType_Lookup(op->ob_type, complex_str);
293 /* complexfunc is a borrowed reference */
294 if (complexfunc) {
295 newop = PyObject_CallFunctionObjArgs(complexfunc, op, NULL);
296 if (!newop)
297 return cv;
298 }
299 }
300
301 if (newop) {
302 if (!PyComplex_Check(newop)) {
303 PyErr_SetString(PyExc_TypeError,
304 "__complex__ should return a complex object");
305 Py_DECREF(newop);
306 return cv;
307 }
308 cv = ((PyComplexObject *)newop)->cval;
309 Py_DECREF(newop);
310 return cv;
311 }
312 /* 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. */
314 else {
315 /* PyFloat_AsDouble will return -1 on failure */
316 cv.real = PyFloat_AsDouble(op);
317 return cv;
318 }
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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000331 PyObject *result = NULL;
332 Py_ssize_t len;
Eric Smith0923d1d2009-04-16 20:16:10 +0000333
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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,
349 precision, 0, NULL);
350 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,
357 precision, 0, NULL);
358 if (!pre) {
359 PyErr_NoMemory();
360 goto done;
361 }
362 re = pre;
Eric Smith0923d1d2009-04-16 20:16:10 +0000363
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000364 im = PyOS_double_to_string(v->cval.imag, format_code,
365 precision, Py_DTSF_SIGN, NULL);
366 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);
Mark Dickinsonad476da2009-04-23 19:14:16 +0000383 done:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000384 PyMem_Free(im);
385 PyMem_Free(pre);
386 PyMem_Free(buf);
Eric Smith0923d1d2009-04-16 20:16:10 +0000387
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000427 if (PyComplex_Check(obj)) \
428 c = ((PyComplexObject *)(obj))->cval; \
429 else if (to_complex(&(obj), &(c)) < 0) \
430 return (obj)
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000431
432static int
433to_complex(PyObject **pobj, Py_complex *pc)
434{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000435 PyObject *obj = *pobj;
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000436
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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}
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000454
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000455
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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000459 Py_complex result;
460 Py_complex a, b;
461 TO_COMPLEX(v, a);
462 TO_COMPLEX(w, b);
463 PyFPE_START_PROTECT("complex_add", return 0)
464 result = c_sum(a, b);
465 PyFPE_END_PROTECT(result)
466 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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000472 Py_complex result;
473 Py_complex a, b;
474 TO_COMPLEX(v, a);
475 TO_COMPLEX(w, b);
476 PyFPE_START_PROTECT("complex_sub", return 0)
477 result = c_diff(a, b);
478 PyFPE_END_PROTECT(result)
479 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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000485 Py_complex result;
486 Py_complex a, b;
487 TO_COMPLEX(v, a);
488 TO_COMPLEX(w, b);
489 PyFPE_START_PROTECT("complex_mul", return 0)
490 result = c_prod(a, b);
491 PyFPE_END_PROTECT(result)
492 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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000498 Py_complex quot;
499 Py_complex a, b;
500 TO_COMPLEX(v, a);
501 TO_COMPLEX(w, b);
502 PyFPE_START_PROTECT("complex_div", return 0)
503 errno = 0;
504 quot = c_quot(a, b);
505 PyFPE_END_PROTECT(quot)
506 if (errno == EDOM) {
507 PyErr_SetString(PyExc_ZeroDivisionError, "complex division");
508 return NULL;
509 }
510 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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000533 Py_complex p;
534 Py_complex exponent;
535 long int_exponent;
536 Py_complex a, b;
537 TO_COMPLEX(v, a);
538 TO_COMPLEX(w, b);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000539
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000540 if (z != Py_None) {
541 PyErr_SetString(PyExc_ValueError, "complex modulo");
542 return NULL;
543 }
544 PyFPE_START_PROTECT("complex_pow", return 0)
545 errno = 0;
546 exponent = b;
547 int_exponent = (long)exponent.real;
548 if (exponent.imag == 0. && exponent.real == int_exponent)
549 p = c_powi(a, int_exponent);
550 else
551 p = c_pow(a, exponent);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000552
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000553 PyFPE_END_PROTECT(p)
554 Py_ADJUST_ERANGE2(p.real, p.imag);
555 if (errno == EDOM) {
556 PyErr_SetString(PyExc_ZeroDivisionError,
557 "0.0 to a negative or complex power");
558 return NULL;
559 }
560 else if (errno == ERANGE) {
561 PyErr_SetString(PyExc_OverflowError,
562 "complex exponentiation");
563 return NULL;
564 }
565 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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000571 PyErr_SetString(PyExc_TypeError,
572 "can't take floor of complex number.");
573 return NULL;
Guido van Rossum4668b002001-08-08 05:00:18 +0000574}
575
576static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000577complex_neg(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000578{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000579 Py_complex neg;
580 neg.real = -v->cval.real;
581 neg.imag = -v->cval.imag;
582 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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000599 double result;
Christian Heimes53876d92008-04-19 00:31:39 +0000600
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000601 PyFPE_START_PROTECT("complex_abs", return 0)
602 result = c_abs(v->cval);
603 PyFPE_END_PROTECT(result)
Christian Heimes53876d92008-04-19 00:31:39 +0000604
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000605 if (errno == ERANGE) {
606 PyErr_SetString(PyExc_OverflowError,
607 "absolute value too large");
608 return NULL;
609 }
610 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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000622 PyObject *res;
623 Py_complex i, j;
624 TO_COMPLEX(v, i);
625 TO_COMPLEX(w, j);
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000626
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000627 if (op != Py_EQ && op != Py_NE) {
628 /* XXX Should eventually return NotImplemented */
629 PyErr_SetString(PyExc_TypeError,
630 "no ordering relation is defined for complex numbers");
631 return NULL;
632 }
Guido van Rossum22056422001-09-24 17:52:04 +0000633
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000634 if ((i.real == j.real && i.imag == j.imag) == (op == Py_EQ))
635 res = Py_True;
636 else
637 res = Py_False;
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000638
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000639 Py_INCREF(res);
640 return res;
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000641}
642
643static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000644complex_int(PyObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000645{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000646 PyErr_SetString(PyExc_TypeError,
647 "can't convert complex to int");
648 return NULL;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000649}
650
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000651static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000652complex_float(PyObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000653{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000654 PyErr_SetString(PyExc_TypeError,
655 "can't convert complex to float");
656 return NULL;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000657}
658
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000659static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000660complex_conjugate(PyObject *self)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000661{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000662 Py_complex c;
663 c = ((PyComplexObject *)self)->cval;
664 c.imag = -c.imag;
665 return PyComplex_FromCComplex(c);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000666}
667
Guido van Rossum46334cd2007-08-01 17:43:15 +0000668PyDoc_STRVAR(complex_conjugate_doc,
669"complex.conjugate() -> complex\n"
670"\n"
671"Returns the complex conjugate of its argument. (3-4j).conjugate() == 3+4j.");
672
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000673static PyObject *
674complex_getnewargs(PyComplexObject *v)
675{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000676 Py_complex c = v->cval;
677 return Py_BuildValue("(dd)", c.real, c.imag);
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000678}
679
Eric Smith58a42242009-04-30 01:00:33 +0000680PyDoc_STRVAR(complex__format__doc,
681"complex.__format__() -> str\n"
682"\n"
683"Converts to a string according to format_spec.");
684
685static PyObject *
686complex__format__(PyObject* self, PyObject* args)
687{
688 PyObject *format_spec;
689
690 if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000691 return NULL;
Eric Smith58a42242009-04-30 01:00:33 +0000692 return _PyComplex_FormatAdvanced(self,
693 PyUnicode_AS_UNICODE(format_spec),
694 PyUnicode_GET_SIZE(format_spec));
695}
696
Christian Heimes53876d92008-04-19 00:31:39 +0000697#if 0
698static PyObject *
699complex_is_finite(PyObject *self)
700{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000701 Py_complex c;
702 c = ((PyComplexObject *)self)->cval;
703 return PyBool_FromLong((long)(Py_IS_FINITE(c.real) &&
704 Py_IS_FINITE(c.imag)));
Christian Heimes53876d92008-04-19 00:31:39 +0000705}
706
707PyDoc_STRVAR(complex_is_finite_doc,
708"complex.is_finite() -> bool\n"
709"\n"
710"Returns True if the real and the imaginary part is finite.");
711#endif
712
Guido van Rossumf9fca921996-01-12 00:47:05 +0000713static PyMethodDef complex_methods[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000714 {"conjugate", (PyCFunction)complex_conjugate, METH_NOARGS,
715 complex_conjugate_doc},
Christian Heimes53876d92008-04-19 00:31:39 +0000716#if 0
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000717 {"is_finite", (PyCFunction)complex_is_finite, METH_NOARGS,
718 complex_is_finite_doc},
Christian Heimes53876d92008-04-19 00:31:39 +0000719#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000720 {"__getnewargs__", (PyCFunction)complex_getnewargs, METH_NOARGS},
721 {"__format__", (PyCFunction)complex__format__,
722 METH_VARARGS, complex__format__doc},
723 {NULL, NULL} /* sentinel */
Guido van Rossumf9fca921996-01-12 00:47:05 +0000724};
725
Guido van Rossum6f799372001-09-20 20:46:19 +0000726static PyMemberDef complex_members[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000727 {"real", T_DOUBLE, offsetof(PyComplexObject, cval.real), READONLY,
728 "the real part of a complex number"},
729 {"imag", T_DOUBLE, offsetof(PyComplexObject, cval.imag), READONLY,
730 "the imaginary part of a complex number"},
731 {0},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000732};
Guido van Rossumf9fca921996-01-12 00:47:05 +0000733
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000734static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000735complex_subtype_from_string(PyTypeObject *type, PyObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000736{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000737 const char *s, *start;
738 char *end;
739 double x=0.0, y=0.0, z;
740 int got_bracket=0;
741 char s_buffer[256];
742 Py_ssize_t len;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000743
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000744 if (PyUnicode_Check(v)) {
745 if (PyUnicode_GET_SIZE(v) >= (Py_ssize_t)sizeof(s_buffer)) {
746 PyErr_SetString(PyExc_ValueError,
747 "complex() literal too large to convert");
748 return NULL;
749 }
750 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
751 PyUnicode_GET_SIZE(v),
752 s_buffer,
753 NULL))
754 return NULL;
755 s = s_buffer;
756 len = strlen(s);
757 }
758 else if (PyObject_AsCharBuffer(v, &s, &len)) {
759 PyErr_SetString(PyExc_TypeError,
760 "complex() arg is not a string");
761 return NULL;
762 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000763
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000764 /* position on first nonblank */
765 start = s;
766 while (Py_ISSPACE(*s))
767 s++;
768 if (*s == '(') {
769 /* Skip over possible bracket from repr(). */
770 got_bracket = 1;
771 s++;
772 while (Py_ISSPACE(*s))
773 s++;
774 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000775
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000776 /* a valid complex string usually takes one of the three forms:
Mark Dickinson6649fa42009-04-24 13:25:20 +0000777
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000778 <float> - real part only
779 <float>j - imaginary part only
780 <float><signed-float>j - real and imaginary parts
Mark Dickinson6649fa42009-04-24 13:25:20 +0000781
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000782 where <float> represents any numeric string that's accepted by the
783 float constructor (including 'nan', 'inf', 'infinity', etc.), and
784 <signed-float> is any string of the form <float> whose first
785 character is '+' or '-'.
Mark Dickinson6649fa42009-04-24 13:25:20 +0000786
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000787 For backwards compatibility, the extra forms
Mark Dickinson6649fa42009-04-24 13:25:20 +0000788
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000789 <float><sign>j
790 <sign>j
791 j
Mark Dickinson6649fa42009-04-24 13:25:20 +0000792
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000793 are also accepted, though support for these forms may be removed from
794 a future version of Python.
795 */
Mark Dickinson6649fa42009-04-24 13:25:20 +0000796
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000797 /* first look for forms starting with <float> */
798 z = PyOS_string_to_double(s, &end, NULL);
799 if (z == -1.0 && PyErr_Occurred()) {
800 if (PyErr_ExceptionMatches(PyExc_ValueError))
801 PyErr_Clear();
802 else
803 return NULL;
804 }
805 if (end != s) {
806 /* all 4 forms starting with <float> land here */
807 s = end;
808 if (*s == '+' || *s == '-') {
809 /* <float><signed-float>j | <float><sign>j */
810 x = z;
811 y = PyOS_string_to_double(s, &end, NULL);
812 if (y == -1.0 && PyErr_Occurred()) {
813 if (PyErr_ExceptionMatches(PyExc_ValueError))
814 PyErr_Clear();
815 else
816 return NULL;
817 }
818 if (end != s)
819 /* <float><signed-float>j */
820 s = end;
821 else {
822 /* <float><sign>j */
823 y = *s == '+' ? 1.0 : -1.0;
824 s++;
825 }
826 if (!(*s == 'j' || *s == 'J'))
827 goto parse_error;
828 s++;
829 }
830 else if (*s == 'j' || *s == 'J') {
831 /* <float>j */
832 s++;
833 y = z;
834 }
835 else
836 /* <float> */
837 x = z;
838 }
839 else {
840 /* not starting with <float>; must be <sign>j or j */
841 if (*s == '+' || *s == '-') {
842 /* <sign>j */
843 y = *s == '+' ? 1.0 : -1.0;
844 s++;
845 }
846 else
847 /* j */
848 y = 1.0;
849 if (!(*s == 'j' || *s == 'J'))
850 goto parse_error;
851 s++;
852 }
Mark Dickinsonad476da2009-04-23 19:14:16 +0000853
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000854 /* trailing whitespace and closing bracket */
855 while (Py_ISSPACE(*s))
856 s++;
857 if (got_bracket) {
858 /* if there was an opening parenthesis, then the corresponding
859 closing parenthesis should be right here */
860 if (*s != ')')
861 goto parse_error;
862 s++;
863 while (Py_ISSPACE(*s))
864 s++;
865 }
Mark Dickinson6649fa42009-04-24 13:25:20 +0000866
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000867 /* we should now be at the end of the string */
868 if (s-start != len)
869 goto parse_error;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000870
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000871 return complex_subtype_from_doubles(type, x, y);
Mark Dickinsonad476da2009-04-23 19:14:16 +0000872
Mark Dickinson6649fa42009-04-24 13:25:20 +0000873 parse_error:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000874 PyErr_SetString(PyExc_ValueError,
875 "complex() arg is a malformed string");
876 return NULL;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000877}
878
Tim Peters6d6c1a32001-08-02 04:15:00 +0000879static PyObject *
880complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
881{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000882 PyObject *r, *i, *tmp, *f;
883 PyNumberMethods *nbr, *nbi = NULL;
884 Py_complex cr, ci;
885 int own_r = 0;
886 int cr_is_complex = 0;
887 int ci_is_complex = 0;
888 static PyObject *complexstr;
889 static char *kwlist[] = {"real", "imag", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000890
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000891 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
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000897 /* Special-case for a single argument when type(arg) is complex. */
898 if (PyComplex_CheckExact(r) && i == NULL &&
899 type == &PyComplex_Type) {
900 /* Note that we can't know whether it's safe to return
901 a complex *subclass* instance as-is, hence the restriction
902 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. */
905 Py_INCREF(r);
906 return r;
907 }
908 if (PyUnicode_Check(r)) {
909 if (i != NULL) {
910 PyErr_SetString(PyExc_TypeError,
911 "complex() can't take second arg"
912 " if first is a string");
913 return NULL;
914 }
915 return complex_subtype_from_string(type, r);
916 }
917 if (i != NULL && PyUnicode_Check(i)) {
918 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
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000923 /* XXX Hack to support classes with __complex__ method */
924 if (complexstr == NULL) {
925 complexstr = PyUnicode_InternFromString("__complex__");
926 if (complexstr == NULL)
927 return NULL;
928 }
929 f = PyObject_GetAttr(r, complexstr);
930 if (f == NULL)
931 PyErr_Clear();
932 else {
933 PyObject *args = PyTuple_New(0);
934 if (args == NULL)
935 return NULL;
936 r = PyEval_CallObject(f, args);
937 Py_DECREF(args);
938 Py_DECREF(f);
939 if (r == NULL)
940 return NULL;
941 own_r = 1;
942 }
943 nbr = r->ob_type->tp_as_number;
944 if (i != NULL)
945 nbi = i->ob_type->tp_as_number;
946 if (nbr == NULL || nbr->nb_float == NULL ||
947 ((i != NULL) && (nbi == NULL || nbi->nb_float == NULL))) {
948 PyErr_SetString(PyExc_TypeError,
949 "complex() argument must be a string or a number");
950 if (own_r) {
951 Py_DECREF(r);
952 }
953 return NULL;
954 }
Guido van Rossumd8faa362007-04-27 19:54:29 +0000955
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000956 /* If we get this far, then the "real" and "imag" parts should
957 both be treated as numbers, and the constructor should return a
958 complex number equal to (real + imag*1j).
Guido van Rossumd8faa362007-04-27 19:54:29 +0000959
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000960 Note that we do NOT assume the input to already be in canonical
961 form; the "real" and "imag" parts might themselves be complex
962 numbers, which slightly complicates the code below. */
963 if (PyComplex_Check(r)) {
964 /* Note that if r is of a complex subtype, we're only
965 retaining its real & imag parts here, and the return
966 value is (properly) of the builtin complex type. */
967 cr = ((PyComplexObject*)r)->cval;
968 cr_is_complex = 1;
969 if (own_r) {
970 Py_DECREF(r);
971 }
972 }
973 else {
974 /* The "real" part really is entirely real, and contributes
975 nothing in the imaginary direction.
976 Just treat it as a double. */
977 tmp = PyNumber_Float(r);
978 if (own_r) {
979 /* r was a newly created complex number, rather
980 than the original "real" argument. */
981 Py_DECREF(r);
982 }
983 if (tmp == NULL)
984 return NULL;
985 if (!PyFloat_Check(tmp)) {
986 PyErr_SetString(PyExc_TypeError,
987 "float(r) didn't return a float");
988 Py_DECREF(tmp);
989 return NULL;
990 }
991 cr.real = PyFloat_AsDouble(tmp);
992 cr.imag = 0.0; /* Shut up compiler warning */
993 Py_DECREF(tmp);
994 }
995 if (i == NULL) {
996 ci.real = 0.0;
997 }
998 else if (PyComplex_Check(i)) {
999 ci = ((PyComplexObject*)i)->cval;
1000 ci_is_complex = 1;
1001 } else {
1002 /* The "imag" part really is entirely imaginary, and
1003 contributes nothing in the real direction.
1004 Just treat it as a double. */
1005 tmp = (*nbi->nb_float)(i);
1006 if (tmp == NULL)
1007 return NULL;
1008 ci.real = PyFloat_AsDouble(tmp);
1009 Py_DECREF(tmp);
1010 }
1011 /* If the input was in canonical form, then the "real" and "imag"
1012 parts are real numbers, so that ci.imag and cr.imag are zero.
1013 We need this correction in case they were not real numbers. */
Christian Heimes69a79632007-11-28 10:04:30 +00001014
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001015 if (ci_is_complex) {
1016 cr.real -= ci.imag;
1017 }
1018 if (cr_is_complex) {
1019 ci.real += cr.imag;
1020 }
1021 return complex_subtype_from_doubles(type, cr.real, ci.real);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001022}
1023
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001024PyDoc_STRVAR(complex_doc,
Tim Peters2400fa42001-09-12 19:12:49 +00001025"complex(real[, imag]) -> complex number\n"
1026"\n"
1027"Create a complex number from a real part and an optional imaginary part.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001028"This is equivalent to (real + imag*1j) where imag defaults to 0.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001029
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001030static PyNumberMethods complex_as_number = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001031 (binaryfunc)complex_add, /* nb_add */
1032 (binaryfunc)complex_sub, /* nb_subtract */
1033 (binaryfunc)complex_mul, /* nb_multiply */
1034 (binaryfunc)complex_remainder, /* nb_remainder */
1035 (binaryfunc)complex_divmod, /* nb_divmod */
1036 (ternaryfunc)complex_pow, /* nb_power */
1037 (unaryfunc)complex_neg, /* nb_negative */
1038 (unaryfunc)complex_pos, /* nb_positive */
1039 (unaryfunc)complex_abs, /* nb_absolute */
1040 (inquiry)complex_bool, /* nb_bool */
1041 0, /* nb_invert */
1042 0, /* nb_lshift */
1043 0, /* nb_rshift */
1044 0, /* nb_and */
1045 0, /* nb_xor */
1046 0, /* nb_or */
1047 complex_int, /* nb_int */
1048 0, /* nb_reserved */
1049 complex_float, /* nb_float */
1050 0, /* nb_inplace_add */
1051 0, /* nb_inplace_subtract */
1052 0, /* nb_inplace_multiply*/
1053 0, /* nb_inplace_remainder */
1054 0, /* nb_inplace_power */
1055 0, /* nb_inplace_lshift */
1056 0, /* nb_inplace_rshift */
1057 0, /* nb_inplace_and */
1058 0, /* nb_inplace_xor */
1059 0, /* nb_inplace_or */
1060 (binaryfunc)complex_int_div, /* nb_floor_divide */
1061 (binaryfunc)complex_div, /* nb_true_divide */
1062 0, /* nb_inplace_floor_divide */
1063 0, /* nb_inplace_true_divide */
Guido van Rossumf9fca921996-01-12 00:47:05 +00001064};
1065
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001066PyTypeObject PyComplex_Type = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001067 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1068 "complex",
1069 sizeof(PyComplexObject),
1070 0,
1071 complex_dealloc, /* tp_dealloc */
1072 0, /* tp_print */
1073 0, /* tp_getattr */
1074 0, /* tp_setattr */
1075 0, /* tp_reserved */
1076 (reprfunc)complex_repr, /* tp_repr */
1077 &complex_as_number, /* tp_as_number */
1078 0, /* tp_as_sequence */
1079 0, /* tp_as_mapping */
1080 (hashfunc)complex_hash, /* tp_hash */
1081 0, /* tp_call */
1082 (reprfunc)complex_str, /* tp_str */
1083 PyObject_GenericGetAttr, /* tp_getattro */
1084 0, /* tp_setattro */
1085 0, /* tp_as_buffer */
1086 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1087 complex_doc, /* tp_doc */
1088 0, /* tp_traverse */
1089 0, /* tp_clear */
1090 complex_richcompare, /* tp_richcompare */
1091 0, /* tp_weaklistoffset */
1092 0, /* tp_iter */
1093 0, /* tp_iternext */
1094 complex_methods, /* tp_methods */
1095 complex_members, /* tp_members */
1096 0, /* tp_getset */
1097 0, /* tp_base */
1098 0, /* tp_dict */
1099 0, /* tp_descr_get */
1100 0, /* tp_descr_set */
1101 0, /* tp_dictoffset */
1102 0, /* tp_init */
1103 PyType_GenericAlloc, /* tp_alloc */
1104 complex_new, /* tp_new */
1105 PyObject_Del, /* tp_free */
Guido van Rossumf9fca921996-01-12 00:47:05 +00001106};
1107
1108#endif