blob: a8a218869ee7d156e6f5f80ece339ab981032cbb [file] [log] [blame]
Guido van Rossum96783941997-05-20 18:21:34 +00001
Guido van Rossumf9fca921996-01-12 00:47:05 +00002/* Complex object implementation */
3
4/* Borrows heavily from floatobject.c */
5
Guido van Rossum96783941997-05-20 18:21:34 +00006/* Submitted by Jim Hugunin */
7
Guido van Rossumc0b618a1997-05-02 03:12:38 +00008#include "Python.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +00009#include "structmember.h"
Guido van Rossumf9fca921996-01-12 00:47:05 +000010
Guido van Rossum65ce6de2002-06-13 17:07:07 +000011#ifndef WITHOUT_COMPLEX
12
Tim Peters70695122001-03-11 08:37:29 +000013/* Precisions used by repr() and str(), respectively.
14
15 The repr() precision (17 significant decimal digits) is the minimal number
16 that is guaranteed to have enough precision so that if the number is read
17 back in the exact same binary value is recreated. This is true for IEEE
18 floating point by design, and also happens to work for all other modern
19 hardware.
20
21 The str() precision is chosen so that in most cases, the rounding noise
22 created by various operations is suppressed, while giving plenty of
23 precision for practical use.
24*/
25
26#define PREC_REPR 17
27#define PREC_STR 12
Guido van Rossumf9fca921996-01-12 00:47:05 +000028
29/* elementary operations on complex numbers */
30
Guido van Rossum9e720e31996-07-21 02:31:35 +000031static Py_complex c_1 = {1., 0.};
Guido van Rossumf9fca921996-01-12 00:47:05 +000032
Tim Peters0f336042001-03-18 08:21:57 +000033Py_complex
34c_sum(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +000035{
Guido van Rossum9e720e31996-07-21 02:31:35 +000036 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000037 r.real = a.real + b.real;
38 r.imag = a.imag + b.imag;
39 return r;
40}
41
Tim Peters0f336042001-03-18 08:21:57 +000042Py_complex
43c_diff(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +000044{
Guido van Rossum9e720e31996-07-21 02:31:35 +000045 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000046 r.real = a.real - b.real;
47 r.imag = a.imag - b.imag;
48 return r;
49}
50
Tim Peters0f336042001-03-18 08:21:57 +000051Py_complex
52c_neg(Py_complex a)
Guido van Rossumf9fca921996-01-12 00:47:05 +000053{
Guido van Rossum9e720e31996-07-21 02:31:35 +000054 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000055 r.real = -a.real;
56 r.imag = -a.imag;
57 return r;
58}
59
Tim Peters0f336042001-03-18 08:21:57 +000060Py_complex
61c_prod(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +000062{
Guido van Rossum9e720e31996-07-21 02:31:35 +000063 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000064 r.real = a.real*b.real - a.imag*b.imag;
65 r.imag = a.real*b.imag + a.imag*b.real;
66 return r;
67}
68
Tim Peters0f336042001-03-18 08:21:57 +000069Py_complex
70c_quot(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +000071{
Tim Peters0f336042001-03-18 08:21:57 +000072 /******************************************************************
73 This was the original algorithm. It's grossly prone to spurious
74 overflow and underflow errors. It also merrily divides by 0 despite
75 checking for that(!). The code still serves a doc purpose here, as
76 the algorithm following is a simple by-cases transformation of this
77 one:
78
Guido van Rossum9e720e31996-07-21 02:31:35 +000079 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +000080 double d = b.real*b.real + b.imag*b.imag;
81 if (d == 0.)
Guido van Rossum96783941997-05-20 18:21:34 +000082 errno = EDOM;
Guido van Rossumf9fca921996-01-12 00:47:05 +000083 r.real = (a.real*b.real + a.imag*b.imag)/d;
84 r.imag = (a.imag*b.real - a.real*b.imag)/d;
85 return r;
Tim Peters0f336042001-03-18 08:21:57 +000086 ******************************************************************/
87
88 /* This algorithm is better, and is pretty obvious: first divide the
89 * numerators and denominator by whichever of {b.real, b.imag} has
90 * larger magnitude. The earliest reference I found was to CACM
91 * Algorithm 116 (Complex Division, Robert L. Smith, Stanford
92 * University). As usual, though, we're still ignoring all IEEE
93 * endcases.
94 */
95 Py_complex r; /* the result */
96 const double abs_breal = b.real < 0 ? -b.real : b.real;
97 const double abs_bimag = b.imag < 0 ? -b.imag : b.imag;
98
99 if (abs_breal >= abs_bimag) {
100 /* divide tops and bottom by b.real */
101 if (abs_breal == 0.0) {
102 errno = EDOM;
103 r.real = r.imag = 0.0;
104 }
105 else {
106 const double ratio = b.imag / b.real;
107 const double denom = b.real + b.imag * ratio;
108 r.real = (a.real + a.imag * ratio) / denom;
109 r.imag = (a.imag - a.real * ratio) / denom;
110 }
111 }
112 else {
113 /* divide tops and bottom by b.imag */
114 const double ratio = b.real / b.imag;
115 const double denom = b.real * ratio + b.imag;
116 assert(b.imag != 0.0);
117 r.real = (a.real * ratio + a.imag) / denom;
118 r.imag = (a.imag * ratio - a.real) / denom;
119 }
120 return r;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000121}
122
Tim Peters0f336042001-03-18 08:21:57 +0000123Py_complex
124c_pow(Py_complex a, Py_complex b)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000125{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000126 Py_complex r;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000127 double vabs,len,at,phase;
128 if (b.real == 0. && b.imag == 0.) {
129 r.real = 1.;
130 r.imag = 0.;
131 }
132 else if (a.real == 0. && a.imag == 0.) {
133 if (b.imag != 0. || b.real < 0.)
Tim Petersbab22be2002-03-22 02:48:46 +0000134 errno = EDOM;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000135 r.real = 0.;
136 r.imag = 0.;
137 }
138 else {
139 vabs = hypot(a.real,a.imag);
140 len = pow(vabs,b.real);
Martin v. Löwis387c5472001-09-06 08:16:17 +0000141 at = atan2(a.imag, a.real);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000142 phase = at*b.real;
143 if (b.imag != 0.0) {
144 len /= exp(at*b.imag);
145 phase += b.imag*log(vabs);
146 }
147 r.real = len*cos(phase);
148 r.imag = len*sin(phase);
149 }
150 return r;
151}
152
Tim Peters0f336042001-03-18 08:21:57 +0000153static Py_complex
154c_powu(Py_complex x, long n)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000155{
Guido van Rossum926518b1996-08-19 19:30:45 +0000156 Py_complex r, p;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000157 long mask = 1;
Guido van Rossum926518b1996-08-19 19:30:45 +0000158 r = c_1;
159 p = x;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000160 while (mask > 0 && n >= mask) {
161 if (n & mask)
162 r = c_prod(r,p);
163 mask <<= 1;
164 p = c_prod(p,p);
165 }
166 return r;
167}
168
Tim Peters0f336042001-03-18 08:21:57 +0000169static Py_complex
170c_powi(Py_complex x, long n)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000171{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000172 Py_complex cn;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000173
174 if (n > 100 || n < -100) {
175 cn.real = (double) n;
176 cn.imag = 0.;
177 return c_pow(x,cn);
178 }
179 else if (n > 0)
180 return c_powu(x,n);
181 else
182 return c_quot(c_1,c_powu(x,-n));
183
184}
185
Tim Peters6d6c1a32001-08-02 04:15:00 +0000186static PyObject *
187complex_subtype_from_c_complex(PyTypeObject *type, Py_complex cval)
188{
189 PyObject *op;
190
Georg Brandl6b50c632006-06-01 08:27:32 +0000191 op = type->tp_alloc(type, 0);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000192 if (op != NULL)
193 ((PyComplexObject *)op)->cval = cval;
194 return op;
195}
196
Guido van Rossumf9fca921996-01-12 00:47:05 +0000197PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000198PyComplex_FromCComplex(Py_complex cval)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000199{
Guido van Rossumb18618d2000-05-03 23:44:39 +0000200 register PyComplexObject *op;
201
Guido van Rossume3a8e7e2002-08-19 19:26:42 +0000202 /* Inline PyObject_New */
Guido van Rossumb18618d2000-05-03 23:44:39 +0000203 op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject));
Guido van Rossumf9fca921996-01-12 00:47:05 +0000204 if (op == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000205 return PyErr_NoMemory();
Guido van Rossumb18618d2000-05-03 23:44:39 +0000206 PyObject_INIT(op, &PyComplex_Type);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000207 op->cval = cval;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000208 return (PyObject *) op;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000209}
210
Tim Peters6d6c1a32001-08-02 04:15:00 +0000211static PyObject *
212complex_subtype_from_doubles(PyTypeObject *type, double real, double imag)
213{
214 Py_complex c;
215 c.real = real;
216 c.imag = imag;
217 return complex_subtype_from_c_complex(type, c);
218}
219
Guido van Rossumf9fca921996-01-12 00:47:05 +0000220PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000221PyComplex_FromDoubles(double real, double imag)
Guido van Rossum926518b1996-08-19 19:30:45 +0000222{
223 Py_complex c;
224 c.real = real;
225 c.imag = imag;
226 return PyComplex_FromCComplex(c);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000227}
228
229double
Fred Drake4288c802000-07-09 04:36:04 +0000230PyComplex_RealAsDouble(PyObject *op)
Guido van Rossum926518b1996-08-19 19:30:45 +0000231{
232 if (PyComplex_Check(op)) {
233 return ((PyComplexObject *)op)->cval.real;
Fred Drake4288c802000-07-09 04:36:04 +0000234 }
235 else {
Guido van Rossum926518b1996-08-19 19:30:45 +0000236 return PyFloat_AsDouble(op);
237 }
Guido van Rossumf9fca921996-01-12 00:47:05 +0000238}
239
240double
Fred Drake4288c802000-07-09 04:36:04 +0000241PyComplex_ImagAsDouble(PyObject *op)
Guido van Rossum926518b1996-08-19 19:30:45 +0000242{
243 if (PyComplex_Check(op)) {
244 return ((PyComplexObject *)op)->cval.imag;
Fred Drake4288c802000-07-09 04:36:04 +0000245 }
246 else {
Guido van Rossum926518b1996-08-19 19:30:45 +0000247 return 0.0;
248 }
Guido van Rossumf9fca921996-01-12 00:47:05 +0000249}
250
Guido van Rossum9e720e31996-07-21 02:31:35 +0000251Py_complex
Fred Drake4288c802000-07-09 04:36:04 +0000252PyComplex_AsCComplex(PyObject *op)
Guido van Rossum926518b1996-08-19 19:30:45 +0000253{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000254 Py_complex cv;
Georg Brandl2b869942007-03-17 16:08:45 +0000255 PyObject *newop = NULL;
256 static PyObject *complex_str = NULL;
257
258 assert(op);
259 /* If op is already of type PyComplex_Type, return its value */
Guido van Rossumcf3d1081996-01-12 01:21:14 +0000260 if (PyComplex_Check(op)) {
261 return ((PyComplexObject *)op)->cval;
Fred Drake4288c802000-07-09 04:36:04 +0000262 }
Georg Brandl2b869942007-03-17 16:08:45 +0000263 /* If not, use op's __complex__ method, if it exists */
264
265 /* return -1 on failure */
266 cv.real = -1.;
267 cv.imag = 0.;
268
269 if (PyInstance_Check(op)) {
270 /* this can go away in python 3000 */
271 if (PyObject_HasAttrString(op, "__complex__")) {
272 newop = PyObject_CallMethod(op, "__complex__", NULL);
273 if (!newop)
274 return cv;
275 }
276 /* else try __float__ */
277 } else {
278 PyObject *complexfunc;
279 if (!complex_str) {
280 if (!(complex_str = PyString_FromString("__complex__")))
281 return cv;
282 }
283 complexfunc = _PyType_Lookup(op->ob_type, complex_str);
284 /* complexfunc is a borrowed reference */
285 if (complexfunc) {
286 newop = PyObject_CallFunctionObjArgs(complexfunc, op, NULL);
287 if (!newop)
288 return cv;
289 }
290 }
291
292 if (newop) {
293 if (!PyComplex_Check(newop)) {
294 PyErr_SetString(PyExc_TypeError,
295 "__complex__ should return a complex object");
296 Py_DECREF(newop);
297 return cv;
298 }
299 cv = ((PyComplexObject *)newop)->cval;
300 Py_DECREF(newop);
301 return cv;
302 }
303 /* If neither of the above works, interpret op as a float giving the
304 real part of the result, and fill in the imaginary part as 0. */
Fred Drake4288c802000-07-09 04:36:04 +0000305 else {
Georg Brandl2b869942007-03-17 16:08:45 +0000306 /* PyFloat_AsDouble will return -1 on failure */
Guido van Rossumcf3d1081996-01-12 01:21:14 +0000307 cv.real = PyFloat_AsDouble(op);
Guido van Rossumcf3d1081996-01-12 01:21:14 +0000308 return cv;
Tim Peters70695122001-03-11 08:37:29 +0000309 }
Guido van Rossumcf3d1081996-01-12 01:21:14 +0000310}
311
Guido van Rossumf9fca921996-01-12 00:47:05 +0000312static void
Fred Drake4288c802000-07-09 04:36:04 +0000313complex_dealloc(PyObject *op)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000314{
Guido van Rossum9475a232001-10-05 20:51:39 +0000315 op->ob_type->tp_free(op);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000316}
317
318
Guido van Rossum363078a1996-05-24 20:45:01 +0000319static void
Barry Warsaw01d697a2001-11-28 20:50:56 +0000320complex_to_buf(char *buf, int bufsz, PyComplexObject *v, int precision)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000321{
Martin v. Löwis737ea822004-06-08 18:52:54 +0000322 char format[32];
323 if (v->cval.real == 0.) {
Neal Norwitz4b0a3152006-07-16 02:22:30 +0000324 PyOS_snprintf(format, sizeof(format), "%%.%ig", precision);
325 PyOS_ascii_formatd(buf, bufsz - 1, format, v->cval.imag);
326 strncat(buf, "j", 1);
Martin v. Löwis737ea822004-06-08 18:52:54 +0000327 } else {
328 char re[64], im[64];
Georg Brandlc404ff22005-09-16 06:42:26 +0000329 /* Format imaginary part with sign, real part without */
Neal Norwitz4b0a3152006-07-16 02:22:30 +0000330 PyOS_snprintf(format, sizeof(format), "%%.%ig", precision);
331 PyOS_ascii_formatd(re, sizeof(re), format, v->cval.real);
332 PyOS_snprintf(format, sizeof(format), "%%+.%ig", precision);
333 PyOS_ascii_formatd(im, sizeof(im), format, v->cval.imag);
Georg Brandlc404ff22005-09-16 06:42:26 +0000334 PyOS_snprintf(buf, bufsz, "(%s%sj)", re, im);
Martin v. Löwis737ea822004-06-08 18:52:54 +0000335 }
Guido van Rossumf9fca921996-01-12 00:47:05 +0000336}
337
338static int
Fred Drake4288c802000-07-09 04:36:04 +0000339complex_print(PyComplexObject *v, FILE *fp, int flags)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000340{
341 char buf[100];
Barry Warsaw01d697a2001-11-28 20:50:56 +0000342 complex_to_buf(buf, sizeof(buf), v,
Tim Peters70695122001-03-11 08:37:29 +0000343 (flags & Py_PRINT_RAW) ? PREC_STR : PREC_REPR);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000344 fputs(buf, fp);
345 return 0;
346}
347
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000348static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000349complex_repr(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000350{
351 char buf[100];
Barry Warsaw01d697a2001-11-28 20:50:56 +0000352 complex_to_buf(buf, sizeof(buf), v, PREC_REPR);
Tim Peters70695122001-03-11 08:37:29 +0000353 return PyString_FromString(buf);
354}
355
356static PyObject *
357complex_str(PyComplexObject *v)
358{
359 char buf[100];
Barry Warsaw01d697a2001-11-28 20:50:56 +0000360 complex_to_buf(buf, sizeof(buf), v, PREC_STR);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000361 return PyString_FromString(buf);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000362}
363
Guido van Rossumf9fca921996-01-12 00:47:05 +0000364static long
Fred Drake4288c802000-07-09 04:36:04 +0000365complex_hash(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000366{
Tim Peters39dce292000-08-15 03:34:48 +0000367 long hashreal, hashimag, combined;
368 hashreal = _Py_HashDouble(v->cval.real);
369 if (hashreal == -1)
370 return -1;
371 hashimag = _Py_HashDouble(v->cval.imag);
372 if (hashimag == -1)
373 return -1;
374 /* Note: if the imaginary part is 0, hashimag is 0 now,
375 * so the following returns hashreal unchanged. This is
376 * important because numbers of different types that
377 * compare equal must have the same hash value, so that
378 * hash(x + 0*j) must equal hash(x).
379 */
380 combined = hashreal + 1000003 * hashimag;
381 if (combined == -1)
382 combined = -2;
383 return combined;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000384}
385
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000386static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000387complex_add(PyComplexObject *v, PyComplexObject *w)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000388{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000389 Py_complex result;
390 PyFPE_START_PROTECT("complex_add", return 0)
391 result = c_sum(v->cval,w->cval);
Guido van Rossum45b83911997-03-14 04:32:50 +0000392 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000393 return PyComplex_FromCComplex(result);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000394}
395
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000396static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000397complex_sub(PyComplexObject *v, PyComplexObject *w)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000398{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000399 Py_complex result;
400 PyFPE_START_PROTECT("complex_sub", return 0)
401 result = c_diff(v->cval,w->cval);
Guido van Rossum45b83911997-03-14 04:32:50 +0000402 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000403 return PyComplex_FromCComplex(result);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000404}
405
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000406static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000407complex_mul(PyComplexObject *v, PyComplexObject *w)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000408{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000409 Py_complex result;
410 PyFPE_START_PROTECT("complex_mul", return 0)
411 result = c_prod(v->cval,w->cval);
Guido van Rossum45b83911997-03-14 04:32:50 +0000412 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000413 return PyComplex_FromCComplex(result);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000414}
415
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000416static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000417complex_div(PyComplexObject *v, PyComplexObject *w)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000418{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000419 Py_complex quot;
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000420 PyFPE_START_PROTECT("complex_div", return 0)
Guido van Rossum96783941997-05-20 18:21:34 +0000421 errno = 0;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000422 quot = c_quot(v->cval,w->cval);
Guido van Rossum45b83911997-03-14 04:32:50 +0000423 PyFPE_END_PROTECT(quot)
Guido van Rossum96783941997-05-20 18:21:34 +0000424 if (errno == EDOM) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000425 PyErr_SetString(PyExc_ZeroDivisionError, "complex division");
Guido van Rossumf9fca921996-01-12 00:47:05 +0000426 return NULL;
427 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000428 return PyComplex_FromCComplex(quot);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000429}
430
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000431static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000432complex_classic_div(PyComplexObject *v, PyComplexObject *w)
433{
434 Py_complex quot;
435
Guido van Rossum1832de42001-09-04 03:51:09 +0000436 if (Py_DivisionWarningFlag >= 2 &&
Guido van Rossum393661d2001-08-31 17:40:15 +0000437 PyErr_Warn(PyExc_DeprecationWarning,
438 "classic complex division") < 0)
439 return NULL;
440
441 PyFPE_START_PROTECT("complex_classic_div", return 0)
442 errno = 0;
443 quot = c_quot(v->cval,w->cval);
444 PyFPE_END_PROTECT(quot)
445 if (errno == EDOM) {
446 PyErr_SetString(PyExc_ZeroDivisionError, "complex division");
447 return NULL;
448 }
449 return PyComplex_FromCComplex(quot);
450}
451
452static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000453complex_remainder(PyComplexObject *v, PyComplexObject *w)
Guido van Rossumee09fc11996-09-11 13:55:55 +0000454{
Guido van Rossum3be12e91996-09-12 20:56:18 +0000455 Py_complex div, mod;
Guido van Rossum69cf3c72002-04-15 12:39:12 +0000456
457 if (PyErr_Warn(PyExc_DeprecationWarning,
458 "complex divmod(), // and % are deprecated") < 0)
459 return NULL;
460
Guido van Rossum96783941997-05-20 18:21:34 +0000461 errno = 0;
Guido van Rossum3be12e91996-09-12 20:56:18 +0000462 div = c_quot(v->cval,w->cval); /* The raw divisor value. */
Guido van Rossum96783941997-05-20 18:21:34 +0000463 if (errno == EDOM) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000464 PyErr_SetString(PyExc_ZeroDivisionError, "complex remainder");
Guido van Rossum3be12e91996-09-12 20:56:18 +0000465 return NULL;
466 }
467 div.real = floor(div.real); /* Use the floor of the real part. */
468 div.imag = 0.0;
469 mod = c_diff(v->cval, c_prod(w->cval, div));
470
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000471 return PyComplex_FromCComplex(mod);
Guido van Rossumee09fc11996-09-11 13:55:55 +0000472}
473
Guido van Rossumee09fc11996-09-11 13:55:55 +0000474
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000475static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000476complex_divmod(PyComplexObject *v, PyComplexObject *w)
Guido van Rossum3be12e91996-09-12 20:56:18 +0000477{
478 Py_complex div, mod;
479 PyObject *d, *m, *z;
Guido van Rossum9ec4c782002-04-15 01:41:56 +0000480
481 if (PyErr_Warn(PyExc_DeprecationWarning,
Guido van Rossum69cf3c72002-04-15 12:39:12 +0000482 "complex divmod(), // and % are deprecated") < 0)
Guido van Rossum9ec4c782002-04-15 01:41:56 +0000483 return NULL;
484
Guido van Rossum96783941997-05-20 18:21:34 +0000485 errno = 0;
Guido van Rossum3be12e91996-09-12 20:56:18 +0000486 div = c_quot(v->cval,w->cval); /* The raw divisor value. */
Guido van Rossum96783941997-05-20 18:21:34 +0000487 if (errno == EDOM) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000488 PyErr_SetString(PyExc_ZeroDivisionError, "complex divmod()");
Guido van Rossum3be12e91996-09-12 20:56:18 +0000489 return NULL;
490 }
491 div.real = floor(div.real); /* Use the floor of the real part. */
492 div.imag = 0.0;
493 mod = c_diff(v->cval, c_prod(w->cval, div));
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000494 d = PyComplex_FromCComplex(div);
495 m = PyComplex_FromCComplex(mod);
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000496 z = PyTuple_Pack(2, d, m);
Guido van Rossum3be12e91996-09-12 20:56:18 +0000497 Py_XDECREF(d);
498 Py_XDECREF(m);
499 return z;
500}
Guido van Rossumf9fca921996-01-12 00:47:05 +0000501
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000502static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000503complex_pow(PyComplexObject *v, PyObject *w, PyComplexObject *z)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000504{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000505 Py_complex p;
506 Py_complex exponent;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000507 long int_exponent;
508
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000509 if ((PyObject *)z!=Py_None) {
510 PyErr_SetString(PyExc_ValueError, "complex modulo");
Guido van Rossumf9fca921996-01-12 00:47:05 +0000511 return NULL;
512 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000513 PyFPE_START_PROTECT("complex_pow", return 0)
Guido van Rossum96783941997-05-20 18:21:34 +0000514 errno = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000515 exponent = ((PyComplexObject*)w)->cval;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000516 int_exponent = (long)exponent.real;
517 if (exponent.imag == 0. && exponent.real == int_exponent)
518 p = c_powi(v->cval,int_exponent);
519 else
520 p = c_pow(v->cval,exponent);
521
Guido van Rossum45b83911997-03-14 04:32:50 +0000522 PyFPE_END_PROTECT(p)
Tim Petersbab22be2002-03-22 02:48:46 +0000523 Py_ADJUST_ERANGE2(p.real, p.imag);
524 if (errno == EDOM) {
525 PyErr_SetString(PyExc_ZeroDivisionError,
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000526 "0.0 to a negative or complex power");
Guido van Rossumf9fca921996-01-12 00:47:05 +0000527 return NULL;
528 }
Tim Petersbab22be2002-03-22 02:48:46 +0000529 else if (errno == ERANGE) {
530 PyErr_SetString(PyExc_OverflowError,
Neal Norwitz0593de32007-03-09 05:59:01 +0000531 "complex exponentiation");
Tim Petersbab22be2002-03-22 02:48:46 +0000532 return NULL;
533 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000534 return PyComplex_FromCComplex(p);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000535}
536
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000537static PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +0000538complex_int_div(PyComplexObject *v, PyComplexObject *w)
539{
540 PyObject *t, *r;
541
542 t = complex_divmod(v, w);
543 if (t != NULL) {
544 r = PyTuple_GET_ITEM(t, 0);
545 Py_INCREF(r);
546 Py_DECREF(t);
547 return r;
548 }
549 return NULL;
550}
551
552static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000553complex_neg(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000554{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000555 Py_complex neg;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000556 neg.real = -v->cval.real;
557 neg.imag = -v->cval.imag;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000558 return PyComplex_FromCComplex(neg);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000559}
560
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000561static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000562complex_pos(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000563{
Tim Peters2400fa42001-09-12 19:12:49 +0000564 if (PyComplex_CheckExact(v)) {
565 Py_INCREF(v);
566 return (PyObject *)v;
567 }
568 else
569 return PyComplex_FromCComplex(v->cval);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000570}
571
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000572static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000573complex_abs(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000574{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000575 double result;
576 PyFPE_START_PROTECT("complex_abs", return 0)
577 result = hypot(v->cval.real,v->cval.imag);
Guido van Rossum45b83911997-03-14 04:32:50 +0000578 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000579 return PyFloat_FromDouble(result);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000580}
581
582static int
Fred Drake4288c802000-07-09 04:36:04 +0000583complex_nonzero(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000584{
Guido van Rossum3bbef601999-01-25 19:42:19 +0000585 return v->cval.real != 0.0 || v->cval.imag != 0.0;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000586}
587
588static int
Fred Drake4288c802000-07-09 04:36:04 +0000589complex_coerce(PyObject **pv, PyObject **pw)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000590{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000591 Py_complex cval;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000592 cval.imag = 0.;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000593 if (PyInt_Check(*pw)) {
594 cval.real = (double)PyInt_AsLong(*pw);
595 *pw = PyComplex_FromCComplex(cval);
596 Py_INCREF(*pv);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000597 return 0;
598 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000599 else if (PyLong_Check(*pw)) {
600 cval.real = PyLong_AsDouble(*pw);
Tim Peters9fffa3e2001-09-04 05:14:19 +0000601 if (cval.real == -1.0 && PyErr_Occurred())
602 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000603 *pw = PyComplex_FromCComplex(cval);
604 Py_INCREF(*pv);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000605 return 0;
606 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000607 else if (PyFloat_Check(*pw)) {
608 cval.real = PyFloat_AsDouble(*pw);
609 *pw = PyComplex_FromCComplex(cval);
610 Py_INCREF(*pv);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000611 return 0;
612 }
Guido van Rossum63805962001-09-19 01:13:10 +0000613 else if (PyComplex_Check(*pw)) {
614 Py_INCREF(*pv);
615 Py_INCREF(*pw);
616 return 0;
617 }
Guido van Rossumf9fca921996-01-12 00:47:05 +0000618 return 1; /* Can't do it */
619}
620
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000621static PyObject *
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000622complex_richcompare(PyObject *v, PyObject *w, int op)
623{
624 int c;
625 Py_complex i, j;
626 PyObject *res;
627
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000628 c = PyNumber_CoerceEx(&v, &w);
629 if (c < 0)
630 return NULL;
631 if (c > 0) {
632 Py_INCREF(Py_NotImplemented);
633 return Py_NotImplemented;
634 }
Guido van Rossum2ed6bf82001-09-27 20:30:07 +0000635 /* Make sure both arguments are complex. */
636 if (!(PyComplex_Check(v) && PyComplex_Check(w))) {
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000637 Py_DECREF(v);
638 Py_DECREF(w);
639 Py_INCREF(Py_NotImplemented);
640 return Py_NotImplemented;
641 }
642
643 i = ((PyComplexObject *)v)->cval;
644 j = ((PyComplexObject *)w)->cval;
645 Py_DECREF(v);
646 Py_DECREF(w);
647
Guido van Rossum22056422001-09-24 17:52:04 +0000648 if (op != Py_EQ && op != Py_NE) {
649 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger5d01aa42004-12-19 20:45:20 +0000650 "no ordering relation is defined for complex numbers");
Guido van Rossum22056422001-09-24 17:52:04 +0000651 return NULL;
652 }
653
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000654 if ((i.real == j.real && i.imag == j.imag) == (op == Py_EQ))
655 res = Py_True;
656 else
657 res = Py_False;
658
659 Py_INCREF(res);
660 return res;
661}
662
663static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000664complex_int(PyObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000665{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000666 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger0970dba2003-08-30 23:57:36 +0000667 "can't convert complex to int; use int(abs(z))");
Guido van Rossumd4ab3cd1996-09-11 22:54:37 +0000668 return NULL;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000669}
670
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000671static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000672complex_long(PyObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000673{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000674 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger0970dba2003-08-30 23:57:36 +0000675 "can't convert complex to long; use long(abs(z))");
Guido van Rossumd4ab3cd1996-09-11 22:54:37 +0000676 return NULL;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000677}
678
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000679static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000680complex_float(PyObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000681{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000682 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger0970dba2003-08-30 23:57:36 +0000683 "can't convert complex to float; use abs(z)");
Guido van Rossumd4ab3cd1996-09-11 22:54:37 +0000684 return NULL;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000685}
686
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000687static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000688complex_conjugate(PyObject *self)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000689{
Guido van Rossum926518b1996-08-19 19:30:45 +0000690 Py_complex c;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000691 c = ((PyComplexObject *)self)->cval;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000692 c.imag = -c.imag;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000693 return PyComplex_FromCComplex(c);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000694}
695
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000696static PyObject *
697complex_getnewargs(PyComplexObject *v)
698{
Guido van Rossum4eadfa22003-03-02 13:51:47 +0000699 return Py_BuildValue("(D)", &v->cval);
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000700}
701
Guido van Rossumf9fca921996-01-12 00:47:05 +0000702static PyMethodDef complex_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000703 {"conjugate", (PyCFunction)complex_conjugate, METH_NOARGS},
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000704 {"__getnewargs__", (PyCFunction)complex_getnewargs, METH_NOARGS},
Guido van Rossumf9fca921996-01-12 00:47:05 +0000705 {NULL, NULL} /* sentinel */
706};
707
Guido van Rossum6f799372001-09-20 20:46:19 +0000708static PyMemberDef complex_members[] = {
Guido van Rossumfa2e4c22002-02-08 21:26:07 +0000709 {"real", T_DOUBLE, offsetof(PyComplexObject, cval.real), READONLY,
Guido van Rossum6f799372001-09-20 20:46:19 +0000710 "the real part of a complex number"},
Guido van Rossumfa2e4c22002-02-08 21:26:07 +0000711 {"imag", T_DOUBLE, offsetof(PyComplexObject, cval.imag), READONLY,
Guido van Rossum6f799372001-09-20 20:46:19 +0000712 "the imaginary part of a complex number"},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000713 {0},
714};
Guido van Rossumf9fca921996-01-12 00:47:05 +0000715
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000716static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000717complex_subtype_from_string(PyTypeObject *type, PyObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000718{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000719 const char *s, *start;
720 char *end;
721 double x=0.0, y=0.0, z;
Collin Wintere38051d2007-03-09 20:33:07 +0000722 int got_re=0, got_im=0, got_bracket=0, done=0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000723 int digit_or_dot;
724 int sw_error=0;
725 int sign;
726 char buffer[256]; /* For errors */
Guido van Rossum70e36882001-10-25 18:07:22 +0000727#ifdef Py_USING_UNICODE
728 char s_buffer[256];
729#endif
Martin v. Löwis18e16552006-02-15 17:27:45 +0000730 Py_ssize_t len;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000731
732 if (PyString_Check(v)) {
733 s = PyString_AS_STRING(v);
734 len = PyString_GET_SIZE(v);
735 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000736#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +0000737 else if (PyUnicode_Check(v)) {
Skip Montanaro429433b2006-04-18 00:35:43 +0000738 if (PyUnicode_GET_SIZE(v) >= (Py_ssize_t)sizeof(s_buffer)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000739 PyErr_SetString(PyExc_ValueError,
740 "complex() literal too large to convert");
741 return NULL;
742 }
743 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
744 PyUnicode_GET_SIZE(v),
745 s_buffer,
746 NULL))
747 return NULL;
748 s = s_buffer;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000749 len = strlen(s);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000750 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000751#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +0000752 else if (PyObject_AsCharBuffer(v, &s, &len)) {
753 PyErr_SetString(PyExc_TypeError,
754 "complex() arg is not a string");
755 return NULL;
756 }
757
758 /* position on first nonblank */
759 start = s;
760 while (*s && isspace(Py_CHARMASK(*s)))
761 s++;
Collin Wintere38051d2007-03-09 20:33:07 +0000762 if (s[0] == '\0') {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000763 PyErr_SetString(PyExc_ValueError,
764 "complex() arg is an empty string");
765 return NULL;
Collin Wintere38051d2007-03-09 20:33:07 +0000766 }
767 if (s[0] == '(') {
768 /* Skip over possible bracket from repr(). */
769 got_bracket = 1;
770 s++;
771 while (*s && isspace(Py_CHARMASK(*s)))
772 s++;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000773 }
774
775 z = -1.0;
776 sign = 1;
777 do {
778
779 switch (*s) {
780
781 case '\0':
782 if (s-start != len) {
783 PyErr_SetString(
784 PyExc_ValueError,
785 "complex() arg contains a null byte");
786 return NULL;
787 }
788 if(!done) sw_error=1;
789 break;
790
Collin Wintere38051d2007-03-09 20:33:07 +0000791 case ')':
792 if (!got_bracket || !(got_re || got_im)) {
793 sw_error=1;
794 break;
795 }
796 got_bracket=0;
797 done=1;
798 s++;
799 while (*s && isspace(Py_CHARMASK(*s)))
800 s++;
801 if (*s) sw_error=1;
802 break;
803
Tim Peters6d6c1a32001-08-02 04:15:00 +0000804 case '-':
805 sign = -1;
806 /* Fallthrough */
807 case '+':
808 if (done) sw_error=1;
809 s++;
Collin Wintere38051d2007-03-09 20:33:07 +0000810 if ( *s=='\0'||*s=='+'||*s=='-'||*s==')'||
Tim Peters6d6c1a32001-08-02 04:15:00 +0000811 isspace(Py_CHARMASK(*s)) ) sw_error=1;
812 break;
813
814 case 'J':
815 case 'j':
816 if (got_im || done) {
817 sw_error = 1;
818 break;
819 }
820 if (z<0.0) {
821 y=sign;
822 }
823 else{
824 y=sign*z;
825 }
826 got_im=1;
827 s++;
828 if (*s!='+' && *s!='-' )
829 done=1;
830 break;
831
832 default:
833 if (isspace(Py_CHARMASK(*s))) {
834 while (*s && isspace(Py_CHARMASK(*s)))
835 s++;
Collin Wintere38051d2007-03-09 20:33:07 +0000836 if (*s && *s != ')')
Tim Peters6d6c1a32001-08-02 04:15:00 +0000837 sw_error=1;
838 else
839 done = 1;
840 break;
841 }
842 digit_or_dot =
843 (*s=='.' || isdigit(Py_CHARMASK(*s)));
844 if (done||!digit_or_dot) {
845 sw_error=1;
846 break;
847 }
848 errno = 0;
849 PyFPE_START_PROTECT("strtod", return 0)
Martin v. Löwis737ea822004-06-08 18:52:54 +0000850 z = PyOS_ascii_strtod(s, &end) ;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000851 PyFPE_END_PROTECT(z)
852 if (errno != 0) {
Barry Warsaw01d697a2001-11-28 20:50:56 +0000853 PyOS_snprintf(buffer, sizeof(buffer),
Tim Peters6d6c1a32001-08-02 04:15:00 +0000854 "float() out of range: %.150s", s);
855 PyErr_SetString(
856 PyExc_ValueError,
857 buffer);
858 return NULL;
859 }
860 s=end;
861 if (*s=='J' || *s=='j') {
862
863 break;
864 }
865 if (got_re) {
866 sw_error=1;
867 break;
868 }
869
870 /* accept a real part */
871 x=sign*z;
872 got_re=1;
873 if (got_im) done=1;
874 z = -1.0;
875 sign = 1;
876 break;
877
878 } /* end of switch */
879
Tim Peters077f2712002-04-14 22:04:03 +0000880 } while (s - start < len && !sw_error);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000881
Collin Wintere38051d2007-03-09 20:33:07 +0000882 if (sw_error || got_bracket) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000883 PyErr_SetString(PyExc_ValueError,
884 "complex() arg is a malformed string");
885 return NULL;
886 }
887
888 return complex_subtype_from_doubles(type, x, y);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000889}
890
Tim Peters6d6c1a32001-08-02 04:15:00 +0000891static PyObject *
892complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
893{
Raymond Hettinger478d47a2002-06-06 15:45:38 +0000894 PyObject *r, *i, *tmp, *f;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000895 PyNumberMethods *nbr, *nbi = NULL;
896 Py_complex cr, ci;
897 int own_r = 0;
Raymond Hettinger478d47a2002-06-06 15:45:38 +0000898 static PyObject *complexstr;
Martin v. Löwis15e62742006-02-27 16:46:16 +0000899 static char *kwlist[] = {"real", "imag", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000900
901 r = Py_False;
902 i = NULL;
903 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:complex", kwlist,
904 &r, &i))
905 return NULL;
Raymond Hettinger604cd6a2002-08-29 14:22:51 +0000906
Georg Brandl8f032cb2007-03-13 07:57:51 +0000907 /* Special-case for a single argument when type(arg) is complex. */
Guido van Rossum4eadfa22003-03-02 13:51:47 +0000908 if (PyComplex_CheckExact(r) && i == NULL &&
909 type == &PyComplex_Type) {
Raymond Hettinger604cd6a2002-08-29 14:22:51 +0000910 /* Note that we can't know whether it's safe to return
911 a complex *subclass* instance as-is, hence the restriction
Georg Brandl8f032cb2007-03-13 07:57:51 +0000912 to exact complexes here. If either the input or the
913 output is a complex subclass, it will be handled below
914 as a non-orthogonal vector. */
Raymond Hettinger604cd6a2002-08-29 14:22:51 +0000915 Py_INCREF(r);
916 return r;
917 }
Fred Drake526c7a02001-12-13 19:52:22 +0000918 if (PyString_Check(r) || PyUnicode_Check(r)) {
919 if (i != NULL) {
920 PyErr_SetString(PyExc_TypeError,
921 "complex() can't take second arg"
922 " if first is a string");
923 return NULL;
924 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000925 return complex_subtype_from_string(type, r);
Fred Drake526c7a02001-12-13 19:52:22 +0000926 }
927 if (i != NULL && (PyString_Check(i) || PyUnicode_Check(i))) {
928 PyErr_SetString(PyExc_TypeError,
929 "complex() second arg can't be a string");
930 return NULL;
931 }
Tim Peters2400fa42001-09-12 19:12:49 +0000932
Raymond Hettinger478d47a2002-06-06 15:45:38 +0000933 /* XXX Hack to support classes with __complex__ method */
934 if (complexstr == NULL) {
935 complexstr = PyString_InternFromString("__complex__");
936 if (complexstr == NULL)
937 return NULL;
938 }
939 f = PyObject_GetAttr(r, complexstr);
940 if (f == NULL)
941 PyErr_Clear();
942 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000943 PyObject *args = PyTuple_New(0);
Raymond Hettinger478d47a2002-06-06 15:45:38 +0000944 if (args == NULL)
945 return NULL;
946 r = PyEval_CallObject(f, args);
947 Py_DECREF(args);
948 Py_DECREF(f);
949 if (r == NULL)
950 return NULL;
951 own_r = 1;
952 }
Tim Peters2400fa42001-09-12 19:12:49 +0000953 nbr = r->ob_type->tp_as_number;
954 if (i != NULL)
955 nbi = i->ob_type->tp_as_number;
956 if (nbr == NULL || nbr->nb_float == NULL ||
957 ((i != NULL) && (nbi == NULL || nbi->nb_float == NULL))) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000958 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger478d47a2002-06-06 15:45:38 +0000959 "complex() argument must be a string or a number");
Tim Peters465fa3d2003-08-15 01:16:37 +0000960 if (own_r) {
961 Py_DECREF(r);
962 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000963 return NULL;
964 }
Georg Brandl8f032cb2007-03-13 07:57:51 +0000965
966 /* If we get this far, then the "real" and "imag" parts should
967 both be treated as numbers, and the constructor should return a
968 complex number equal to (real + imag*1j).
969
970 Note that we do NOT assume the input to already be in canonical
971 form; the "real" and "imag" parts might themselves be complex
972 numbers, which slightly complicates the code below. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000973 if (PyComplex_Check(r)) {
Tim Peters2400fa42001-09-12 19:12:49 +0000974 /* Note that if r is of a complex subtype, we're only
975 retaining its real & imag parts here, and the return
976 value is (properly) of the builtin complex type. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000977 cr = ((PyComplexObject*)r)->cval;
978 if (own_r) {
979 Py_DECREF(r);
980 }
981 }
982 else {
Georg Brandl8f032cb2007-03-13 07:57:51 +0000983 /* The "real" part really is entirely real, and contributes
984 nothing in the imaginary direction.
985 Just treat it as a double. */
986 cr.imag = 0.0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000987 tmp = PyNumber_Float(r);
988 if (own_r) {
Georg Brandl8f032cb2007-03-13 07:57:51 +0000989 /* r was a newly created complex number, rather
990 than the original "real" argument. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000991 Py_DECREF(r);
992 }
993 if (tmp == NULL)
994 return NULL;
995 if (!PyFloat_Check(tmp)) {
996 PyErr_SetString(PyExc_TypeError,
997 "float(r) didn't return a float");
998 Py_DECREF(tmp);
999 return NULL;
1000 }
1001 cr.real = PyFloat_AsDouble(tmp);
1002 Py_DECREF(tmp);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001003 }
1004 if (i == NULL) {
1005 ci.real = 0.0;
1006 ci.imag = 0.0;
1007 }
1008 else if (PyComplex_Check(i))
1009 ci = ((PyComplexObject*)i)->cval;
1010 else {
Georg Brandl8f032cb2007-03-13 07:57:51 +00001011 /* The "imag" part really is entirely imaginary, and
1012 contributes nothing in the real direction.
1013 Just treat it as a double. */
1014 ci.imag = 0.0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001015 tmp = (*nbi->nb_float)(i);
1016 if (tmp == NULL)
1017 return NULL;
1018 ci.real = PyFloat_AsDouble(tmp);
1019 Py_DECREF(tmp);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001020 }
Georg Brandl8f032cb2007-03-13 07:57:51 +00001021 /* If the input was in canonical form, then the "real" and "imag"
1022 parts are real numbers, so that ci.real and cr.imag are zero.
1023 We need this correction in case they were not real numbers. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001024 cr.real -= ci.imag;
1025 cr.imag += ci.real;
1026 return complex_subtype_from_c_complex(type, cr);
1027}
1028
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001029PyDoc_STRVAR(complex_doc,
Tim Peters2400fa42001-09-12 19:12:49 +00001030"complex(real[, imag]) -> complex number\n"
1031"\n"
1032"Create a complex number from a real part and an optional imaginary part.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001033"This is equivalent to (real + imag*1j) where imag defaults to 0.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001034
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001035static PyNumberMethods complex_as_number = {
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001036 (binaryfunc)complex_add, /* nb_add */
1037 (binaryfunc)complex_sub, /* nb_subtract */
1038 (binaryfunc)complex_mul, /* nb_multiply */
Guido van Rossum393661d2001-08-31 17:40:15 +00001039 (binaryfunc)complex_classic_div, /* nb_divide */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001040 (binaryfunc)complex_remainder, /* nb_remainder */
1041 (binaryfunc)complex_divmod, /* nb_divmod */
1042 (ternaryfunc)complex_pow, /* nb_power */
1043 (unaryfunc)complex_neg, /* nb_negative */
1044 (unaryfunc)complex_pos, /* nb_positive */
1045 (unaryfunc)complex_abs, /* nb_absolute */
1046 (inquiry)complex_nonzero, /* nb_nonzero */
1047 0, /* nb_invert */
1048 0, /* nb_lshift */
1049 0, /* nb_rshift */
1050 0, /* nb_and */
1051 0, /* nb_xor */
1052 0, /* nb_or */
Georg Brandl347b3002006-03-30 11:57:00 +00001053 complex_coerce, /* nb_coerce */
1054 complex_int, /* nb_int */
1055 complex_long, /* nb_long */
1056 complex_float, /* nb_float */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001057 0, /* nb_oct */
1058 0, /* nb_hex */
Guido van Rossum4668b002001-08-08 05:00:18 +00001059 0, /* nb_inplace_add */
1060 0, /* nb_inplace_subtract */
1061 0, /* nb_inplace_multiply*/
1062 0, /* nb_inplace_divide */
1063 0, /* nb_inplace_remainder */
1064 0, /* nb_inplace_power */
1065 0, /* nb_inplace_lshift */
1066 0, /* nb_inplace_rshift */
1067 0, /* nb_inplace_and */
1068 0, /* nb_inplace_xor */
1069 0, /* nb_inplace_or */
1070 (binaryfunc)complex_int_div, /* nb_floor_divide */
1071 (binaryfunc)complex_div, /* nb_true_divide */
1072 0, /* nb_inplace_floor_divide */
1073 0, /* nb_inplace_true_divide */
Guido van Rossumf9fca921996-01-12 00:47:05 +00001074};
1075
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001076PyTypeObject PyComplex_Type = {
1077 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossumf9fca921996-01-12 00:47:05 +00001078 0,
1079 "complex",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001080 sizeof(PyComplexObject),
Guido van Rossumf9fca921996-01-12 00:47:05 +00001081 0,
Georg Brandl347b3002006-03-30 11:57:00 +00001082 complex_dealloc, /* tp_dealloc */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001083 (printfunc)complex_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001084 0, /* tp_getattr */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001085 0, /* tp_setattr */
1086 0, /* tp_compare */
1087 (reprfunc)complex_repr, /* tp_repr */
1088 &complex_as_number, /* tp_as_number */
1089 0, /* tp_as_sequence */
1090 0, /* tp_as_mapping */
1091 (hashfunc)complex_hash, /* tp_hash */
1092 0, /* tp_call */
Tim Peters70695122001-03-11 08:37:29 +00001093 (reprfunc)complex_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001094 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001095 0, /* tp_setattro */
1096 0, /* tp_as_buffer */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001097 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1098 complex_doc, /* tp_doc */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001099 0, /* tp_traverse */
1100 0, /* tp_clear */
1101 complex_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001102 0, /* tp_weaklistoffset */
1103 0, /* tp_iter */
1104 0, /* tp_iternext */
1105 complex_methods, /* tp_methods */
1106 complex_members, /* tp_members */
1107 0, /* tp_getset */
1108 0, /* tp_base */
1109 0, /* tp_dict */
1110 0, /* tp_descr_get */
1111 0, /* tp_descr_set */
1112 0, /* tp_dictoffset */
1113 0, /* tp_init */
Georg Brandl6b50c632006-06-01 08:27:32 +00001114 PyType_GenericAlloc, /* tp_alloc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001115 complex_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00001116 PyObject_Del, /* tp_free */
Guido van Rossumf9fca921996-01-12 00:47:05 +00001117};
1118
1119#endif