blob: 4023fa045e4160a9ce4e3ca4dbae9471ec348d5e [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
191 op = PyType_GenericAlloc(type, 0);
192 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;
Guido van Rossumcf3d1081996-01-12 01:21:14 +0000255 if (PyComplex_Check(op)) {
256 return ((PyComplexObject *)op)->cval;
Fred Drake4288c802000-07-09 04:36:04 +0000257 }
258 else {
Guido van Rossumcf3d1081996-01-12 01:21:14 +0000259 cv.real = PyFloat_AsDouble(op);
260 cv.imag = 0.;
261 return cv;
Tim Peters70695122001-03-11 08:37:29 +0000262 }
Guido van Rossumcf3d1081996-01-12 01:21:14 +0000263}
264
Guido van Rossumf9fca921996-01-12 00:47:05 +0000265static void
Fred Drake4288c802000-07-09 04:36:04 +0000266complex_dealloc(PyObject *op)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000267{
Guido van Rossum9475a232001-10-05 20:51:39 +0000268 op->ob_type->tp_free(op);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000269}
270
271
Guido van Rossum363078a1996-05-24 20:45:01 +0000272static void
Barry Warsaw01d697a2001-11-28 20:50:56 +0000273complex_to_buf(char *buf, int bufsz, PyComplexObject *v, int precision)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000274{
Martin v. Löwis737ea822004-06-08 18:52:54 +0000275 char format[32];
276 if (v->cval.real == 0.) {
277 PyOS_snprintf(format, 32, "%%.%ig", precision);
278 PyOS_ascii_formatd(buf, bufsz, format, v->cval.imag);
279 strncat(buf, "j", bufsz);
280 } else {
281 char re[64], im[64];
282
283 PyOS_snprintf(format, 32, "%%.%ig", precision);
284 PyOS_ascii_formatd(re, 64, format, v->cval.real);
285 PyOS_ascii_formatd(im, 64, format, v->cval.imag);
286 PyOS_snprintf(buf, bufsz, "(%s+%sj)", re, im);
287 }
Guido van Rossumf9fca921996-01-12 00:47:05 +0000288}
289
290static int
Fred Drake4288c802000-07-09 04:36:04 +0000291complex_print(PyComplexObject *v, FILE *fp, int flags)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000292{
293 char buf[100];
Barry Warsaw01d697a2001-11-28 20:50:56 +0000294 complex_to_buf(buf, sizeof(buf), v,
Tim Peters70695122001-03-11 08:37:29 +0000295 (flags & Py_PRINT_RAW) ? PREC_STR : PREC_REPR);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000296 fputs(buf, fp);
297 return 0;
298}
299
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000300static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000301complex_repr(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000302{
303 char buf[100];
Barry Warsaw01d697a2001-11-28 20:50:56 +0000304 complex_to_buf(buf, sizeof(buf), v, PREC_REPR);
Tim Peters70695122001-03-11 08:37:29 +0000305 return PyString_FromString(buf);
306}
307
308static PyObject *
309complex_str(PyComplexObject *v)
310{
311 char buf[100];
Barry Warsaw01d697a2001-11-28 20:50:56 +0000312 complex_to_buf(buf, sizeof(buf), v, PREC_STR);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000313 return PyString_FromString(buf);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000314}
315
Guido van Rossumf9fca921996-01-12 00:47:05 +0000316static long
Fred Drake4288c802000-07-09 04:36:04 +0000317complex_hash(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000318{
Tim Peters39dce292000-08-15 03:34:48 +0000319 long hashreal, hashimag, combined;
320 hashreal = _Py_HashDouble(v->cval.real);
321 if (hashreal == -1)
322 return -1;
323 hashimag = _Py_HashDouble(v->cval.imag);
324 if (hashimag == -1)
325 return -1;
326 /* Note: if the imaginary part is 0, hashimag is 0 now,
327 * so the following returns hashreal unchanged. This is
328 * important because numbers of different types that
329 * compare equal must have the same hash value, so that
330 * hash(x + 0*j) must equal hash(x).
331 */
332 combined = hashreal + 1000003 * hashimag;
333 if (combined == -1)
334 combined = -2;
335 return combined;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000336}
337
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000338static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000339complex_add(PyComplexObject *v, PyComplexObject *w)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000340{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000341 Py_complex result;
342 PyFPE_START_PROTECT("complex_add", return 0)
343 result = c_sum(v->cval,w->cval);
Guido van Rossum45b83911997-03-14 04:32:50 +0000344 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000345 return PyComplex_FromCComplex(result);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000346}
347
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000348static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000349complex_sub(PyComplexObject *v, PyComplexObject *w)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000350{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000351 Py_complex result;
352 PyFPE_START_PROTECT("complex_sub", return 0)
353 result = c_diff(v->cval,w->cval);
Guido van Rossum45b83911997-03-14 04:32:50 +0000354 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000355 return PyComplex_FromCComplex(result);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000356}
357
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000358static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000359complex_mul(PyComplexObject *v, PyComplexObject *w)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000360{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000361 Py_complex result;
362 PyFPE_START_PROTECT("complex_mul", return 0)
363 result = c_prod(v->cval,w->cval);
Guido van Rossum45b83911997-03-14 04:32:50 +0000364 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000365 return PyComplex_FromCComplex(result);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000366}
367
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000368static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000369complex_div(PyComplexObject *v, PyComplexObject *w)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000370{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000371 Py_complex quot;
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000372 PyFPE_START_PROTECT("complex_div", return 0)
Guido van Rossum96783941997-05-20 18:21:34 +0000373 errno = 0;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000374 quot = c_quot(v->cval,w->cval);
Guido van Rossum45b83911997-03-14 04:32:50 +0000375 PyFPE_END_PROTECT(quot)
Guido van Rossum96783941997-05-20 18:21:34 +0000376 if (errno == EDOM) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000377 PyErr_SetString(PyExc_ZeroDivisionError, "complex division");
Guido van Rossumf9fca921996-01-12 00:47:05 +0000378 return NULL;
379 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000380 return PyComplex_FromCComplex(quot);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000381}
382
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000383static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000384complex_classic_div(PyComplexObject *v, PyComplexObject *w)
385{
386 Py_complex quot;
387
Guido van Rossum1832de42001-09-04 03:51:09 +0000388 if (Py_DivisionWarningFlag >= 2 &&
Guido van Rossum393661d2001-08-31 17:40:15 +0000389 PyErr_Warn(PyExc_DeprecationWarning,
390 "classic complex division") < 0)
391 return NULL;
392
393 PyFPE_START_PROTECT("complex_classic_div", return 0)
394 errno = 0;
395 quot = c_quot(v->cval,w->cval);
396 PyFPE_END_PROTECT(quot)
397 if (errno == EDOM) {
398 PyErr_SetString(PyExc_ZeroDivisionError, "complex division");
399 return NULL;
400 }
401 return PyComplex_FromCComplex(quot);
402}
403
404static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000405complex_remainder(PyComplexObject *v, PyComplexObject *w)
Guido van Rossumee09fc11996-09-11 13:55:55 +0000406{
Guido van Rossum3be12e91996-09-12 20:56:18 +0000407 Py_complex div, mod;
Guido van Rossum69cf3c72002-04-15 12:39:12 +0000408
409 if (PyErr_Warn(PyExc_DeprecationWarning,
410 "complex divmod(), // and % are deprecated") < 0)
411 return NULL;
412
Guido van Rossum96783941997-05-20 18:21:34 +0000413 errno = 0;
Guido van Rossum3be12e91996-09-12 20:56:18 +0000414 div = c_quot(v->cval,w->cval); /* The raw divisor value. */
Guido van Rossum96783941997-05-20 18:21:34 +0000415 if (errno == EDOM) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000416 PyErr_SetString(PyExc_ZeroDivisionError, "complex remainder");
Guido van Rossum3be12e91996-09-12 20:56:18 +0000417 return NULL;
418 }
419 div.real = floor(div.real); /* Use the floor of the real part. */
420 div.imag = 0.0;
421 mod = c_diff(v->cval, c_prod(w->cval, div));
422
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000423 return PyComplex_FromCComplex(mod);
Guido van Rossumee09fc11996-09-11 13:55:55 +0000424}
425
Guido van Rossumee09fc11996-09-11 13:55:55 +0000426
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000427static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000428complex_divmod(PyComplexObject *v, PyComplexObject *w)
Guido van Rossum3be12e91996-09-12 20:56:18 +0000429{
430 Py_complex div, mod;
431 PyObject *d, *m, *z;
Guido van Rossum9ec4c782002-04-15 01:41:56 +0000432
433 if (PyErr_Warn(PyExc_DeprecationWarning,
Guido van Rossum69cf3c72002-04-15 12:39:12 +0000434 "complex divmod(), // and % are deprecated") < 0)
Guido van Rossum9ec4c782002-04-15 01:41:56 +0000435 return NULL;
436
Guido van Rossum96783941997-05-20 18:21:34 +0000437 errno = 0;
Guido van Rossum3be12e91996-09-12 20:56:18 +0000438 div = c_quot(v->cval,w->cval); /* The raw divisor value. */
Guido van Rossum96783941997-05-20 18:21:34 +0000439 if (errno == EDOM) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000440 PyErr_SetString(PyExc_ZeroDivisionError, "complex divmod()");
Guido van Rossum3be12e91996-09-12 20:56:18 +0000441 return NULL;
442 }
443 div.real = floor(div.real); /* Use the floor of the real part. */
444 div.imag = 0.0;
445 mod = c_diff(v->cval, c_prod(w->cval, div));
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000446 d = PyComplex_FromCComplex(div);
447 m = PyComplex_FromCComplex(mod);
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000448 z = PyTuple_Pack(2, d, m);
Guido van Rossum3be12e91996-09-12 20:56:18 +0000449 Py_XDECREF(d);
450 Py_XDECREF(m);
451 return z;
452}
Guido van Rossumf9fca921996-01-12 00:47:05 +0000453
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000454static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000455complex_pow(PyComplexObject *v, PyObject *w, PyComplexObject *z)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000456{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000457 Py_complex p;
458 Py_complex exponent;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000459 long int_exponent;
460
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000461 if ((PyObject *)z!=Py_None) {
462 PyErr_SetString(PyExc_ValueError, "complex modulo");
Guido van Rossumf9fca921996-01-12 00:47:05 +0000463 return NULL;
464 }
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000465 PyFPE_START_PROTECT("complex_pow", return 0)
Guido van Rossum96783941997-05-20 18:21:34 +0000466 errno = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000467 exponent = ((PyComplexObject*)w)->cval;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000468 int_exponent = (long)exponent.real;
469 if (exponent.imag == 0. && exponent.real == int_exponent)
470 p = c_powi(v->cval,int_exponent);
471 else
472 p = c_pow(v->cval,exponent);
473
Guido van Rossum45b83911997-03-14 04:32:50 +0000474 PyFPE_END_PROTECT(p)
Tim Petersbab22be2002-03-22 02:48:46 +0000475 Py_ADJUST_ERANGE2(p.real, p.imag);
476 if (errno == EDOM) {
477 PyErr_SetString(PyExc_ZeroDivisionError,
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000478 "0.0 to a negative or complex power");
Guido van Rossumf9fca921996-01-12 00:47:05 +0000479 return NULL;
480 }
Tim Petersbab22be2002-03-22 02:48:46 +0000481 else if (errno == ERANGE) {
482 PyErr_SetString(PyExc_OverflowError,
483 "complex exponentiaion");
484 return NULL;
485 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000486 return PyComplex_FromCComplex(p);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000487}
488
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000489static PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +0000490complex_int_div(PyComplexObject *v, PyComplexObject *w)
491{
492 PyObject *t, *r;
493
494 t = complex_divmod(v, w);
495 if (t != NULL) {
496 r = PyTuple_GET_ITEM(t, 0);
497 Py_INCREF(r);
498 Py_DECREF(t);
499 return r;
500 }
501 return NULL;
502}
503
504static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000505complex_neg(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000506{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000507 Py_complex neg;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000508 neg.real = -v->cval.real;
509 neg.imag = -v->cval.imag;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000510 return PyComplex_FromCComplex(neg);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000511}
512
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000513static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000514complex_pos(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000515{
Tim Peters2400fa42001-09-12 19:12:49 +0000516 if (PyComplex_CheckExact(v)) {
517 Py_INCREF(v);
518 return (PyObject *)v;
519 }
520 else
521 return PyComplex_FromCComplex(v->cval);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000522}
523
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000524static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000525complex_abs(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000526{
Guido van Rossum09e6ad01997-02-14 22:54:21 +0000527 double result;
528 PyFPE_START_PROTECT("complex_abs", return 0)
529 result = hypot(v->cval.real,v->cval.imag);
Guido van Rossum45b83911997-03-14 04:32:50 +0000530 PyFPE_END_PROTECT(result)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000531 return PyFloat_FromDouble(result);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000532}
533
534static int
Fred Drake4288c802000-07-09 04:36:04 +0000535complex_nonzero(PyComplexObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000536{
Guido van Rossum3bbef601999-01-25 19:42:19 +0000537 return v->cval.real != 0.0 || v->cval.imag != 0.0;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000538}
539
540static int
Fred Drake4288c802000-07-09 04:36:04 +0000541complex_coerce(PyObject **pv, PyObject **pw)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000542{
Guido van Rossum9e720e31996-07-21 02:31:35 +0000543 Py_complex cval;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000544 cval.imag = 0.;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000545 if (PyInt_Check(*pw)) {
546 cval.real = (double)PyInt_AsLong(*pw);
547 *pw = PyComplex_FromCComplex(cval);
548 Py_INCREF(*pv);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000549 return 0;
550 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000551 else if (PyLong_Check(*pw)) {
552 cval.real = PyLong_AsDouble(*pw);
Tim Peters9fffa3e2001-09-04 05:14:19 +0000553 if (cval.real == -1.0 && PyErr_Occurred())
554 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000555 *pw = PyComplex_FromCComplex(cval);
556 Py_INCREF(*pv);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000557 return 0;
558 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000559 else if (PyFloat_Check(*pw)) {
560 cval.real = PyFloat_AsDouble(*pw);
561 *pw = PyComplex_FromCComplex(cval);
562 Py_INCREF(*pv);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000563 return 0;
564 }
Guido van Rossum63805962001-09-19 01:13:10 +0000565 else if (PyComplex_Check(*pw)) {
566 Py_INCREF(*pv);
567 Py_INCREF(*pw);
568 return 0;
569 }
Guido van Rossumf9fca921996-01-12 00:47:05 +0000570 return 1; /* Can't do it */
571}
572
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000573static PyObject *
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000574complex_richcompare(PyObject *v, PyObject *w, int op)
575{
576 int c;
577 Py_complex i, j;
578 PyObject *res;
579
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000580 c = PyNumber_CoerceEx(&v, &w);
581 if (c < 0)
582 return NULL;
583 if (c > 0) {
584 Py_INCREF(Py_NotImplemented);
585 return Py_NotImplemented;
586 }
Guido van Rossum2ed6bf82001-09-27 20:30:07 +0000587 /* Make sure both arguments are complex. */
588 if (!(PyComplex_Check(v) && PyComplex_Check(w))) {
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000589 Py_DECREF(v);
590 Py_DECREF(w);
591 Py_INCREF(Py_NotImplemented);
592 return Py_NotImplemented;
593 }
594
595 i = ((PyComplexObject *)v)->cval;
596 j = ((PyComplexObject *)w)->cval;
597 Py_DECREF(v);
598 Py_DECREF(w);
599
Guido van Rossum22056422001-09-24 17:52:04 +0000600 if (op != Py_EQ && op != Py_NE) {
601 PyErr_SetString(PyExc_TypeError,
602 "cannot compare complex numbers using <, <=, >, >=");
603 return NULL;
604 }
605
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000606 if ((i.real == j.real && i.imag == j.imag) == (op == Py_EQ))
607 res = Py_True;
608 else
609 res = Py_False;
610
611 Py_INCREF(res);
612 return res;
613}
614
615static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000616complex_int(PyObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000617{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000618 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger0970dba2003-08-30 23:57:36 +0000619 "can't convert complex to int; use int(abs(z))");
Guido van Rossumd4ab3cd1996-09-11 22:54:37 +0000620 return NULL;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000621}
622
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000623static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000624complex_long(PyObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000625{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000626 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger0970dba2003-08-30 23:57:36 +0000627 "can't convert complex to long; use long(abs(z))");
Guido van Rossumd4ab3cd1996-09-11 22:54:37 +0000628 return NULL;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000629}
630
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000631static PyObject *
Fred Drake4288c802000-07-09 04:36:04 +0000632complex_float(PyObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000633{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000634 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger0970dba2003-08-30 23:57:36 +0000635 "can't convert complex to float; use abs(z)");
Guido van Rossumd4ab3cd1996-09-11 22:54:37 +0000636 return NULL;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000637}
638
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000639static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000640complex_conjugate(PyObject *self)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000641{
Guido van Rossum926518b1996-08-19 19:30:45 +0000642 Py_complex c;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000643 c = ((PyComplexObject *)self)->cval;
Guido van Rossumf9fca921996-01-12 00:47:05 +0000644 c.imag = -c.imag;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000645 return PyComplex_FromCComplex(c);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000646}
647
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000648static PyObject *
649complex_getnewargs(PyComplexObject *v)
650{
Guido van Rossum4eadfa22003-03-02 13:51:47 +0000651 return Py_BuildValue("(D)", &v->cval);
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000652}
653
Guido van Rossumf9fca921996-01-12 00:47:05 +0000654static PyMethodDef complex_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000655 {"conjugate", (PyCFunction)complex_conjugate, METH_NOARGS},
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000656 {"__getnewargs__", (PyCFunction)complex_getnewargs, METH_NOARGS},
Guido van Rossumf9fca921996-01-12 00:47:05 +0000657 {NULL, NULL} /* sentinel */
658};
659
Guido van Rossum6f799372001-09-20 20:46:19 +0000660static PyMemberDef complex_members[] = {
Guido van Rossumfa2e4c22002-02-08 21:26:07 +0000661 {"real", T_DOUBLE, offsetof(PyComplexObject, cval.real), READONLY,
Guido van Rossum6f799372001-09-20 20:46:19 +0000662 "the real part of a complex number"},
Guido van Rossumfa2e4c22002-02-08 21:26:07 +0000663 {"imag", T_DOUBLE, offsetof(PyComplexObject, cval.imag), READONLY,
Guido van Rossum6f799372001-09-20 20:46:19 +0000664 "the imaginary part of a complex number"},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000665 {0},
666};
Guido van Rossumf9fca921996-01-12 00:47:05 +0000667
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000668static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000669complex_subtype_from_string(PyTypeObject *type, PyObject *v)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000670{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000671 const char *s, *start;
672 char *end;
673 double x=0.0, y=0.0, z;
674 int got_re=0, got_im=0, done=0;
675 int digit_or_dot;
676 int sw_error=0;
677 int sign;
678 char buffer[256]; /* For errors */
Guido van Rossum70e36882001-10-25 18:07:22 +0000679#ifdef Py_USING_UNICODE
680 char s_buffer[256];
681#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +0000682 int len;
683
684 if (PyString_Check(v)) {
685 s = PyString_AS_STRING(v);
686 len = PyString_GET_SIZE(v);
687 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000688#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +0000689 else if (PyUnicode_Check(v)) {
690 if (PyUnicode_GET_SIZE(v) >= sizeof(s_buffer)) {
691 PyErr_SetString(PyExc_ValueError,
692 "complex() literal too large to convert");
693 return NULL;
694 }
695 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
696 PyUnicode_GET_SIZE(v),
697 s_buffer,
698 NULL))
699 return NULL;
700 s = s_buffer;
701 len = (int)strlen(s);
702 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000703#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +0000704 else if (PyObject_AsCharBuffer(v, &s, &len)) {
705 PyErr_SetString(PyExc_TypeError,
706 "complex() arg is not a string");
707 return NULL;
708 }
709
710 /* position on first nonblank */
711 start = s;
712 while (*s && isspace(Py_CHARMASK(*s)))
713 s++;
714 if (s[0] == '\0') {
715 PyErr_SetString(PyExc_ValueError,
716 "complex() arg is an empty string");
717 return NULL;
718 }
719
720 z = -1.0;
721 sign = 1;
722 do {
723
724 switch (*s) {
725
726 case '\0':
727 if (s-start != len) {
728 PyErr_SetString(
729 PyExc_ValueError,
730 "complex() arg contains a null byte");
731 return NULL;
732 }
733 if(!done) sw_error=1;
734 break;
735
736 case '-':
737 sign = -1;
738 /* Fallthrough */
739 case '+':
740 if (done) sw_error=1;
741 s++;
742 if ( *s=='\0'||*s=='+'||*s=='-' ||
743 isspace(Py_CHARMASK(*s)) ) sw_error=1;
744 break;
745
746 case 'J':
747 case 'j':
748 if (got_im || done) {
749 sw_error = 1;
750 break;
751 }
752 if (z<0.0) {
753 y=sign;
754 }
755 else{
756 y=sign*z;
757 }
758 got_im=1;
759 s++;
760 if (*s!='+' && *s!='-' )
761 done=1;
762 break;
763
764 default:
765 if (isspace(Py_CHARMASK(*s))) {
766 while (*s && isspace(Py_CHARMASK(*s)))
767 s++;
768 if (s[0] != '\0')
769 sw_error=1;
770 else
771 done = 1;
772 break;
773 }
774 digit_or_dot =
775 (*s=='.' || isdigit(Py_CHARMASK(*s)));
776 if (done||!digit_or_dot) {
777 sw_error=1;
778 break;
779 }
780 errno = 0;
781 PyFPE_START_PROTECT("strtod", return 0)
Martin v. Löwis737ea822004-06-08 18:52:54 +0000782 z = PyOS_ascii_strtod(s, &end) ;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000783 PyFPE_END_PROTECT(z)
784 if (errno != 0) {
Barry Warsaw01d697a2001-11-28 20:50:56 +0000785 PyOS_snprintf(buffer, sizeof(buffer),
Tim Peters6d6c1a32001-08-02 04:15:00 +0000786 "float() out of range: %.150s", s);
787 PyErr_SetString(
788 PyExc_ValueError,
789 buffer);
790 return NULL;
791 }
792 s=end;
793 if (*s=='J' || *s=='j') {
794
795 break;
796 }
797 if (got_re) {
798 sw_error=1;
799 break;
800 }
801
802 /* accept a real part */
803 x=sign*z;
804 got_re=1;
805 if (got_im) done=1;
806 z = -1.0;
807 sign = 1;
808 break;
809
810 } /* end of switch */
811
Tim Peters077f2712002-04-14 22:04:03 +0000812 } while (s - start < len && !sw_error);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000813
814 if (sw_error) {
815 PyErr_SetString(PyExc_ValueError,
816 "complex() arg is a malformed string");
817 return NULL;
818 }
819
820 return complex_subtype_from_doubles(type, x, y);
Guido van Rossumf9fca921996-01-12 00:47:05 +0000821}
822
Tim Peters6d6c1a32001-08-02 04:15:00 +0000823static PyObject *
824complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
825{
Raymond Hettinger478d47a2002-06-06 15:45:38 +0000826 PyObject *r, *i, *tmp, *f;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000827 PyNumberMethods *nbr, *nbi = NULL;
828 Py_complex cr, ci;
829 int own_r = 0;
Raymond Hettinger478d47a2002-06-06 15:45:38 +0000830 static PyObject *complexstr;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000831 static char *kwlist[] = {"real", "imag", 0};
832
833 r = Py_False;
834 i = NULL;
835 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:complex", kwlist,
836 &r, &i))
837 return NULL;
Raymond Hettinger604cd6a2002-08-29 14:22:51 +0000838
Walter Dörwald5ecd6c42003-06-17 20:22:24 +0000839 /* Special-case for single argument that is already complex */
Guido van Rossum4eadfa22003-03-02 13:51:47 +0000840 if (PyComplex_CheckExact(r) && i == NULL &&
841 type == &PyComplex_Type) {
Raymond Hettinger604cd6a2002-08-29 14:22:51 +0000842 /* Note that we can't know whether it's safe to return
843 a complex *subclass* instance as-is, hence the restriction
844 to exact complexes here. */
845 Py_INCREF(r);
846 return r;
847 }
Fred Drake526c7a02001-12-13 19:52:22 +0000848 if (PyString_Check(r) || PyUnicode_Check(r)) {
849 if (i != NULL) {
850 PyErr_SetString(PyExc_TypeError,
851 "complex() can't take second arg"
852 " if first is a string");
853 return NULL;
854 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000855 return complex_subtype_from_string(type, r);
Fred Drake526c7a02001-12-13 19:52:22 +0000856 }
857 if (i != NULL && (PyString_Check(i) || PyUnicode_Check(i))) {
858 PyErr_SetString(PyExc_TypeError,
859 "complex() second arg can't be a string");
860 return NULL;
861 }
Tim Peters2400fa42001-09-12 19:12:49 +0000862
Raymond Hettinger478d47a2002-06-06 15:45:38 +0000863 /* XXX Hack to support classes with __complex__ method */
864 if (complexstr == NULL) {
865 complexstr = PyString_InternFromString("__complex__");
866 if (complexstr == NULL)
867 return NULL;
868 }
869 f = PyObject_GetAttr(r, complexstr);
870 if (f == NULL)
871 PyErr_Clear();
872 else {
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000873 PyObject *args = PyTuple_New(0);
Raymond Hettinger478d47a2002-06-06 15:45:38 +0000874 if (args == NULL)
875 return NULL;
876 r = PyEval_CallObject(f, args);
877 Py_DECREF(args);
878 Py_DECREF(f);
879 if (r == NULL)
880 return NULL;
881 own_r = 1;
882 }
Tim Peters2400fa42001-09-12 19:12:49 +0000883 nbr = r->ob_type->tp_as_number;
884 if (i != NULL)
885 nbi = i->ob_type->tp_as_number;
886 if (nbr == NULL || nbr->nb_float == NULL ||
887 ((i != NULL) && (nbi == NULL || nbi->nb_float == NULL))) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000888 PyErr_SetString(PyExc_TypeError,
Raymond Hettinger478d47a2002-06-06 15:45:38 +0000889 "complex() argument must be a string or a number");
Tim Peters465fa3d2003-08-15 01:16:37 +0000890 if (own_r) {
891 Py_DECREF(r);
892 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000893 return NULL;
894 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000895 if (PyComplex_Check(r)) {
Tim Peters2400fa42001-09-12 19:12:49 +0000896 /* Note that if r is of a complex subtype, we're only
897 retaining its real & imag parts here, and the return
898 value is (properly) of the builtin complex type. */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000899 cr = ((PyComplexObject*)r)->cval;
900 if (own_r) {
901 Py_DECREF(r);
902 }
903 }
904 else {
905 tmp = PyNumber_Float(r);
906 if (own_r) {
907 Py_DECREF(r);
908 }
909 if (tmp == NULL)
910 return NULL;
911 if (!PyFloat_Check(tmp)) {
912 PyErr_SetString(PyExc_TypeError,
913 "float(r) didn't return a float");
914 Py_DECREF(tmp);
915 return NULL;
916 }
917 cr.real = PyFloat_AsDouble(tmp);
918 Py_DECREF(tmp);
919 cr.imag = 0.0;
920 }
921 if (i == NULL) {
922 ci.real = 0.0;
923 ci.imag = 0.0;
924 }
925 else if (PyComplex_Check(i))
926 ci = ((PyComplexObject*)i)->cval;
927 else {
928 tmp = (*nbi->nb_float)(i);
929 if (tmp == NULL)
930 return NULL;
931 ci.real = PyFloat_AsDouble(tmp);
932 Py_DECREF(tmp);
933 ci.imag = 0.;
934 }
935 cr.real -= ci.imag;
936 cr.imag += ci.real;
937 return complex_subtype_from_c_complex(type, cr);
938}
939
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000940PyDoc_STRVAR(complex_doc,
Tim Peters2400fa42001-09-12 19:12:49 +0000941"complex(real[, imag]) -> complex number\n"
942"\n"
943"Create a complex number from a real part and an optional imaginary part.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000944"This is equivalent to (real + imag*1j) where imag defaults to 0.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000945
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000946static PyNumberMethods complex_as_number = {
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000947 (binaryfunc)complex_add, /* nb_add */
948 (binaryfunc)complex_sub, /* nb_subtract */
949 (binaryfunc)complex_mul, /* nb_multiply */
Guido van Rossum393661d2001-08-31 17:40:15 +0000950 (binaryfunc)complex_classic_div, /* nb_divide */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000951 (binaryfunc)complex_remainder, /* nb_remainder */
952 (binaryfunc)complex_divmod, /* nb_divmod */
953 (ternaryfunc)complex_pow, /* nb_power */
954 (unaryfunc)complex_neg, /* nb_negative */
955 (unaryfunc)complex_pos, /* nb_positive */
956 (unaryfunc)complex_abs, /* nb_absolute */
957 (inquiry)complex_nonzero, /* nb_nonzero */
958 0, /* nb_invert */
959 0, /* nb_lshift */
960 0, /* nb_rshift */
961 0, /* nb_and */
962 0, /* nb_xor */
963 0, /* nb_or */
964 (coercion)complex_coerce, /* nb_coerce */
965 (unaryfunc)complex_int, /* nb_int */
966 (unaryfunc)complex_long, /* nb_long */
967 (unaryfunc)complex_float, /* nb_float */
968 0, /* nb_oct */
969 0, /* nb_hex */
Guido van Rossum4668b002001-08-08 05:00:18 +0000970 0, /* nb_inplace_add */
971 0, /* nb_inplace_subtract */
972 0, /* nb_inplace_multiply*/
973 0, /* nb_inplace_divide */
974 0, /* nb_inplace_remainder */
975 0, /* nb_inplace_power */
976 0, /* nb_inplace_lshift */
977 0, /* nb_inplace_rshift */
978 0, /* nb_inplace_and */
979 0, /* nb_inplace_xor */
980 0, /* nb_inplace_or */
981 (binaryfunc)complex_int_div, /* nb_floor_divide */
982 (binaryfunc)complex_div, /* nb_true_divide */
983 0, /* nb_inplace_floor_divide */
984 0, /* nb_inplace_true_divide */
Guido van Rossumf9fca921996-01-12 00:47:05 +0000985};
986
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000987PyTypeObject PyComplex_Type = {
988 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossumf9fca921996-01-12 00:47:05 +0000989 0,
990 "complex",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000991 sizeof(PyComplexObject),
Guido van Rossumf9fca921996-01-12 00:47:05 +0000992 0,
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000993 (destructor)complex_dealloc, /* tp_dealloc */
994 (printfunc)complex_print, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000995 0, /* tp_getattr */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +0000996 0, /* tp_setattr */
997 0, /* tp_compare */
998 (reprfunc)complex_repr, /* tp_repr */
999 &complex_as_number, /* tp_as_number */
1000 0, /* tp_as_sequence */
1001 0, /* tp_as_mapping */
1002 (hashfunc)complex_hash, /* tp_hash */
1003 0, /* tp_call */
Tim Peters70695122001-03-11 08:37:29 +00001004 (reprfunc)complex_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001005 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001006 0, /* tp_setattro */
1007 0, /* tp_as_buffer */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001008 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1009 complex_doc, /* tp_doc */
Guido van Rossumbe4cbb12001-01-18 01:12:39 +00001010 0, /* tp_traverse */
1011 0, /* tp_clear */
1012 complex_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001013 0, /* tp_weaklistoffset */
1014 0, /* tp_iter */
1015 0, /* tp_iternext */
1016 complex_methods, /* tp_methods */
1017 complex_members, /* tp_members */
1018 0, /* tp_getset */
1019 0, /* tp_base */
1020 0, /* tp_dict */
1021 0, /* tp_descr_get */
1022 0, /* tp_descr_set */
1023 0, /* tp_dictoffset */
1024 0, /* tp_init */
1025 0, /* tp_alloc */
1026 complex_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00001027 PyObject_Del, /* tp_free */
Guido van Rossumf9fca921996-01-12 00:47:05 +00001028};
1029
1030#endif