Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 2 | /* Complex object implementation */ |
| 3 | |
| 4 | /* Borrows heavily from floatobject.c */ |
| 5 | |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 6 | /* Submitted by Jim Hugunin */ |
| 7 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 8 | #include "Python.h" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9 | #include "structmember.h" |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | 65ce6de | 2002-06-13 17:07:07 +0000 | [diff] [blame] | 11 | #ifndef WITHOUT_COMPLEX |
| 12 | |
Tim Peters | 7069512 | 2001-03-11 08:37:29 +0000 | [diff] [blame] | 13 | /* 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 Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 28 | |
| 29 | /* elementary operations on complex numbers */ |
| 30 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 31 | static Py_complex c_1 = {1., 0.}; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 32 | |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 33 | Py_complex |
| 34 | c_sum(Py_complex a, Py_complex b) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 35 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 36 | Py_complex r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 37 | r.real = a.real + b.real; |
| 38 | r.imag = a.imag + b.imag; |
| 39 | return r; |
| 40 | } |
| 41 | |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 42 | Py_complex |
| 43 | c_diff(Py_complex a, Py_complex b) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 44 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 45 | Py_complex r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 46 | r.real = a.real - b.real; |
| 47 | r.imag = a.imag - b.imag; |
| 48 | return r; |
| 49 | } |
| 50 | |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 51 | Py_complex |
| 52 | c_neg(Py_complex a) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 53 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 54 | Py_complex r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 55 | r.real = -a.real; |
| 56 | r.imag = -a.imag; |
| 57 | return r; |
| 58 | } |
| 59 | |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 60 | Py_complex |
| 61 | c_prod(Py_complex a, Py_complex b) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 62 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 63 | Py_complex r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 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; |
| 67 | } |
| 68 | |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 69 | Py_complex |
| 70 | c_quot(Py_complex a, Py_complex b) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 71 | { |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 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: |
| 78 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 79 | Py_complex r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 80 | double d = b.real*b.real + b.imag*b.imag; |
| 81 | if (d == 0.) |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 82 | errno = EDOM; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 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; |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 86 | ******************************************************************/ |
| 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 Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 123 | Py_complex |
| 124 | c_pow(Py_complex a, Py_complex b) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 125 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 126 | Py_complex r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 127 | 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 Peters | bab22be | 2002-03-22 02:48:46 +0000 | [diff] [blame] | 134 | errno = EDOM; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 135 | 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öwis | 387c547 | 2001-09-06 08:16:17 +0000 | [diff] [blame] | 141 | at = atan2(a.imag, a.real); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 142 | 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 Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 153 | static Py_complex |
| 154 | c_powu(Py_complex x, long n) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 155 | { |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 156 | Py_complex r, p; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 157 | long mask = 1; |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 158 | r = c_1; |
| 159 | p = x; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 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); |
| 165 | } |
| 166 | return r; |
| 167 | } |
| 168 | |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 169 | static Py_complex |
| 170 | c_powi(Py_complex x, long n) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 171 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 172 | Py_complex cn; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 173 | |
| 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 Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 186 | static PyObject * |
| 187 | complex_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 Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 197 | PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 198 | PyComplex_FromCComplex(Py_complex cval) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 199 | { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 200 | register PyComplexObject *op; |
| 201 | |
Guido van Rossum | e3a8e7e | 2002-08-19 19:26:42 +0000 | [diff] [blame] | 202 | /* Inline PyObject_New */ |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 203 | op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject)); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 204 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 205 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 206 | PyObject_INIT(op, &PyComplex_Type); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 207 | op->cval = cval; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 208 | return (PyObject *) op; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 211 | static PyObject * |
| 212 | complex_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 Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 220 | PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 221 | PyComplex_FromDoubles(double real, double imag) |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 222 | { |
| 223 | Py_complex c; |
| 224 | c.real = real; |
| 225 | c.imag = imag; |
| 226 | return PyComplex_FromCComplex(c); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | double |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 230 | PyComplex_RealAsDouble(PyObject *op) |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 231 | { |
| 232 | if (PyComplex_Check(op)) { |
| 233 | return ((PyComplexObject *)op)->cval.real; |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 234 | } |
| 235 | else { |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 236 | return PyFloat_AsDouble(op); |
| 237 | } |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | double |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 241 | PyComplex_ImagAsDouble(PyObject *op) |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 242 | { |
| 243 | if (PyComplex_Check(op)) { |
| 244 | return ((PyComplexObject *)op)->cval.imag; |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 245 | } |
| 246 | else { |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 247 | return 0.0; |
| 248 | } |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 251 | Py_complex |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 252 | PyComplex_AsCComplex(PyObject *op) |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 253 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 254 | Py_complex cv; |
Guido van Rossum | cf3d108 | 1996-01-12 01:21:14 +0000 | [diff] [blame] | 255 | if (PyComplex_Check(op)) { |
| 256 | return ((PyComplexObject *)op)->cval; |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 257 | } |
| 258 | else { |
Guido van Rossum | cf3d108 | 1996-01-12 01:21:14 +0000 | [diff] [blame] | 259 | cv.real = PyFloat_AsDouble(op); |
| 260 | cv.imag = 0.; |
| 261 | return cv; |
Tim Peters | 7069512 | 2001-03-11 08:37:29 +0000 | [diff] [blame] | 262 | } |
Guido van Rossum | cf3d108 | 1996-01-12 01:21:14 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 265 | static void |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 266 | complex_dealloc(PyObject *op) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 267 | { |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 268 | op->ob_type->tp_free(op); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | |
Guido van Rossum | 363078a | 1996-05-24 20:45:01 +0000 | [diff] [blame] | 272 | static void |
Barry Warsaw | 01d697a | 2001-11-28 20:50:56 +0000 | [diff] [blame] | 273 | complex_to_buf(char *buf, int bufsz, PyComplexObject *v, int precision) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 274 | { |
| 275 | if (v->cval.real == 0.) |
Barry Warsaw | 01d697a | 2001-11-28 20:50:56 +0000 | [diff] [blame] | 276 | PyOS_snprintf(buf, bufsz, "%.*gj", |
| 277 | precision, v->cval.imag); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 278 | else |
Barry Warsaw | 01d697a | 2001-11-28 20:50:56 +0000 | [diff] [blame] | 279 | PyOS_snprintf(buf, bufsz, "(%.*g%+.*gj)", |
| 280 | precision, v->cval.real, |
| 281 | precision, v->cval.imag); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | static int |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 285 | complex_print(PyComplexObject *v, FILE *fp, int flags) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 286 | { |
| 287 | char buf[100]; |
Barry Warsaw | 01d697a | 2001-11-28 20:50:56 +0000 | [diff] [blame] | 288 | complex_to_buf(buf, sizeof(buf), v, |
Tim Peters | 7069512 | 2001-03-11 08:37:29 +0000 | [diff] [blame] | 289 | (flags & Py_PRINT_RAW) ? PREC_STR : PREC_REPR); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 290 | fputs(buf, fp); |
| 291 | return 0; |
| 292 | } |
| 293 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 294 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 295 | complex_repr(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 296 | { |
| 297 | char buf[100]; |
Barry Warsaw | 01d697a | 2001-11-28 20:50:56 +0000 | [diff] [blame] | 298 | complex_to_buf(buf, sizeof(buf), v, PREC_REPR); |
Tim Peters | 7069512 | 2001-03-11 08:37:29 +0000 | [diff] [blame] | 299 | return PyString_FromString(buf); |
| 300 | } |
| 301 | |
| 302 | static PyObject * |
| 303 | complex_str(PyComplexObject *v) |
| 304 | { |
| 305 | char buf[100]; |
Barry Warsaw | 01d697a | 2001-11-28 20:50:56 +0000 | [diff] [blame] | 306 | complex_to_buf(buf, sizeof(buf), v, PREC_STR); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 307 | return PyString_FromString(buf); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 310 | static long |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 311 | complex_hash(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 312 | { |
Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 313 | long hashreal, hashimag, combined; |
| 314 | hashreal = _Py_HashDouble(v->cval.real); |
| 315 | if (hashreal == -1) |
| 316 | return -1; |
| 317 | hashimag = _Py_HashDouble(v->cval.imag); |
| 318 | if (hashimag == -1) |
| 319 | return -1; |
| 320 | /* Note: if the imaginary part is 0, hashimag is 0 now, |
| 321 | * so the following returns hashreal unchanged. This is |
| 322 | * important because numbers of different types that |
| 323 | * compare equal must have the same hash value, so that |
| 324 | * hash(x + 0*j) must equal hash(x). |
| 325 | */ |
| 326 | combined = hashreal + 1000003 * hashimag; |
| 327 | if (combined == -1) |
| 328 | combined = -2; |
| 329 | return combined; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 332 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 333 | complex_add(PyComplexObject *v, PyComplexObject *w) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 334 | { |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 335 | Py_complex result; |
| 336 | PyFPE_START_PROTECT("complex_add", return 0) |
| 337 | result = c_sum(v->cval,w->cval); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 338 | PyFPE_END_PROTECT(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 339 | return PyComplex_FromCComplex(result); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 342 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 343 | complex_sub(PyComplexObject *v, PyComplexObject *w) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 344 | { |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 345 | Py_complex result; |
| 346 | PyFPE_START_PROTECT("complex_sub", return 0) |
| 347 | result = c_diff(v->cval,w->cval); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 348 | PyFPE_END_PROTECT(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 349 | return PyComplex_FromCComplex(result); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 352 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 353 | complex_mul(PyComplexObject *v, PyComplexObject *w) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 354 | { |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 355 | Py_complex result; |
| 356 | PyFPE_START_PROTECT("complex_mul", return 0) |
| 357 | result = c_prod(v->cval,w->cval); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 358 | PyFPE_END_PROTECT(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 359 | return PyComplex_FromCComplex(result); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 362 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 363 | complex_div(PyComplexObject *v, PyComplexObject *w) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 364 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 365 | Py_complex quot; |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 366 | PyFPE_START_PROTECT("complex_div", return 0) |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 367 | errno = 0; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 368 | quot = c_quot(v->cval,w->cval); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 369 | PyFPE_END_PROTECT(quot) |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 370 | if (errno == EDOM) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 371 | PyErr_SetString(PyExc_ZeroDivisionError, "complex division"); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 372 | return NULL; |
| 373 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 374 | return PyComplex_FromCComplex(quot); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 377 | static PyObject * |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 378 | complex_classic_div(PyComplexObject *v, PyComplexObject *w) |
| 379 | { |
| 380 | Py_complex quot; |
| 381 | |
Guido van Rossum | 1832de4 | 2001-09-04 03:51:09 +0000 | [diff] [blame] | 382 | if (Py_DivisionWarningFlag >= 2 && |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 383 | PyErr_Warn(PyExc_DeprecationWarning, |
| 384 | "classic complex division") < 0) |
| 385 | return NULL; |
| 386 | |
| 387 | PyFPE_START_PROTECT("complex_classic_div", return 0) |
| 388 | errno = 0; |
| 389 | quot = c_quot(v->cval,w->cval); |
| 390 | PyFPE_END_PROTECT(quot) |
| 391 | if (errno == EDOM) { |
| 392 | PyErr_SetString(PyExc_ZeroDivisionError, "complex division"); |
| 393 | return NULL; |
| 394 | } |
| 395 | return PyComplex_FromCComplex(quot); |
| 396 | } |
| 397 | |
| 398 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 399 | complex_remainder(PyComplexObject *v, PyComplexObject *w) |
Guido van Rossum | ee09fc1 | 1996-09-11 13:55:55 +0000 | [diff] [blame] | 400 | { |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 401 | Py_complex div, mod; |
Guido van Rossum | 69cf3c7 | 2002-04-15 12:39:12 +0000 | [diff] [blame] | 402 | |
| 403 | if (PyErr_Warn(PyExc_DeprecationWarning, |
| 404 | "complex divmod(), // and % are deprecated") < 0) |
| 405 | return NULL; |
| 406 | |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 407 | errno = 0; |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 408 | div = c_quot(v->cval,w->cval); /* The raw divisor value. */ |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 409 | if (errno == EDOM) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 410 | PyErr_SetString(PyExc_ZeroDivisionError, "complex remainder"); |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 411 | return NULL; |
| 412 | } |
| 413 | div.real = floor(div.real); /* Use the floor of the real part. */ |
| 414 | div.imag = 0.0; |
| 415 | mod = c_diff(v->cval, c_prod(w->cval, div)); |
| 416 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 417 | return PyComplex_FromCComplex(mod); |
Guido van Rossum | ee09fc1 | 1996-09-11 13:55:55 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Guido van Rossum | ee09fc1 | 1996-09-11 13:55:55 +0000 | [diff] [blame] | 420 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 421 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 422 | complex_divmod(PyComplexObject *v, PyComplexObject *w) |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 423 | { |
| 424 | Py_complex div, mod; |
| 425 | PyObject *d, *m, *z; |
Guido van Rossum | 9ec4c78 | 2002-04-15 01:41:56 +0000 | [diff] [blame] | 426 | |
| 427 | if (PyErr_Warn(PyExc_DeprecationWarning, |
Guido van Rossum | 69cf3c7 | 2002-04-15 12:39:12 +0000 | [diff] [blame] | 428 | "complex divmod(), // and % are deprecated") < 0) |
Guido van Rossum | 9ec4c78 | 2002-04-15 01:41:56 +0000 | [diff] [blame] | 429 | return NULL; |
| 430 | |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 431 | errno = 0; |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 432 | div = c_quot(v->cval,w->cval); /* The raw divisor value. */ |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 433 | if (errno == EDOM) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 434 | PyErr_SetString(PyExc_ZeroDivisionError, "complex divmod()"); |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 435 | return NULL; |
| 436 | } |
| 437 | div.real = floor(div.real); /* Use the floor of the real part. */ |
| 438 | div.imag = 0.0; |
| 439 | mod = c_diff(v->cval, c_prod(w->cval, div)); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 440 | d = PyComplex_FromCComplex(div); |
| 441 | m = PyComplex_FromCComplex(mod); |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 442 | z = PyTuple_Pack(2, d, m); |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 443 | Py_XDECREF(d); |
| 444 | Py_XDECREF(m); |
| 445 | return z; |
| 446 | } |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 447 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 448 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 449 | complex_pow(PyComplexObject *v, PyObject *w, PyComplexObject *z) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 450 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 451 | Py_complex p; |
| 452 | Py_complex exponent; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 453 | long int_exponent; |
| 454 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 455 | if ((PyObject *)z!=Py_None) { |
| 456 | PyErr_SetString(PyExc_ValueError, "complex modulo"); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 457 | return NULL; |
| 458 | } |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 459 | PyFPE_START_PROTECT("complex_pow", return 0) |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 460 | errno = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 461 | exponent = ((PyComplexObject*)w)->cval; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 462 | int_exponent = (long)exponent.real; |
| 463 | if (exponent.imag == 0. && exponent.real == int_exponent) |
| 464 | p = c_powi(v->cval,int_exponent); |
| 465 | else |
| 466 | p = c_pow(v->cval,exponent); |
| 467 | |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 468 | PyFPE_END_PROTECT(p) |
Tim Peters | bab22be | 2002-03-22 02:48:46 +0000 | [diff] [blame] | 469 | Py_ADJUST_ERANGE2(p.real, p.imag); |
| 470 | if (errno == EDOM) { |
| 471 | PyErr_SetString(PyExc_ZeroDivisionError, |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 472 | "0.0 to a negative or complex power"); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 473 | return NULL; |
| 474 | } |
Tim Peters | bab22be | 2002-03-22 02:48:46 +0000 | [diff] [blame] | 475 | else if (errno == ERANGE) { |
| 476 | PyErr_SetString(PyExc_OverflowError, |
| 477 | "complex exponentiaion"); |
| 478 | return NULL; |
| 479 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 480 | return PyComplex_FromCComplex(p); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 483 | static PyObject * |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 484 | complex_int_div(PyComplexObject *v, PyComplexObject *w) |
| 485 | { |
| 486 | PyObject *t, *r; |
| 487 | |
| 488 | t = complex_divmod(v, w); |
| 489 | if (t != NULL) { |
| 490 | r = PyTuple_GET_ITEM(t, 0); |
| 491 | Py_INCREF(r); |
| 492 | Py_DECREF(t); |
| 493 | return r; |
| 494 | } |
| 495 | return NULL; |
| 496 | } |
| 497 | |
| 498 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 499 | complex_neg(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 500 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 501 | Py_complex neg; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 502 | neg.real = -v->cval.real; |
| 503 | neg.imag = -v->cval.imag; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 504 | return PyComplex_FromCComplex(neg); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 507 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 508 | complex_pos(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 509 | { |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 510 | if (PyComplex_CheckExact(v)) { |
| 511 | Py_INCREF(v); |
| 512 | return (PyObject *)v; |
| 513 | } |
| 514 | else |
| 515 | return PyComplex_FromCComplex(v->cval); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 518 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 519 | complex_abs(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 520 | { |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 521 | double result; |
| 522 | PyFPE_START_PROTECT("complex_abs", return 0) |
| 523 | result = hypot(v->cval.real,v->cval.imag); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 524 | PyFPE_END_PROTECT(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 525 | return PyFloat_FromDouble(result); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | static int |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 529 | complex_nonzero(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 530 | { |
Guido van Rossum | 3bbef60 | 1999-01-25 19:42:19 +0000 | [diff] [blame] | 531 | return v->cval.real != 0.0 || v->cval.imag != 0.0; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | static int |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 535 | complex_coerce(PyObject **pv, PyObject **pw) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 536 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 537 | Py_complex cval; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 538 | cval.imag = 0.; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 539 | if (PyInt_Check(*pw)) { |
| 540 | cval.real = (double)PyInt_AsLong(*pw); |
| 541 | *pw = PyComplex_FromCComplex(cval); |
| 542 | Py_INCREF(*pv); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 543 | return 0; |
| 544 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 545 | else if (PyLong_Check(*pw)) { |
| 546 | cval.real = PyLong_AsDouble(*pw); |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 547 | if (cval.real == -1.0 && PyErr_Occurred()) |
| 548 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 549 | *pw = PyComplex_FromCComplex(cval); |
| 550 | Py_INCREF(*pv); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 551 | return 0; |
| 552 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 553 | else if (PyFloat_Check(*pw)) { |
| 554 | cval.real = PyFloat_AsDouble(*pw); |
| 555 | *pw = PyComplex_FromCComplex(cval); |
| 556 | Py_INCREF(*pv); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 557 | return 0; |
| 558 | } |
Guido van Rossum | 6380596 | 2001-09-19 01:13:10 +0000 | [diff] [blame] | 559 | else if (PyComplex_Check(*pw)) { |
| 560 | Py_INCREF(*pv); |
| 561 | Py_INCREF(*pw); |
| 562 | return 0; |
| 563 | } |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 564 | return 1; /* Can't do it */ |
| 565 | } |
| 566 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 567 | static PyObject * |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 568 | complex_richcompare(PyObject *v, PyObject *w, int op) |
| 569 | { |
| 570 | int c; |
| 571 | Py_complex i, j; |
| 572 | PyObject *res; |
| 573 | |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 574 | c = PyNumber_CoerceEx(&v, &w); |
| 575 | if (c < 0) |
| 576 | return NULL; |
| 577 | if (c > 0) { |
| 578 | Py_INCREF(Py_NotImplemented); |
| 579 | return Py_NotImplemented; |
| 580 | } |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 581 | /* Make sure both arguments are complex. */ |
| 582 | if (!(PyComplex_Check(v) && PyComplex_Check(w))) { |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 583 | Py_DECREF(v); |
| 584 | Py_DECREF(w); |
| 585 | Py_INCREF(Py_NotImplemented); |
| 586 | return Py_NotImplemented; |
| 587 | } |
| 588 | |
| 589 | i = ((PyComplexObject *)v)->cval; |
| 590 | j = ((PyComplexObject *)w)->cval; |
| 591 | Py_DECREF(v); |
| 592 | Py_DECREF(w); |
| 593 | |
Guido van Rossum | 2205642 | 2001-09-24 17:52:04 +0000 | [diff] [blame] | 594 | if (op != Py_EQ && op != Py_NE) { |
| 595 | PyErr_SetString(PyExc_TypeError, |
| 596 | "cannot compare complex numbers using <, <=, >, >="); |
| 597 | return NULL; |
| 598 | } |
| 599 | |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 600 | if ((i.real == j.real && i.imag == j.imag) == (op == Py_EQ)) |
| 601 | res = Py_True; |
| 602 | else |
| 603 | res = Py_False; |
| 604 | |
| 605 | Py_INCREF(res); |
| 606 | return res; |
| 607 | } |
| 608 | |
| 609 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 610 | complex_int(PyObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 611 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 612 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0970dba | 2003-08-30 23:57:36 +0000 | [diff] [blame] | 613 | "can't convert complex to int; use int(abs(z))"); |
Guido van Rossum | d4ab3cd | 1996-09-11 22:54:37 +0000 | [diff] [blame] | 614 | return NULL; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 617 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 618 | complex_long(PyObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 619 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 620 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0970dba | 2003-08-30 23:57:36 +0000 | [diff] [blame] | 621 | "can't convert complex to long; use long(abs(z))"); |
Guido van Rossum | d4ab3cd | 1996-09-11 22:54:37 +0000 | [diff] [blame] | 622 | return NULL; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 625 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 626 | complex_float(PyObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 627 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 628 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0970dba | 2003-08-30 23:57:36 +0000 | [diff] [blame] | 629 | "can't convert complex to float; use abs(z)"); |
Guido van Rossum | d4ab3cd | 1996-09-11 22:54:37 +0000 | [diff] [blame] | 630 | return NULL; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 633 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 634 | complex_conjugate(PyObject *self) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 635 | { |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 636 | Py_complex c; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 637 | c = ((PyComplexObject *)self)->cval; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 638 | c.imag = -c.imag; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 639 | return PyComplex_FromCComplex(c); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 642 | static PyObject * |
| 643 | complex_getnewargs(PyComplexObject *v) |
| 644 | { |
Guido van Rossum | 4eadfa2 | 2003-03-02 13:51:47 +0000 | [diff] [blame] | 645 | return Py_BuildValue("(D)", &v->cval); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 648 | static PyMethodDef complex_methods[] = { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 649 | {"conjugate", (PyCFunction)complex_conjugate, METH_NOARGS}, |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 650 | {"__getnewargs__", (PyCFunction)complex_getnewargs, METH_NOARGS}, |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 651 | {NULL, NULL} /* sentinel */ |
| 652 | }; |
| 653 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 654 | static PyMemberDef complex_members[] = { |
Guido van Rossum | fa2e4c2 | 2002-02-08 21:26:07 +0000 | [diff] [blame] | 655 | {"real", T_DOUBLE, offsetof(PyComplexObject, cval.real), READONLY, |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 656 | "the real part of a complex number"}, |
Guido van Rossum | fa2e4c2 | 2002-02-08 21:26:07 +0000 | [diff] [blame] | 657 | {"imag", T_DOUBLE, offsetof(PyComplexObject, cval.imag), READONLY, |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 658 | "the imaginary part of a complex number"}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 659 | {0}, |
| 660 | }; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 661 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 662 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 663 | complex_subtype_from_string(PyTypeObject *type, PyObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 664 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 665 | extern double strtod(const char *, char **); |
| 666 | const char *s, *start; |
| 667 | char *end; |
| 668 | double x=0.0, y=0.0, z; |
| 669 | int got_re=0, got_im=0, done=0; |
| 670 | int digit_or_dot; |
| 671 | int sw_error=0; |
| 672 | int sign; |
| 673 | char buffer[256]; /* For errors */ |
Guido van Rossum | 70e3688 | 2001-10-25 18:07:22 +0000 | [diff] [blame] | 674 | #ifdef Py_USING_UNICODE |
| 675 | char s_buffer[256]; |
| 676 | #endif |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 677 | int len; |
| 678 | |
| 679 | if (PyString_Check(v)) { |
| 680 | s = PyString_AS_STRING(v); |
| 681 | len = PyString_GET_SIZE(v); |
| 682 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 683 | #ifdef Py_USING_UNICODE |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 684 | else if (PyUnicode_Check(v)) { |
| 685 | if (PyUnicode_GET_SIZE(v) >= sizeof(s_buffer)) { |
| 686 | PyErr_SetString(PyExc_ValueError, |
| 687 | "complex() literal too large to convert"); |
| 688 | return NULL; |
| 689 | } |
| 690 | if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v), |
| 691 | PyUnicode_GET_SIZE(v), |
| 692 | s_buffer, |
| 693 | NULL)) |
| 694 | return NULL; |
| 695 | s = s_buffer; |
| 696 | len = (int)strlen(s); |
| 697 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 698 | #endif |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 699 | else if (PyObject_AsCharBuffer(v, &s, &len)) { |
| 700 | PyErr_SetString(PyExc_TypeError, |
| 701 | "complex() arg is not a string"); |
| 702 | return NULL; |
| 703 | } |
| 704 | |
| 705 | /* position on first nonblank */ |
| 706 | start = s; |
| 707 | while (*s && isspace(Py_CHARMASK(*s))) |
| 708 | s++; |
| 709 | if (s[0] == '\0') { |
| 710 | PyErr_SetString(PyExc_ValueError, |
| 711 | "complex() arg is an empty string"); |
| 712 | return NULL; |
| 713 | } |
| 714 | |
| 715 | z = -1.0; |
| 716 | sign = 1; |
| 717 | do { |
| 718 | |
| 719 | switch (*s) { |
| 720 | |
| 721 | case '\0': |
| 722 | if (s-start != len) { |
| 723 | PyErr_SetString( |
| 724 | PyExc_ValueError, |
| 725 | "complex() arg contains a null byte"); |
| 726 | return NULL; |
| 727 | } |
| 728 | if(!done) sw_error=1; |
| 729 | break; |
| 730 | |
| 731 | case '-': |
| 732 | sign = -1; |
| 733 | /* Fallthrough */ |
| 734 | case '+': |
| 735 | if (done) sw_error=1; |
| 736 | s++; |
| 737 | if ( *s=='\0'||*s=='+'||*s=='-' || |
| 738 | isspace(Py_CHARMASK(*s)) ) sw_error=1; |
| 739 | break; |
| 740 | |
| 741 | case 'J': |
| 742 | case 'j': |
| 743 | if (got_im || done) { |
| 744 | sw_error = 1; |
| 745 | break; |
| 746 | } |
| 747 | if (z<0.0) { |
| 748 | y=sign; |
| 749 | } |
| 750 | else{ |
| 751 | y=sign*z; |
| 752 | } |
| 753 | got_im=1; |
| 754 | s++; |
| 755 | if (*s!='+' && *s!='-' ) |
| 756 | done=1; |
| 757 | break; |
| 758 | |
| 759 | default: |
| 760 | if (isspace(Py_CHARMASK(*s))) { |
| 761 | while (*s && isspace(Py_CHARMASK(*s))) |
| 762 | s++; |
| 763 | if (s[0] != '\0') |
| 764 | sw_error=1; |
| 765 | else |
| 766 | done = 1; |
| 767 | break; |
| 768 | } |
| 769 | digit_or_dot = |
| 770 | (*s=='.' || isdigit(Py_CHARMASK(*s))); |
| 771 | if (done||!digit_or_dot) { |
| 772 | sw_error=1; |
| 773 | break; |
| 774 | } |
| 775 | errno = 0; |
| 776 | PyFPE_START_PROTECT("strtod", return 0) |
| 777 | z = strtod(s, &end) ; |
| 778 | PyFPE_END_PROTECT(z) |
| 779 | if (errno != 0) { |
Barry Warsaw | 01d697a | 2001-11-28 20:50:56 +0000 | [diff] [blame] | 780 | PyOS_snprintf(buffer, sizeof(buffer), |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 781 | "float() out of range: %.150s", s); |
| 782 | PyErr_SetString( |
| 783 | PyExc_ValueError, |
| 784 | buffer); |
| 785 | return NULL; |
| 786 | } |
| 787 | s=end; |
| 788 | if (*s=='J' || *s=='j') { |
| 789 | |
| 790 | break; |
| 791 | } |
| 792 | if (got_re) { |
| 793 | sw_error=1; |
| 794 | break; |
| 795 | } |
| 796 | |
| 797 | /* accept a real part */ |
| 798 | x=sign*z; |
| 799 | got_re=1; |
| 800 | if (got_im) done=1; |
| 801 | z = -1.0; |
| 802 | sign = 1; |
| 803 | break; |
| 804 | |
| 805 | } /* end of switch */ |
| 806 | |
Tim Peters | 077f271 | 2002-04-14 22:04:03 +0000 | [diff] [blame] | 807 | } while (s - start < len && !sw_error); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 808 | |
| 809 | if (sw_error) { |
| 810 | PyErr_SetString(PyExc_ValueError, |
| 811 | "complex() arg is a malformed string"); |
| 812 | return NULL; |
| 813 | } |
| 814 | |
| 815 | return complex_subtype_from_doubles(type, x, y); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 818 | static PyObject * |
| 819 | complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 820 | { |
Raymond Hettinger | 478d47a | 2002-06-06 15:45:38 +0000 | [diff] [blame] | 821 | PyObject *r, *i, *tmp, *f; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 822 | PyNumberMethods *nbr, *nbi = NULL; |
| 823 | Py_complex cr, ci; |
| 824 | int own_r = 0; |
Raymond Hettinger | 478d47a | 2002-06-06 15:45:38 +0000 | [diff] [blame] | 825 | static PyObject *complexstr; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 826 | static char *kwlist[] = {"real", "imag", 0}; |
| 827 | |
| 828 | r = Py_False; |
| 829 | i = NULL; |
| 830 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:complex", kwlist, |
| 831 | &r, &i)) |
| 832 | return NULL; |
Raymond Hettinger | 604cd6a | 2002-08-29 14:22:51 +0000 | [diff] [blame] | 833 | |
Walter Dörwald | 5ecd6c4 | 2003-06-17 20:22:24 +0000 | [diff] [blame] | 834 | /* Special-case for single argument that is already complex */ |
Guido van Rossum | 4eadfa2 | 2003-03-02 13:51:47 +0000 | [diff] [blame] | 835 | if (PyComplex_CheckExact(r) && i == NULL && |
| 836 | type == &PyComplex_Type) { |
Raymond Hettinger | 604cd6a | 2002-08-29 14:22:51 +0000 | [diff] [blame] | 837 | /* Note that we can't know whether it's safe to return |
| 838 | a complex *subclass* instance as-is, hence the restriction |
| 839 | to exact complexes here. */ |
| 840 | Py_INCREF(r); |
| 841 | return r; |
| 842 | } |
Fred Drake | 526c7a0 | 2001-12-13 19:52:22 +0000 | [diff] [blame] | 843 | if (PyString_Check(r) || PyUnicode_Check(r)) { |
| 844 | if (i != NULL) { |
| 845 | PyErr_SetString(PyExc_TypeError, |
| 846 | "complex() can't take second arg" |
| 847 | " if first is a string"); |
| 848 | return NULL; |
| 849 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 850 | return complex_subtype_from_string(type, r); |
Fred Drake | 526c7a0 | 2001-12-13 19:52:22 +0000 | [diff] [blame] | 851 | } |
| 852 | if (i != NULL && (PyString_Check(i) || PyUnicode_Check(i))) { |
| 853 | PyErr_SetString(PyExc_TypeError, |
| 854 | "complex() second arg can't be a string"); |
| 855 | return NULL; |
| 856 | } |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 857 | |
Raymond Hettinger | 478d47a | 2002-06-06 15:45:38 +0000 | [diff] [blame] | 858 | /* XXX Hack to support classes with __complex__ method */ |
| 859 | if (complexstr == NULL) { |
| 860 | complexstr = PyString_InternFromString("__complex__"); |
| 861 | if (complexstr == NULL) |
| 862 | return NULL; |
| 863 | } |
| 864 | f = PyObject_GetAttr(r, complexstr); |
| 865 | if (f == NULL) |
| 866 | PyErr_Clear(); |
| 867 | else { |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 868 | PyObject *args = PyTuple_New(0); |
Raymond Hettinger | 478d47a | 2002-06-06 15:45:38 +0000 | [diff] [blame] | 869 | if (args == NULL) |
| 870 | return NULL; |
| 871 | r = PyEval_CallObject(f, args); |
| 872 | Py_DECREF(args); |
| 873 | Py_DECREF(f); |
| 874 | if (r == NULL) |
| 875 | return NULL; |
| 876 | own_r = 1; |
| 877 | } |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 878 | nbr = r->ob_type->tp_as_number; |
| 879 | if (i != NULL) |
| 880 | nbi = i->ob_type->tp_as_number; |
| 881 | if (nbr == NULL || nbr->nb_float == NULL || |
| 882 | ((i != NULL) && (nbi == NULL || nbi->nb_float == NULL))) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 883 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 478d47a | 2002-06-06 15:45:38 +0000 | [diff] [blame] | 884 | "complex() argument must be a string or a number"); |
Tim Peters | 465fa3d | 2003-08-15 01:16:37 +0000 | [diff] [blame] | 885 | if (own_r) { |
| 886 | Py_DECREF(r); |
| 887 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 888 | return NULL; |
| 889 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 890 | if (PyComplex_Check(r)) { |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 891 | /* Note that if r is of a complex subtype, we're only |
| 892 | retaining its real & imag parts here, and the return |
| 893 | value is (properly) of the builtin complex type. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 894 | cr = ((PyComplexObject*)r)->cval; |
| 895 | if (own_r) { |
| 896 | Py_DECREF(r); |
| 897 | } |
| 898 | } |
| 899 | else { |
| 900 | tmp = PyNumber_Float(r); |
| 901 | if (own_r) { |
| 902 | Py_DECREF(r); |
| 903 | } |
| 904 | if (tmp == NULL) |
| 905 | return NULL; |
| 906 | if (!PyFloat_Check(tmp)) { |
| 907 | PyErr_SetString(PyExc_TypeError, |
| 908 | "float(r) didn't return a float"); |
| 909 | Py_DECREF(tmp); |
| 910 | return NULL; |
| 911 | } |
| 912 | cr.real = PyFloat_AsDouble(tmp); |
| 913 | Py_DECREF(tmp); |
| 914 | cr.imag = 0.0; |
| 915 | } |
| 916 | if (i == NULL) { |
| 917 | ci.real = 0.0; |
| 918 | ci.imag = 0.0; |
| 919 | } |
| 920 | else if (PyComplex_Check(i)) |
| 921 | ci = ((PyComplexObject*)i)->cval; |
| 922 | else { |
| 923 | tmp = (*nbi->nb_float)(i); |
| 924 | if (tmp == NULL) |
| 925 | return NULL; |
| 926 | ci.real = PyFloat_AsDouble(tmp); |
| 927 | Py_DECREF(tmp); |
| 928 | ci.imag = 0.; |
| 929 | } |
| 930 | cr.real -= ci.imag; |
| 931 | cr.imag += ci.real; |
| 932 | return complex_subtype_from_c_complex(type, cr); |
| 933 | } |
| 934 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 935 | PyDoc_STRVAR(complex_doc, |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 936 | "complex(real[, imag]) -> complex number\n" |
| 937 | "\n" |
| 938 | "Create a complex number from a real part and an optional imaginary part.\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 939 | "This is equivalent to (real + imag*1j) where imag defaults to 0."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 940 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 941 | static PyNumberMethods complex_as_number = { |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 942 | (binaryfunc)complex_add, /* nb_add */ |
| 943 | (binaryfunc)complex_sub, /* nb_subtract */ |
| 944 | (binaryfunc)complex_mul, /* nb_multiply */ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 945 | (binaryfunc)complex_classic_div, /* nb_divide */ |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 946 | (binaryfunc)complex_remainder, /* nb_remainder */ |
| 947 | (binaryfunc)complex_divmod, /* nb_divmod */ |
| 948 | (ternaryfunc)complex_pow, /* nb_power */ |
| 949 | (unaryfunc)complex_neg, /* nb_negative */ |
| 950 | (unaryfunc)complex_pos, /* nb_positive */ |
| 951 | (unaryfunc)complex_abs, /* nb_absolute */ |
| 952 | (inquiry)complex_nonzero, /* nb_nonzero */ |
| 953 | 0, /* nb_invert */ |
| 954 | 0, /* nb_lshift */ |
| 955 | 0, /* nb_rshift */ |
| 956 | 0, /* nb_and */ |
| 957 | 0, /* nb_xor */ |
| 958 | 0, /* nb_or */ |
| 959 | (coercion)complex_coerce, /* nb_coerce */ |
| 960 | (unaryfunc)complex_int, /* nb_int */ |
| 961 | (unaryfunc)complex_long, /* nb_long */ |
| 962 | (unaryfunc)complex_float, /* nb_float */ |
| 963 | 0, /* nb_oct */ |
| 964 | 0, /* nb_hex */ |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 965 | 0, /* nb_inplace_add */ |
| 966 | 0, /* nb_inplace_subtract */ |
| 967 | 0, /* nb_inplace_multiply*/ |
| 968 | 0, /* nb_inplace_divide */ |
| 969 | 0, /* nb_inplace_remainder */ |
| 970 | 0, /* nb_inplace_power */ |
| 971 | 0, /* nb_inplace_lshift */ |
| 972 | 0, /* nb_inplace_rshift */ |
| 973 | 0, /* nb_inplace_and */ |
| 974 | 0, /* nb_inplace_xor */ |
| 975 | 0, /* nb_inplace_or */ |
| 976 | (binaryfunc)complex_int_div, /* nb_floor_divide */ |
| 977 | (binaryfunc)complex_div, /* nb_true_divide */ |
| 978 | 0, /* nb_inplace_floor_divide */ |
| 979 | 0, /* nb_inplace_true_divide */ |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 980 | }; |
| 981 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 982 | PyTypeObject PyComplex_Type = { |
| 983 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 984 | 0, |
| 985 | "complex", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 986 | sizeof(PyComplexObject), |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 987 | 0, |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 988 | (destructor)complex_dealloc, /* tp_dealloc */ |
| 989 | (printfunc)complex_print, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 990 | 0, /* tp_getattr */ |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 991 | 0, /* tp_setattr */ |
| 992 | 0, /* tp_compare */ |
| 993 | (reprfunc)complex_repr, /* tp_repr */ |
| 994 | &complex_as_number, /* tp_as_number */ |
| 995 | 0, /* tp_as_sequence */ |
| 996 | 0, /* tp_as_mapping */ |
| 997 | (hashfunc)complex_hash, /* tp_hash */ |
| 998 | 0, /* tp_call */ |
Tim Peters | 7069512 | 2001-03-11 08:37:29 +0000 | [diff] [blame] | 999 | (reprfunc)complex_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1000 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 1001 | 0, /* tp_setattro */ |
| 1002 | 0, /* tp_as_buffer */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1003 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 1004 | complex_doc, /* tp_doc */ |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 1005 | 0, /* tp_traverse */ |
| 1006 | 0, /* tp_clear */ |
| 1007 | complex_richcompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1008 | 0, /* tp_weaklistoffset */ |
| 1009 | 0, /* tp_iter */ |
| 1010 | 0, /* tp_iternext */ |
| 1011 | complex_methods, /* tp_methods */ |
| 1012 | complex_members, /* tp_members */ |
| 1013 | 0, /* tp_getset */ |
| 1014 | 0, /* tp_base */ |
| 1015 | 0, /* tp_dict */ |
| 1016 | 0, /* tp_descr_get */ |
| 1017 | 0, /* tp_descr_set */ |
| 1018 | 0, /* tp_dictoffset */ |
| 1019 | 0, /* tp_init */ |
| 1020 | 0, /* tp_alloc */ |
| 1021 | complex_new, /* tp_new */ |
Neil Schemenauer | aa769ae | 2002-04-12 02:44:10 +0000 | [diff] [blame] | 1022 | PyObject_Del, /* tp_free */ |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 1023 | }; |
| 1024 | |
| 1025 | #endif |