Merged release21-maint changes.
[python/dscho.git] / Objects / complexobject.c
blob5cfb3ca5c852519181a1d6d811bcf01a485bfbb6
2 /* Complex object implementation */
4 /* Borrows heavily from floatobject.c */
6 /* Submitted by Jim Hugunin */
8 #ifndef WITHOUT_COMPLEX
10 #include "Python.h"
11 #include "structmember.h"
13 /* Precisions used by repr() and str(), respectively.
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.
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.
26 #define PREC_REPR 17
27 #define PREC_STR 12
29 /* elementary operations on complex numbers */
31 static Py_complex c_1 = {1., 0.};
33 Py_complex
34 c_sum(Py_complex a, Py_complex b)
36 Py_complex r;
37 r.real = a.real + b.real;
38 r.imag = a.imag + b.imag;
39 return r;
42 Py_complex
43 c_diff(Py_complex a, Py_complex b)
45 Py_complex r;
46 r.real = a.real - b.real;
47 r.imag = a.imag - b.imag;
48 return r;
51 Py_complex
52 c_neg(Py_complex a)
54 Py_complex r;
55 r.real = -a.real;
56 r.imag = -a.imag;
57 return r;
60 Py_complex
61 c_prod(Py_complex a, Py_complex b)
63 Py_complex r;
64 r.real = a.real*b.real - a.imag*b.imag;
65 r.imag = a.real*b.imag + a.imag*b.real;
66 return r;
69 Py_complex
70 c_quot(Py_complex a, Py_complex b)
72 /******************************************************************
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:
79 Py_complex r;
80 double d = b.real*b.real + b.imag*b.imag;
81 if (d == 0.)
82 errno = EDOM;
83 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;
86 ******************************************************************/
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.
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;
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;
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;
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;
120 return r;
123 Py_complex
124 c_pow(Py_complex a, Py_complex b)
126 Py_complex r;
127 double vabs,len,at,phase;
128 if (b.real == 0. && b.imag == 0.) {
129 r.real = 1.;
130 r.imag = 0.;
132 else if (a.real == 0. && a.imag == 0.) {
133 if (b.imag != 0. || b.real < 0.)
134 errno = ERANGE;
135 r.real = 0.;
136 r.imag = 0.;
138 else {
139 vabs = hypot(a.real,a.imag);
140 len = pow(vabs,b.real);
141 at = atan2(a.imag, a.real);
142 phase = at*b.real;
143 if (b.imag != 0.0) {
144 len /= exp(at*b.imag);
145 phase += b.imag*log(vabs);
147 r.real = len*cos(phase);
148 r.imag = len*sin(phase);
150 return r;
153 static Py_complex
154 c_powu(Py_complex x, long n)
156 Py_complex r, p;
157 long mask = 1;
158 r = c_1;
159 p = x;
160 while (mask > 0 && n >= mask) {
161 if (n & mask)
162 r = c_prod(r,p);
163 mask <<= 1;
164 p = c_prod(p,p);
166 return r;
169 static Py_complex
170 c_powi(Py_complex x, long n)
172 Py_complex cn;
174 if (n > 100 || n < -100) {
175 cn.real = (double) n;
176 cn.imag = 0.;
177 return c_pow(x,cn);
179 else if (n > 0)
180 return c_powu(x,n);
181 else
182 return c_quot(c_1,c_powu(x,-n));
186 static PyObject *
187 complex_subtype_from_c_complex(PyTypeObject *type, Py_complex cval)
189 PyObject *op;
191 op = PyType_GenericAlloc(type, 0);
192 if (op != NULL)
193 ((PyComplexObject *)op)->cval = cval;
194 return op;
197 PyObject *
198 PyComplex_FromCComplex(Py_complex cval)
200 register PyComplexObject *op;
202 /* PyObject_New is inlined */
203 op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject));
204 if (op == NULL)
205 return PyErr_NoMemory();
206 PyObject_INIT(op, &PyComplex_Type);
207 op->cval = cval;
208 return (PyObject *) op;
211 static PyObject *
212 complex_subtype_from_doubles(PyTypeObject *type, double real, double imag)
214 Py_complex c;
215 c.real = real;
216 c.imag = imag;
217 return complex_subtype_from_c_complex(type, c);
220 PyObject *
221 PyComplex_FromDoubles(double real, double imag)
223 Py_complex c;
224 c.real = real;
225 c.imag = imag;
226 return PyComplex_FromCComplex(c);
229 double
230 PyComplex_RealAsDouble(PyObject *op)
232 if (PyComplex_Check(op)) {
233 return ((PyComplexObject *)op)->cval.real;
235 else {
236 return PyFloat_AsDouble(op);
240 double
241 PyComplex_ImagAsDouble(PyObject *op)
243 if (PyComplex_Check(op)) {
244 return ((PyComplexObject *)op)->cval.imag;
246 else {
247 return 0.0;
251 Py_complex
252 PyComplex_AsCComplex(PyObject *op)
254 Py_complex cv;
255 if (PyComplex_Check(op)) {
256 return ((PyComplexObject *)op)->cval;
258 else {
259 cv.real = PyFloat_AsDouble(op);
260 cv.imag = 0.;
261 return cv;
265 static void
266 complex_dealloc(PyObject *op)
268 PyObject_DEL(op);
272 static void
273 complex_to_buf(char *buf, PyComplexObject *v, int precision)
275 if (v->cval.real == 0.)
276 sprintf(buf, "%.*gj", precision, v->cval.imag);
277 else
278 sprintf(buf, "(%.*g%+.*gj)", precision, v->cval.real,
279 precision, v->cval.imag);
282 static int
283 complex_print(PyComplexObject *v, FILE *fp, int flags)
285 char buf[100];
286 complex_to_buf(buf, v,
287 (flags & Py_PRINT_RAW) ? PREC_STR : PREC_REPR);
288 fputs(buf, fp);
289 return 0;
292 static PyObject *
293 complex_repr(PyComplexObject *v)
295 char buf[100];
296 complex_to_buf(buf, v, PREC_REPR);
297 return PyString_FromString(buf);
300 static PyObject *
301 complex_str(PyComplexObject *v)
303 char buf[100];
304 complex_to_buf(buf, v, PREC_STR);
305 return PyString_FromString(buf);
308 static long
309 complex_hash(PyComplexObject *v)
311 long hashreal, hashimag, combined;
312 hashreal = _Py_HashDouble(v->cval.real);
313 if (hashreal == -1)
314 return -1;
315 hashimag = _Py_HashDouble(v->cval.imag);
316 if (hashimag == -1)
317 return -1;
318 /* Note: if the imaginary part is 0, hashimag is 0 now,
319 * so the following returns hashreal unchanged. This is
320 * important because numbers of different types that
321 * compare equal must have the same hash value, so that
322 * hash(x + 0*j) must equal hash(x).
324 combined = hashreal + 1000003 * hashimag;
325 if (combined == -1)
326 combined = -2;
327 return combined;
330 static PyObject *
331 complex_add(PyComplexObject *v, PyComplexObject *w)
333 Py_complex result;
334 PyFPE_START_PROTECT("complex_add", return 0)
335 result = c_sum(v->cval,w->cval);
336 PyFPE_END_PROTECT(result)
337 return PyComplex_FromCComplex(result);
340 static PyObject *
341 complex_sub(PyComplexObject *v, PyComplexObject *w)
343 Py_complex result;
344 PyFPE_START_PROTECT("complex_sub", return 0)
345 result = c_diff(v->cval,w->cval);
346 PyFPE_END_PROTECT(result)
347 return PyComplex_FromCComplex(result);
350 static PyObject *
351 complex_mul(PyComplexObject *v, PyComplexObject *w)
353 Py_complex result;
354 PyFPE_START_PROTECT("complex_mul", return 0)
355 result = c_prod(v->cval,w->cval);
356 PyFPE_END_PROTECT(result)
357 return PyComplex_FromCComplex(result);
360 static PyObject *
361 complex_div(PyComplexObject *v, PyComplexObject *w)
363 Py_complex quot;
364 PyFPE_START_PROTECT("complex_div", return 0)
365 errno = 0;
366 quot = c_quot(v->cval,w->cval);
367 PyFPE_END_PROTECT(quot)
368 if (errno == EDOM) {
369 PyErr_SetString(PyExc_ZeroDivisionError, "complex division");
370 return NULL;
372 return PyComplex_FromCComplex(quot);
375 static PyObject *
376 complex_remainder(PyComplexObject *v, PyComplexObject *w)
378 Py_complex div, mod;
379 errno = 0;
380 div = c_quot(v->cval,w->cval); /* The raw divisor value. */
381 if (errno == EDOM) {
382 PyErr_SetString(PyExc_ZeroDivisionError, "complex remainder");
383 return NULL;
385 div.real = floor(div.real); /* Use the floor of the real part. */
386 div.imag = 0.0;
387 mod = c_diff(v->cval, c_prod(w->cval, div));
389 return PyComplex_FromCComplex(mod);
393 static PyObject *
394 complex_divmod(PyComplexObject *v, PyComplexObject *w)
396 Py_complex div, mod;
397 PyObject *d, *m, *z;
398 errno = 0;
399 div = c_quot(v->cval,w->cval); /* The raw divisor value. */
400 if (errno == EDOM) {
401 PyErr_SetString(PyExc_ZeroDivisionError, "complex divmod()");
402 return NULL;
404 div.real = floor(div.real); /* Use the floor of the real part. */
405 div.imag = 0.0;
406 mod = c_diff(v->cval, c_prod(w->cval, div));
407 d = PyComplex_FromCComplex(div);
408 m = PyComplex_FromCComplex(mod);
409 z = Py_BuildValue("(OO)", d, m);
410 Py_XDECREF(d);
411 Py_XDECREF(m);
412 return z;
415 static PyObject *
416 complex_pow(PyComplexObject *v, PyObject *w, PyComplexObject *z)
418 Py_complex p;
419 Py_complex exponent;
420 long int_exponent;
422 if ((PyObject *)z!=Py_None) {
423 PyErr_SetString(PyExc_ValueError, "complex modulo");
424 return NULL;
426 PyFPE_START_PROTECT("complex_pow", return 0)
427 errno = 0;
428 exponent = ((PyComplexObject*)w)->cval;
429 int_exponent = (long)exponent.real;
430 if (exponent.imag == 0. && exponent.real == int_exponent)
431 p = c_powi(v->cval,int_exponent);
432 else
433 p = c_pow(v->cval,exponent);
435 PyFPE_END_PROTECT(p)
436 if (errno == ERANGE) {
437 PyErr_SetString(PyExc_ValueError,
438 "0.0 to a negative or complex power");
439 return NULL;
441 return PyComplex_FromCComplex(p);
444 static PyObject *
445 complex_int_div(PyComplexObject *v, PyComplexObject *w)
447 PyObject *t, *r;
449 t = complex_divmod(v, w);
450 if (t != NULL) {
451 r = PyTuple_GET_ITEM(t, 0);
452 Py_INCREF(r);
453 Py_DECREF(t);
454 return r;
456 return NULL;
459 static PyObject *
460 complex_neg(PyComplexObject *v)
462 Py_complex neg;
463 neg.real = -v->cval.real;
464 neg.imag = -v->cval.imag;
465 return PyComplex_FromCComplex(neg);
468 static PyObject *
469 complex_pos(PyComplexObject *v)
471 Py_INCREF(v);
472 return (PyObject *)v;
475 static PyObject *
476 complex_abs(PyComplexObject *v)
478 double result;
479 PyFPE_START_PROTECT("complex_abs", return 0)
480 result = hypot(v->cval.real,v->cval.imag);
481 PyFPE_END_PROTECT(result)
482 return PyFloat_FromDouble(result);
485 static int
486 complex_nonzero(PyComplexObject *v)
488 return v->cval.real != 0.0 || v->cval.imag != 0.0;
491 static int
492 complex_coerce(PyObject **pv, PyObject **pw)
494 Py_complex cval;
495 cval.imag = 0.;
496 if (PyInt_Check(*pw)) {
497 cval.real = (double)PyInt_AsLong(*pw);
498 *pw = PyComplex_FromCComplex(cval);
499 Py_INCREF(*pv);
500 return 0;
502 else if (PyLong_Check(*pw)) {
503 cval.real = PyLong_AsDouble(*pw);
504 *pw = PyComplex_FromCComplex(cval);
505 Py_INCREF(*pv);
506 return 0;
508 else if (PyFloat_Check(*pw)) {
509 cval.real = PyFloat_AsDouble(*pw);
510 *pw = PyComplex_FromCComplex(cval);
511 Py_INCREF(*pv);
512 return 0;
514 return 1; /* Can't do it */
517 static PyObject *
518 complex_richcompare(PyObject *v, PyObject *w, int op)
520 int c;
521 Py_complex i, j;
522 PyObject *res;
524 if (op != Py_EQ && op != Py_NE) {
525 PyErr_SetString(PyExc_TypeError,
526 "cannot compare complex numbers using <, <=, >, >=");
527 return NULL;
530 c = PyNumber_CoerceEx(&v, &w);
531 if (c < 0)
532 return NULL;
533 if (c > 0) {
534 Py_INCREF(Py_NotImplemented);
535 return Py_NotImplemented;
537 if (!PyComplex_Check(v) || !PyComplex_Check(w)) {
538 Py_DECREF(v);
539 Py_DECREF(w);
540 Py_INCREF(Py_NotImplemented);
541 return Py_NotImplemented;
544 i = ((PyComplexObject *)v)->cval;
545 j = ((PyComplexObject *)w)->cval;
546 Py_DECREF(v);
547 Py_DECREF(w);
549 if ((i.real == j.real && i.imag == j.imag) == (op == Py_EQ))
550 res = Py_True;
551 else
552 res = Py_False;
554 Py_INCREF(res);
555 return res;
558 static PyObject *
559 complex_int(PyObject *v)
561 PyErr_SetString(PyExc_TypeError,
562 "can't convert complex to int; use e.g. int(abs(z))");
563 return NULL;
566 static PyObject *
567 complex_long(PyObject *v)
569 PyErr_SetString(PyExc_TypeError,
570 "can't convert complex to long; use e.g. long(abs(z))");
571 return NULL;
574 static PyObject *
575 complex_float(PyObject *v)
577 PyErr_SetString(PyExc_TypeError,
578 "can't convert complex to float; use e.g. abs(z)");
579 return NULL;
582 static PyObject *
583 complex_conjugate(PyObject *self, PyObject *args)
585 Py_complex c;
586 if (!PyArg_ParseTuple(args, ":conjugate"))
587 return NULL;
588 c = ((PyComplexObject *)self)->cval;
589 c.imag = -c.imag;
590 return PyComplex_FromCComplex(c);
593 static PyMethodDef complex_methods[] = {
594 {"conjugate", complex_conjugate, 1},
595 {NULL, NULL} /* sentinel */
598 static struct memberlist complex_members[] = {
599 {"real", T_DOUBLE, offsetof(PyComplexObject, cval.real), 0},
600 {"imag", T_DOUBLE, offsetof(PyComplexObject, cval.imag), 0},
601 {0},
604 static PyObject *
605 complex_subtype_from_string(PyTypeObject *type, PyObject *v)
607 extern double strtod(const char *, char **);
608 const char *s, *start;
609 char *end;
610 double x=0.0, y=0.0, z;
611 int got_re=0, got_im=0, done=0;
612 int digit_or_dot;
613 int sw_error=0;
614 int sign;
615 char buffer[256]; /* For errors */
616 char s_buffer[256];
617 int len;
619 if (PyString_Check(v)) {
620 s = PyString_AS_STRING(v);
621 len = PyString_GET_SIZE(v);
623 else if (PyUnicode_Check(v)) {
624 if (PyUnicode_GET_SIZE(v) >= sizeof(s_buffer)) {
625 PyErr_SetString(PyExc_ValueError,
626 "complex() literal too large to convert");
627 return NULL;
629 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
630 PyUnicode_GET_SIZE(v),
631 s_buffer,
632 NULL))
633 return NULL;
634 s = s_buffer;
635 len = (int)strlen(s);
637 else if (PyObject_AsCharBuffer(v, &s, &len)) {
638 PyErr_SetString(PyExc_TypeError,
639 "complex() arg is not a string");
640 return NULL;
643 /* position on first nonblank */
644 start = s;
645 while (*s && isspace(Py_CHARMASK(*s)))
646 s++;
647 if (s[0] == '\0') {
648 PyErr_SetString(PyExc_ValueError,
649 "complex() arg is an empty string");
650 return NULL;
653 z = -1.0;
654 sign = 1;
655 do {
657 switch (*s) {
659 case '\0':
660 if (s-start != len) {
661 PyErr_SetString(
662 PyExc_ValueError,
663 "complex() arg contains a null byte");
664 return NULL;
666 if(!done) sw_error=1;
667 break;
669 case '-':
670 sign = -1;
671 /* Fallthrough */
672 case '+':
673 if (done) sw_error=1;
674 s++;
675 if ( *s=='\0'||*s=='+'||*s=='-' ||
676 isspace(Py_CHARMASK(*s)) ) sw_error=1;
677 break;
679 case 'J':
680 case 'j':
681 if (got_im || done) {
682 sw_error = 1;
683 break;
685 if (z<0.0) {
686 y=sign;
688 else{
689 y=sign*z;
691 got_im=1;
692 s++;
693 if (*s!='+' && *s!='-' )
694 done=1;
695 break;
697 default:
698 if (isspace(Py_CHARMASK(*s))) {
699 while (*s && isspace(Py_CHARMASK(*s)))
700 s++;
701 if (s[0] != '\0')
702 sw_error=1;
703 else
704 done = 1;
705 break;
707 digit_or_dot =
708 (*s=='.' || isdigit(Py_CHARMASK(*s)));
709 if (done||!digit_or_dot) {
710 sw_error=1;
711 break;
713 errno = 0;
714 PyFPE_START_PROTECT("strtod", return 0)
715 z = strtod(s, &end) ;
716 PyFPE_END_PROTECT(z)
717 if (errno != 0) {
718 sprintf(buffer,
719 "float() out of range: %.150s", s);
720 PyErr_SetString(
721 PyExc_ValueError,
722 buffer);
723 return NULL;
725 s=end;
726 if (*s=='J' || *s=='j') {
728 break;
730 if (got_re) {
731 sw_error=1;
732 break;
735 /* accept a real part */
736 x=sign*z;
737 got_re=1;
738 if (got_im) done=1;
739 z = -1.0;
740 sign = 1;
741 break;
743 } /* end of switch */
745 } while (*s!='\0' && !sw_error);
747 if (sw_error) {
748 PyErr_SetString(PyExc_ValueError,
749 "complex() arg is a malformed string");
750 return NULL;
753 return complex_subtype_from_doubles(type, x, y);
756 static PyObject *
757 complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
759 PyObject *r, *i, *tmp;
760 PyNumberMethods *nbr, *nbi = NULL;
761 Py_complex cr, ci;
762 int own_r = 0;
763 static char *kwlist[] = {"real", "imag", 0};
765 r = Py_False;
766 i = NULL;
767 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:complex", kwlist,
768 &r, &i))
769 return NULL;
770 if (PyString_Check(r) || PyUnicode_Check(r))
771 return complex_subtype_from_string(type, r);
772 if ((nbr = r->ob_type->tp_as_number) == NULL ||
773 nbr->nb_float == NULL ||
774 (i != NULL &&
775 ((nbi = i->ob_type->tp_as_number) == NULL ||
776 nbi->nb_float == NULL))) {
777 PyErr_SetString(PyExc_TypeError,
778 "complex() arg can't be converted to complex");
779 return NULL;
781 /* XXX Hack to support classes with __complex__ method */
782 if (PyInstance_Check(r)) {
783 static PyObject *complexstr;
784 PyObject *f;
785 if (complexstr == NULL) {
786 complexstr = PyString_InternFromString("__complex__");
787 if (complexstr == NULL)
788 return NULL;
790 f = PyObject_GetAttr(r, complexstr);
791 if (f == NULL)
792 PyErr_Clear();
793 else {
794 PyObject *args = Py_BuildValue("()");
795 if (args == NULL)
796 return NULL;
797 r = PyEval_CallObject(f, args);
798 Py_DECREF(args);
799 Py_DECREF(f);
800 if (r == NULL)
801 return NULL;
802 own_r = 1;
805 if (PyComplex_Check(r)) {
806 cr = ((PyComplexObject*)r)->cval;
807 if (own_r) {
808 Py_DECREF(r);
811 else {
812 tmp = PyNumber_Float(r);
813 if (own_r) {
814 Py_DECREF(r);
816 if (tmp == NULL)
817 return NULL;
818 if (!PyFloat_Check(tmp)) {
819 PyErr_SetString(PyExc_TypeError,
820 "float(r) didn't return a float");
821 Py_DECREF(tmp);
822 return NULL;
824 cr.real = PyFloat_AsDouble(tmp);
825 Py_DECREF(tmp);
826 cr.imag = 0.0;
828 if (i == NULL) {
829 ci.real = 0.0;
830 ci.imag = 0.0;
832 else if (PyComplex_Check(i))
833 ci = ((PyComplexObject*)i)->cval;
834 else {
835 tmp = (*nbi->nb_float)(i);
836 if (tmp == NULL)
837 return NULL;
838 ci.real = PyFloat_AsDouble(tmp);
839 Py_DECREF(tmp);
840 ci.imag = 0.;
842 cr.real -= ci.imag;
843 cr.imag += ci.real;
844 return complex_subtype_from_c_complex(type, cr);
847 static char complex_doc[] =
848 "complex(real[, imag]) -> complex number\n\
850 Create a complex number from a real part and an optional imaginary part.\n\
851 This is equivalent to (real + imag*1j) where imag defaults to 0.";
853 static PyNumberMethods complex_as_number = {
854 (binaryfunc)complex_add, /* nb_add */
855 (binaryfunc)complex_sub, /* nb_subtract */
856 (binaryfunc)complex_mul, /* nb_multiply */
857 (binaryfunc)complex_div, /* nb_divide */
858 (binaryfunc)complex_remainder, /* nb_remainder */
859 (binaryfunc)complex_divmod, /* nb_divmod */
860 (ternaryfunc)complex_pow, /* nb_power */
861 (unaryfunc)complex_neg, /* nb_negative */
862 (unaryfunc)complex_pos, /* nb_positive */
863 (unaryfunc)complex_abs, /* nb_absolute */
864 (inquiry)complex_nonzero, /* nb_nonzero */
865 0, /* nb_invert */
866 0, /* nb_lshift */
867 0, /* nb_rshift */
868 0, /* nb_and */
869 0, /* nb_xor */
870 0, /* nb_or */
871 (coercion)complex_coerce, /* nb_coerce */
872 (unaryfunc)complex_int, /* nb_int */
873 (unaryfunc)complex_long, /* nb_long */
874 (unaryfunc)complex_float, /* nb_float */
875 0, /* nb_oct */
876 0, /* nb_hex */
877 0, /* nb_inplace_add */
878 0, /* nb_inplace_subtract */
879 0, /* nb_inplace_multiply*/
880 0, /* nb_inplace_divide */
881 0, /* nb_inplace_remainder */
882 0, /* nb_inplace_power */
883 0, /* nb_inplace_lshift */
884 0, /* nb_inplace_rshift */
885 0, /* nb_inplace_and */
886 0, /* nb_inplace_xor */
887 0, /* nb_inplace_or */
888 (binaryfunc)complex_int_div, /* nb_floor_divide */
889 (binaryfunc)complex_div, /* nb_true_divide */
890 0, /* nb_inplace_floor_divide */
891 0, /* nb_inplace_true_divide */
894 PyTypeObject PyComplex_Type = {
895 PyObject_HEAD_INIT(&PyType_Type)
897 "complex",
898 sizeof(PyComplexObject),
900 (destructor)complex_dealloc, /* tp_dealloc */
901 (printfunc)complex_print, /* tp_print */
902 0, /* tp_getattr */
903 0, /* tp_setattr */
904 0, /* tp_compare */
905 (reprfunc)complex_repr, /* tp_repr */
906 &complex_as_number, /* tp_as_number */
907 0, /* tp_as_sequence */
908 0, /* tp_as_mapping */
909 (hashfunc)complex_hash, /* tp_hash */
910 0, /* tp_call */
911 (reprfunc)complex_str, /* tp_str */
912 PyObject_GenericGetAttr, /* tp_getattro */
913 0, /* tp_setattro */
914 0, /* tp_as_buffer */
915 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
916 complex_doc, /* tp_doc */
917 0, /* tp_traverse */
918 0, /* tp_clear */
919 complex_richcompare, /* tp_richcompare */
920 0, /* tp_weaklistoffset */
921 0, /* tp_iter */
922 0, /* tp_iternext */
923 complex_methods, /* tp_methods */
924 complex_members, /* tp_members */
925 0, /* tp_getset */
926 0, /* tp_base */
927 0, /* tp_dict */
928 0, /* tp_descr_get */
929 0, /* tp_descr_set */
930 0, /* tp_dictoffset */
931 0, /* tp_init */
932 0, /* tp_alloc */
933 complex_new, /* tp_new */
936 #endif