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 | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 11 | /* elementary operations on complex numbers */ |
| 12 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 13 | static Py_complex c_1 = {1., 0.}; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 14 | |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 15 | Py_complex |
| 16 | c_sum(Py_complex a, Py_complex b) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 17 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 18 | Py_complex r; |
| 19 | r.real = a.real + b.real; |
| 20 | r.imag = a.imag + b.imag; |
| 21 | return r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 22 | } |
| 23 | |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 24 | Py_complex |
| 25 | c_diff(Py_complex a, Py_complex b) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 26 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 27 | Py_complex r; |
| 28 | r.real = a.real - b.real; |
| 29 | r.imag = a.imag - b.imag; |
| 30 | return r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 33 | Py_complex |
| 34 | c_neg(Py_complex a) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 35 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 36 | Py_complex r; |
| 37 | r.real = -a.real; |
| 38 | r.imag = -a.imag; |
| 39 | return r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 42 | Py_complex |
| 43 | c_prod(Py_complex a, Py_complex b) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 44 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 45 | Py_complex r; |
| 46 | r.real = a.real*b.real - a.imag*b.imag; |
| 47 | r.imag = a.real*b.imag + a.imag*b.real; |
| 48 | return r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 51 | Py_complex |
| 52 | c_quot(Py_complex a, Py_complex b) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 53 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 54 | /****************************************************************** |
| 55 | This was the original algorithm. It's grossly prone to spurious |
| 56 | overflow and underflow errors. It also merrily divides by 0 despite |
| 57 | checking for that(!). The code still serves a doc purpose here, as |
| 58 | the algorithm following is a simple by-cases transformation of this |
| 59 | one: |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 60 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 61 | Py_complex r; |
| 62 | double d = b.real*b.real + b.imag*b.imag; |
| 63 | if (d == 0.) |
| 64 | errno = EDOM; |
| 65 | r.real = (a.real*b.real + a.imag*b.imag)/d; |
| 66 | r.imag = (a.imag*b.real - a.real*b.imag)/d; |
| 67 | return r; |
| 68 | ******************************************************************/ |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 69 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 70 | /* This algorithm is better, and is pretty obvious: first divide the |
| 71 | * numerators and denominator by whichever of {b.real, b.imag} has |
| 72 | * larger magnitude. The earliest reference I found was to CACM |
| 73 | * Algorithm 116 (Complex Division, Robert L. Smith, Stanford |
| 74 | * University). As usual, though, we're still ignoring all IEEE |
| 75 | * endcases. |
| 76 | */ |
| 77 | Py_complex r; /* the result */ |
| 78 | const double abs_breal = b.real < 0 ? -b.real : b.real; |
| 79 | const double abs_bimag = b.imag < 0 ? -b.imag : b.imag; |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 80 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 81 | if (abs_breal >= abs_bimag) { |
| 82 | /* divide tops and bottom by b.real */ |
| 83 | if (abs_breal == 0.0) { |
| 84 | errno = EDOM; |
| 85 | r.real = r.imag = 0.0; |
| 86 | } |
| 87 | else { |
| 88 | const double ratio = b.imag / b.real; |
| 89 | const double denom = b.real + b.imag * ratio; |
| 90 | r.real = (a.real + a.imag * ratio) / denom; |
| 91 | r.imag = (a.imag - a.real * ratio) / denom; |
| 92 | } |
| 93 | } |
| 94 | else { |
| 95 | /* divide tops and bottom by b.imag */ |
| 96 | const double ratio = b.real / b.imag; |
| 97 | const double denom = b.real * ratio + b.imag; |
| 98 | assert(b.imag != 0.0); |
| 99 | r.real = (a.real * ratio + a.imag) / denom; |
| 100 | r.imag = (a.imag * ratio - a.real) / denom; |
| 101 | } |
| 102 | return r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 105 | Py_complex |
| 106 | c_pow(Py_complex a, Py_complex b) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 107 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 108 | Py_complex r; |
| 109 | double vabs,len,at,phase; |
| 110 | if (b.real == 0. && b.imag == 0.) { |
| 111 | r.real = 1.; |
| 112 | r.imag = 0.; |
| 113 | } |
| 114 | else if (a.real == 0. && a.imag == 0.) { |
| 115 | if (b.imag != 0. || b.real < 0.) |
| 116 | errno = EDOM; |
| 117 | r.real = 0.; |
| 118 | r.imag = 0.; |
| 119 | } |
| 120 | else { |
| 121 | vabs = hypot(a.real,a.imag); |
| 122 | len = pow(vabs,b.real); |
| 123 | at = atan2(a.imag, a.real); |
| 124 | phase = at*b.real; |
| 125 | if (b.imag != 0.0) { |
| 126 | len /= exp(at*b.imag); |
| 127 | phase += b.imag*log(vabs); |
| 128 | } |
| 129 | r.real = len*cos(phase); |
| 130 | r.imag = len*sin(phase); |
| 131 | } |
| 132 | return r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 135 | static Py_complex |
| 136 | c_powu(Py_complex x, long n) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 137 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 138 | Py_complex r, p; |
| 139 | long mask = 1; |
| 140 | r = c_1; |
| 141 | p = x; |
| 142 | while (mask > 0 && n >= mask) { |
| 143 | if (n & mask) |
| 144 | r = c_prod(r,p); |
| 145 | mask <<= 1; |
| 146 | p = c_prod(p,p); |
| 147 | } |
| 148 | return r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 151 | static Py_complex |
| 152 | c_powi(Py_complex x, long n) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 153 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 154 | Py_complex cn; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 155 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 156 | if (n > 100 || n < -100) { |
| 157 | cn.real = (double) n; |
| 158 | cn.imag = 0.; |
| 159 | return c_pow(x,cn); |
| 160 | } |
| 161 | else if (n > 0) |
| 162 | return c_powu(x,n); |
| 163 | else |
| 164 | return c_quot(c_1,c_powu(x,-n)); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 165 | |
| 166 | } |
| 167 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 168 | double |
| 169 | c_abs(Py_complex z) |
| 170 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 171 | /* sets errno = ERANGE on overflow; otherwise errno = 0 */ |
| 172 | double result; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 174 | if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) { |
| 175 | /* C99 rules: if either the real or the imaginary part is an |
| 176 | infinity, return infinity, even if the other part is a |
| 177 | NaN. */ |
| 178 | if (Py_IS_INFINITY(z.real)) { |
| 179 | result = fabs(z.real); |
| 180 | errno = 0; |
| 181 | return result; |
| 182 | } |
| 183 | if (Py_IS_INFINITY(z.imag)) { |
| 184 | result = fabs(z.imag); |
| 185 | errno = 0; |
| 186 | return result; |
| 187 | } |
| 188 | /* either the real or imaginary part is a NaN, |
| 189 | and neither is infinite. Result should be NaN. */ |
| 190 | return Py_NAN; |
| 191 | } |
| 192 | result = hypot(z.real, z.imag); |
| 193 | if (!Py_IS_FINITE(result)) |
| 194 | errno = ERANGE; |
| 195 | else |
| 196 | errno = 0; |
| 197 | return result; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 200 | static PyObject * |
| 201 | complex_subtype_from_c_complex(PyTypeObject *type, Py_complex cval) |
| 202 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 203 | PyObject *op; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 204 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 205 | op = type->tp_alloc(type, 0); |
| 206 | if (op != NULL) |
| 207 | ((PyComplexObject *)op)->cval = cval; |
| 208 | return op; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 211 | PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 212 | PyComplex_FromCComplex(Py_complex cval) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 213 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 214 | register PyComplexObject *op; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 215 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 216 | /* Inline PyObject_New */ |
| 217 | op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject)); |
| 218 | if (op == NULL) |
| 219 | return PyErr_NoMemory(); |
| 220 | PyObject_INIT(op, &PyComplex_Type); |
| 221 | op->cval = cval; |
| 222 | return (PyObject *) op; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 225 | static PyObject * |
| 226 | complex_subtype_from_doubles(PyTypeObject *type, double real, double imag) |
| 227 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 228 | Py_complex c; |
| 229 | c.real = real; |
| 230 | c.imag = imag; |
| 231 | return complex_subtype_from_c_complex(type, c); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 234 | PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 235 | PyComplex_FromDoubles(double real, double imag) |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 236 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 237 | Py_complex c; |
| 238 | c.real = real; |
| 239 | c.imag = imag; |
| 240 | return PyComplex_FromCComplex(c); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | double |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 244 | PyComplex_RealAsDouble(PyObject *op) |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 245 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 | if (PyComplex_Check(op)) { |
| 247 | return ((PyComplexObject *)op)->cval.real; |
| 248 | } |
| 249 | else { |
| 250 | return PyFloat_AsDouble(op); |
| 251 | } |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | double |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 255 | PyComplex_ImagAsDouble(PyObject *op) |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 256 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 257 | if (PyComplex_Check(op)) { |
| 258 | return ((PyComplexObject *)op)->cval.imag; |
| 259 | } |
| 260 | else { |
| 261 | return 0.0; |
| 262 | } |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Benjamin Peterson | aea4428 | 2010-01-04 01:10:28 +0000 | [diff] [blame] | 265 | static PyObject * |
| 266 | try_complex_special_method(PyObject *op) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 267 | PyObject *f; |
| 268 | static PyObject *complexstr; |
Benjamin Peterson | aea4428 | 2010-01-04 01:10:28 +0000 | [diff] [blame] | 269 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 270 | f = _PyObject_LookupSpecial(op, "__complex__", &complexstr); |
| 271 | if (f) { |
| 272 | PyObject *res = PyObject_CallFunctionObjArgs(f, NULL); |
| 273 | Py_DECREF(f); |
| 274 | return res; |
| 275 | } |
| 276 | return NULL; |
Benjamin Peterson | aea4428 | 2010-01-04 01:10:28 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 279 | Py_complex |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 280 | PyComplex_AsCComplex(PyObject *op) |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 281 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 282 | Py_complex cv; |
| 283 | PyObject *newop = NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 285 | assert(op); |
| 286 | /* If op is already of type PyComplex_Type, return its value */ |
| 287 | if (PyComplex_Check(op)) { |
| 288 | return ((PyComplexObject *)op)->cval; |
| 289 | } |
| 290 | /* If not, use op's __complex__ method, if it exists */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 291 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 292 | /* return -1 on failure */ |
| 293 | cv.real = -1.; |
| 294 | cv.imag = 0.; |
| 295 | |
| 296 | newop = try_complex_special_method(op); |
| 297 | |
| 298 | if (newop) { |
| 299 | if (!PyComplex_Check(newop)) { |
| 300 | PyErr_SetString(PyExc_TypeError, |
| 301 | "__complex__ should return a complex object"); |
| 302 | Py_DECREF(newop); |
| 303 | return cv; |
| 304 | } |
| 305 | cv = ((PyComplexObject *)newop)->cval; |
| 306 | Py_DECREF(newop); |
| 307 | return cv; |
| 308 | } |
| 309 | else if (PyErr_Occurred()) { |
| 310 | return cv; |
| 311 | } |
| 312 | /* If neither of the above works, interpret op as a float giving the |
| 313 | real part of the result, and fill in the imaginary part as 0. */ |
| 314 | else { |
| 315 | /* PyFloat_AsDouble will return -1 on failure */ |
| 316 | cv.real = PyFloat_AsDouble(op); |
| 317 | return cv; |
| 318 | } |
Guido van Rossum | cf3d108 | 1996-01-12 01:21:14 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 321 | static void |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 322 | complex_dealloc(PyObject *op) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 323 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 324 | op->ob_type->tp_free(op); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 327 | static PyObject * |
Eric Smith | 70099a1 | 2010-12-04 13:27:34 +0000 | [diff] [blame] | 328 | complex_repr(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 329 | { |
Eric Smith | 70099a1 | 2010-12-04 13:27:34 +0000 | [diff] [blame] | 330 | int precision = 0; |
| 331 | char format_code = 'r'; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 332 | PyObject *result = NULL; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 333 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 334 | /* If these are non-NULL, they'll need to be freed. */ |
| 335 | char *pre = NULL; |
| 336 | char *im = NULL; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 337 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 338 | /* These do not need to be freed. re is either an alias |
| 339 | for pre or a pointer to a constant. lead and tail |
| 340 | are pointers to constants. */ |
| 341 | char *re = NULL; |
| 342 | char *lead = ""; |
| 343 | char *tail = ""; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 344 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 345 | if (v->cval.real == 0. && copysign(1.0, v->cval.real)==1.0) { |
Eric Smith | 70099a1 | 2010-12-04 13:27:34 +0000 | [diff] [blame] | 346 | /* Real part is +0: just output the imaginary part and do not |
| 347 | include parens. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 348 | re = ""; |
| 349 | im = PyOS_double_to_string(v->cval.imag, format_code, |
| 350 | precision, 0, NULL); |
| 351 | if (!im) { |
| 352 | PyErr_NoMemory(); |
| 353 | goto done; |
| 354 | } |
| 355 | } else { |
Eric Smith | 70099a1 | 2010-12-04 13:27:34 +0000 | [diff] [blame] | 356 | /* Format imaginary part with sign, real part without. Include |
| 357 | parens in the result. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 358 | pre = PyOS_double_to_string(v->cval.real, format_code, |
| 359 | precision, 0, NULL); |
| 360 | if (!pre) { |
| 361 | PyErr_NoMemory(); |
| 362 | goto done; |
| 363 | } |
| 364 | re = pre; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 365 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 366 | im = PyOS_double_to_string(v->cval.imag, format_code, |
| 367 | precision, Py_DTSF_SIGN, NULL); |
| 368 | if (!im) { |
| 369 | PyErr_NoMemory(); |
| 370 | goto done; |
| 371 | } |
| 372 | lead = "("; |
| 373 | tail = ")"; |
| 374 | } |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 375 | result = PyUnicode_FromFormat("%s%s%sj%s", lead, re, im, tail); |
Mark Dickinson | ad476da | 2009-04-23 19:14:16 +0000 | [diff] [blame] | 376 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 377 | PyMem_Free(im); |
| 378 | PyMem_Free(pre); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 379 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 380 | return result; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 383 | static Py_hash_t |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 384 | complex_hash(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 385 | { |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 386 | Py_uhash_t hashreal, hashimag, combined; |
| 387 | hashreal = (Py_uhash_t)_Py_HashDouble(v->cval.real); |
| 388 | if (hashreal == (Py_uhash_t)-1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | return -1; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 390 | hashimag = (Py_uhash_t)_Py_HashDouble(v->cval.imag); |
| 391 | if (hashimag == (Py_uhash_t)-1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 392 | return -1; |
| 393 | /* Note: if the imaginary part is 0, hashimag is 0 now, |
| 394 | * so the following returns hashreal unchanged. This is |
| 395 | * important because numbers of different types that |
| 396 | * compare equal must have the same hash value, so that |
| 397 | * hash(x + 0*j) must equal hash(x). |
| 398 | */ |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 399 | combined = hashreal + _PyHASH_IMAG * hashimag; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 400 | if (combined == (Py_uhash_t)-1) |
| 401 | combined = (Py_uhash_t)-2; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 402 | return (Py_hash_t)combined; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 405 | /* This macro may return! */ |
| 406 | #define TO_COMPLEX(obj, c) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 407 | if (PyComplex_Check(obj)) \ |
| 408 | c = ((PyComplexObject *)(obj))->cval; \ |
| 409 | else if (to_complex(&(obj), &(c)) < 0) \ |
| 410 | return (obj) |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 411 | |
| 412 | static int |
| 413 | to_complex(PyObject **pobj, Py_complex *pc) |
| 414 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 415 | PyObject *obj = *pobj; |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 416 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 417 | pc->real = pc->imag = 0.0; |
| 418 | if (PyLong_Check(obj)) { |
| 419 | pc->real = PyLong_AsDouble(obj); |
| 420 | if (pc->real == -1.0 && PyErr_Occurred()) { |
| 421 | *pobj = NULL; |
| 422 | return -1; |
| 423 | } |
| 424 | return 0; |
| 425 | } |
| 426 | if (PyFloat_Check(obj)) { |
| 427 | pc->real = PyFloat_AsDouble(obj); |
| 428 | return 0; |
| 429 | } |
| 430 | Py_INCREF(Py_NotImplemented); |
| 431 | *pobj = Py_NotImplemented; |
| 432 | return -1; |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 433 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 434 | |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 435 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 436 | static PyObject * |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 437 | complex_add(PyObject *v, PyObject *w) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 438 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 439 | Py_complex result; |
| 440 | Py_complex a, b; |
| 441 | TO_COMPLEX(v, a); |
| 442 | TO_COMPLEX(w, b); |
| 443 | PyFPE_START_PROTECT("complex_add", return 0) |
| 444 | result = c_sum(a, b); |
| 445 | PyFPE_END_PROTECT(result) |
| 446 | return PyComplex_FromCComplex(result); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 449 | static PyObject * |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 450 | complex_sub(PyObject *v, PyObject *w) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 451 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 452 | Py_complex result; |
| 453 | Py_complex a, b; |
| 454 | TO_COMPLEX(v, a); |
| 455 | TO_COMPLEX(w, b); |
| 456 | PyFPE_START_PROTECT("complex_sub", return 0) |
| 457 | result = c_diff(a, b); |
| 458 | PyFPE_END_PROTECT(result) |
| 459 | return PyComplex_FromCComplex(result); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 462 | static PyObject * |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 463 | complex_mul(PyObject *v, PyObject *w) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 464 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 465 | Py_complex result; |
| 466 | Py_complex a, b; |
| 467 | TO_COMPLEX(v, a); |
| 468 | TO_COMPLEX(w, b); |
| 469 | PyFPE_START_PROTECT("complex_mul", return 0) |
| 470 | result = c_prod(a, b); |
| 471 | PyFPE_END_PROTECT(result) |
| 472 | return PyComplex_FromCComplex(result); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 475 | static PyObject * |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 476 | complex_div(PyObject *v, PyObject *w) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 477 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 478 | Py_complex quot; |
| 479 | Py_complex a, b; |
| 480 | TO_COMPLEX(v, a); |
| 481 | TO_COMPLEX(w, b); |
| 482 | PyFPE_START_PROTECT("complex_div", return 0) |
| 483 | errno = 0; |
| 484 | quot = c_quot(a, b); |
| 485 | PyFPE_END_PROTECT(quot) |
| 486 | if (errno == EDOM) { |
| 487 | PyErr_SetString(PyExc_ZeroDivisionError, "complex division by zero"); |
| 488 | return NULL; |
| 489 | } |
| 490 | return PyComplex_FromCComplex(quot); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 493 | static PyObject * |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 494 | complex_remainder(PyObject *v, PyObject *w) |
Guido van Rossum | ee09fc1 | 1996-09-11 13:55:55 +0000 | [diff] [blame] | 495 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 496 | PyErr_SetString(PyExc_TypeError, |
| 497 | "can't mod complex numbers."); |
| 498 | return NULL; |
Guido van Rossum | ee09fc1 | 1996-09-11 13:55:55 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Guido van Rossum | ee09fc1 | 1996-09-11 13:55:55 +0000 | [diff] [blame] | 501 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 502 | static PyObject * |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 503 | complex_divmod(PyObject *v, PyObject *w) |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 504 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 505 | PyErr_SetString(PyExc_TypeError, |
| 506 | "can't take floor or mod of complex number."); |
| 507 | return NULL; |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 508 | } |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 509 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 510 | static PyObject * |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 511 | complex_pow(PyObject *v, PyObject *w, PyObject *z) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 512 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 513 | Py_complex p; |
| 514 | Py_complex exponent; |
| 515 | long int_exponent; |
| 516 | Py_complex a, b; |
| 517 | TO_COMPLEX(v, a); |
| 518 | TO_COMPLEX(w, b); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 519 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 520 | if (z != Py_None) { |
| 521 | PyErr_SetString(PyExc_ValueError, "complex modulo"); |
| 522 | return NULL; |
| 523 | } |
| 524 | PyFPE_START_PROTECT("complex_pow", return 0) |
| 525 | errno = 0; |
| 526 | exponent = b; |
| 527 | int_exponent = (long)exponent.real; |
| 528 | if (exponent.imag == 0. && exponent.real == int_exponent) |
| 529 | p = c_powi(a, int_exponent); |
| 530 | else |
| 531 | p = c_pow(a, exponent); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 532 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 533 | PyFPE_END_PROTECT(p) |
| 534 | Py_ADJUST_ERANGE2(p.real, p.imag); |
| 535 | if (errno == EDOM) { |
| 536 | PyErr_SetString(PyExc_ZeroDivisionError, |
| 537 | "0.0 to a negative or complex power"); |
| 538 | return NULL; |
| 539 | } |
| 540 | else if (errno == ERANGE) { |
| 541 | PyErr_SetString(PyExc_OverflowError, |
| 542 | "complex exponentiation"); |
| 543 | return NULL; |
| 544 | } |
| 545 | return PyComplex_FromCComplex(p); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 548 | static PyObject * |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 549 | complex_int_div(PyObject *v, PyObject *w) |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 550 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 551 | PyErr_SetString(PyExc_TypeError, |
| 552 | "can't take floor of complex number."); |
| 553 | return NULL; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 557 | complex_neg(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 558 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 559 | Py_complex neg; |
| 560 | neg.real = -v->cval.real; |
| 561 | neg.imag = -v->cval.imag; |
| 562 | return PyComplex_FromCComplex(neg); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 565 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 566 | complex_pos(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 567 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 568 | if (PyComplex_CheckExact(v)) { |
| 569 | Py_INCREF(v); |
| 570 | return (PyObject *)v; |
| 571 | } |
| 572 | else |
| 573 | return PyComplex_FromCComplex(v->cval); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 576 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 577 | complex_abs(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 578 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 579 | double result; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 580 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 581 | PyFPE_START_PROTECT("complex_abs", return 0) |
| 582 | result = c_abs(v->cval); |
| 583 | PyFPE_END_PROTECT(result) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 584 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 585 | if (errno == ERANGE) { |
| 586 | PyErr_SetString(PyExc_OverflowError, |
| 587 | "absolute value too large"); |
| 588 | return NULL; |
| 589 | } |
| 590 | return PyFloat_FromDouble(result); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 594 | complex_bool(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 595 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 596 | return v->cval.real != 0.0 || v->cval.imag != 0.0; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 599 | static PyObject * |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 600 | complex_richcompare(PyObject *v, PyObject *w, int op) |
| 601 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 602 | PyObject *res; |
Mark Dickinson | cc6a982 | 2010-05-21 14:55:26 +0000 | [diff] [blame] | 603 | Py_complex i; |
| 604 | int equal; |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 605 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 606 | if (op != Py_EQ && op != Py_NE) { |
Mark Dickinson | cc6a982 | 2010-05-21 14:55:26 +0000 | [diff] [blame] | 607 | goto Unimplemented; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 608 | } |
Guido van Rossum | 2205642 | 2001-09-24 17:52:04 +0000 | [diff] [blame] | 609 | |
Mark Dickinson | cc6a982 | 2010-05-21 14:55:26 +0000 | [diff] [blame] | 610 | assert(PyComplex_Check(v)); |
| 611 | TO_COMPLEX(v, i); |
| 612 | |
| 613 | if (PyLong_Check(w)) { |
| 614 | /* Check for 0.0 imaginary part first to avoid the rich |
| 615 | * comparison when possible. |
| 616 | */ |
| 617 | if (i.imag == 0.0) { |
| 618 | PyObject *j, *sub_res; |
| 619 | j = PyFloat_FromDouble(i.real); |
| 620 | if (j == NULL) |
| 621 | return NULL; |
| 622 | |
| 623 | sub_res = PyObject_RichCompare(j, w, op); |
| 624 | Py_DECREF(j); |
| 625 | return sub_res; |
| 626 | } |
| 627 | else { |
| 628 | equal = 0; |
| 629 | } |
| 630 | } |
| 631 | else if (PyFloat_Check(w)) { |
| 632 | equal = (i.real == PyFloat_AsDouble(w) && i.imag == 0.0); |
| 633 | } |
| 634 | else if (PyComplex_Check(w)) { |
| 635 | Py_complex j; |
| 636 | |
| 637 | TO_COMPLEX(w, j); |
| 638 | equal = (i.real == j.real && i.imag == j.imag); |
| 639 | } |
| 640 | else { |
| 641 | goto Unimplemented; |
| 642 | } |
| 643 | |
| 644 | if (equal == (op == Py_EQ)) |
| 645 | res = Py_True; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 646 | else |
Mark Dickinson | cc6a982 | 2010-05-21 14:55:26 +0000 | [diff] [blame] | 647 | res = Py_False; |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 648 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 649 | Py_INCREF(res); |
| 650 | return res; |
Mark Dickinson | cc6a982 | 2010-05-21 14:55:26 +0000 | [diff] [blame] | 651 | |
| 652 | Unimplemented: |
| 653 | Py_INCREF(Py_NotImplemented); |
| 654 | return Py_NotImplemented; |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 658 | complex_int(PyObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 659 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 660 | PyErr_SetString(PyExc_TypeError, |
| 661 | "can't convert complex to int"); |
| 662 | return NULL; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 665 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 666 | complex_float(PyObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 667 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 668 | PyErr_SetString(PyExc_TypeError, |
| 669 | "can't convert complex to float"); |
| 670 | return NULL; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 673 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 674 | complex_conjugate(PyObject *self) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 675 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 676 | Py_complex c; |
| 677 | c = ((PyComplexObject *)self)->cval; |
| 678 | c.imag = -c.imag; |
| 679 | return PyComplex_FromCComplex(c); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Guido van Rossum | 46334cd | 2007-08-01 17:43:15 +0000 | [diff] [blame] | 682 | PyDoc_STRVAR(complex_conjugate_doc, |
| 683 | "complex.conjugate() -> complex\n" |
| 684 | "\n" |
| 685 | "Returns the complex conjugate of its argument. (3-4j).conjugate() == 3+4j."); |
| 686 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 687 | static PyObject * |
| 688 | complex_getnewargs(PyComplexObject *v) |
| 689 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 690 | Py_complex c = v->cval; |
| 691 | return Py_BuildValue("(dd)", c.real, c.imag); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Eric Smith | 58a4224 | 2009-04-30 01:00:33 +0000 | [diff] [blame] | 694 | PyDoc_STRVAR(complex__format__doc, |
| 695 | "complex.__format__() -> str\n" |
| 696 | "\n" |
| 697 | "Converts to a string according to format_spec."); |
| 698 | |
| 699 | static PyObject * |
| 700 | complex__format__(PyObject* self, PyObject* args) |
| 701 | { |
| 702 | PyObject *format_spec; |
| 703 | |
| 704 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 705 | return NULL; |
Eric Smith | 58a4224 | 2009-04-30 01:00:33 +0000 | [diff] [blame] | 706 | return _PyComplex_FormatAdvanced(self, |
| 707 | PyUnicode_AS_UNICODE(format_spec), |
| 708 | PyUnicode_GET_SIZE(format_spec)); |
| 709 | } |
| 710 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 711 | #if 0 |
| 712 | static PyObject * |
| 713 | complex_is_finite(PyObject *self) |
| 714 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 715 | Py_complex c; |
| 716 | c = ((PyComplexObject *)self)->cval; |
| 717 | return PyBool_FromLong((long)(Py_IS_FINITE(c.real) && |
| 718 | Py_IS_FINITE(c.imag))); |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | PyDoc_STRVAR(complex_is_finite_doc, |
| 722 | "complex.is_finite() -> bool\n" |
| 723 | "\n" |
| 724 | "Returns True if the real and the imaginary part is finite."); |
| 725 | #endif |
| 726 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 727 | static PyMethodDef complex_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 728 | {"conjugate", (PyCFunction)complex_conjugate, METH_NOARGS, |
| 729 | complex_conjugate_doc}, |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 730 | #if 0 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 731 | {"is_finite", (PyCFunction)complex_is_finite, METH_NOARGS, |
| 732 | complex_is_finite_doc}, |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 733 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 734 | {"__getnewargs__", (PyCFunction)complex_getnewargs, METH_NOARGS}, |
| 735 | {"__format__", (PyCFunction)complex__format__, |
| 736 | METH_VARARGS, complex__format__doc}, |
| 737 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 738 | }; |
| 739 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 740 | static PyMemberDef complex_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 741 | {"real", T_DOUBLE, offsetof(PyComplexObject, cval.real), READONLY, |
| 742 | "the real part of a complex number"}, |
| 743 | {"imag", T_DOUBLE, offsetof(PyComplexObject, cval.imag), READONLY, |
| 744 | "the imaginary part of a complex number"}, |
| 745 | {0}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 746 | }; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 747 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 748 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 749 | complex_subtype_from_string(PyTypeObject *type, PyObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 750 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 751 | const char *s, *start; |
| 752 | char *end; |
| 753 | double x=0.0, y=0.0, z; |
| 754 | int got_bracket=0; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 755 | PyObject *s_buffer = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 756 | Py_ssize_t len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 757 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 758 | if (PyUnicode_Check(v)) { |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 759 | Py_ssize_t i, buflen = PyUnicode_GET_SIZE(v); |
| 760 | Py_UNICODE *bufptr; |
| 761 | s_buffer = PyUnicode_TransformDecimalToASCII( |
| 762 | PyUnicode_AS_UNICODE(v), buflen); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 763 | if (s_buffer == NULL) |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 764 | return NULL; |
| 765 | /* Replace non-ASCII whitespace with ' ' */ |
| 766 | bufptr = PyUnicode_AS_UNICODE(s_buffer); |
| 767 | for (i = 0; i < buflen; i++) { |
| 768 | Py_UNICODE ch = bufptr[i]; |
| 769 | if (ch > 127 && Py_UNICODE_ISSPACE(ch)) |
| 770 | bufptr[i] = ' '; |
| 771 | } |
| 772 | s = _PyUnicode_AsStringAndSize(s_buffer, &len); |
| 773 | if (s == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 774 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 775 | } |
| 776 | else if (PyObject_AsCharBuffer(v, &s, &len)) { |
| 777 | PyErr_SetString(PyExc_TypeError, |
Victor Stinner | f961377 | 2010-12-03 16:51:33 +0000 | [diff] [blame] | 778 | "complex() argument must be a string or a number"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 779 | return NULL; |
| 780 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 781 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 782 | /* position on first nonblank */ |
| 783 | start = s; |
| 784 | while (Py_ISSPACE(*s)) |
| 785 | s++; |
| 786 | if (*s == '(') { |
| 787 | /* Skip over possible bracket from repr(). */ |
| 788 | got_bracket = 1; |
| 789 | s++; |
| 790 | while (Py_ISSPACE(*s)) |
| 791 | s++; |
| 792 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 793 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 794 | /* a valid complex string usually takes one of the three forms: |
Mark Dickinson | 6649fa4 | 2009-04-24 13:25:20 +0000 | [diff] [blame] | 795 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 796 | <float> - real part only |
| 797 | <float>j - imaginary part only |
| 798 | <float><signed-float>j - real and imaginary parts |
Mark Dickinson | 6649fa4 | 2009-04-24 13:25:20 +0000 | [diff] [blame] | 799 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 800 | where <float> represents any numeric string that's accepted by the |
| 801 | float constructor (including 'nan', 'inf', 'infinity', etc.), and |
| 802 | <signed-float> is any string of the form <float> whose first |
| 803 | character is '+' or '-'. |
Mark Dickinson | 6649fa4 | 2009-04-24 13:25:20 +0000 | [diff] [blame] | 804 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 805 | For backwards compatibility, the extra forms |
Mark Dickinson | 6649fa4 | 2009-04-24 13:25:20 +0000 | [diff] [blame] | 806 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 807 | <float><sign>j |
| 808 | <sign>j |
| 809 | j |
Mark Dickinson | 6649fa4 | 2009-04-24 13:25:20 +0000 | [diff] [blame] | 810 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 811 | are also accepted, though support for these forms may be removed from |
| 812 | a future version of Python. |
| 813 | */ |
Mark Dickinson | 6649fa4 | 2009-04-24 13:25:20 +0000 | [diff] [blame] | 814 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 815 | /* first look for forms starting with <float> */ |
| 816 | z = PyOS_string_to_double(s, &end, NULL); |
| 817 | if (z == -1.0 && PyErr_Occurred()) { |
| 818 | if (PyErr_ExceptionMatches(PyExc_ValueError)) |
| 819 | PyErr_Clear(); |
| 820 | else |
| 821 | goto error; |
| 822 | } |
| 823 | if (end != s) { |
| 824 | /* all 4 forms starting with <float> land here */ |
| 825 | s = end; |
| 826 | if (*s == '+' || *s == '-') { |
| 827 | /* <float><signed-float>j | <float><sign>j */ |
| 828 | x = z; |
| 829 | y = PyOS_string_to_double(s, &end, NULL); |
| 830 | if (y == -1.0 && PyErr_Occurred()) { |
| 831 | if (PyErr_ExceptionMatches(PyExc_ValueError)) |
| 832 | PyErr_Clear(); |
| 833 | else |
| 834 | goto error; |
| 835 | } |
| 836 | if (end != s) |
| 837 | /* <float><signed-float>j */ |
| 838 | s = end; |
| 839 | else { |
| 840 | /* <float><sign>j */ |
| 841 | y = *s == '+' ? 1.0 : -1.0; |
| 842 | s++; |
| 843 | } |
| 844 | if (!(*s == 'j' || *s == 'J')) |
| 845 | goto parse_error; |
| 846 | s++; |
| 847 | } |
| 848 | else if (*s == 'j' || *s == 'J') { |
| 849 | /* <float>j */ |
| 850 | s++; |
| 851 | y = z; |
| 852 | } |
| 853 | else |
| 854 | /* <float> */ |
| 855 | x = z; |
| 856 | } |
| 857 | else { |
| 858 | /* not starting with <float>; must be <sign>j or j */ |
| 859 | if (*s == '+' || *s == '-') { |
| 860 | /* <sign>j */ |
| 861 | y = *s == '+' ? 1.0 : -1.0; |
| 862 | s++; |
| 863 | } |
| 864 | else |
| 865 | /* j */ |
| 866 | y = 1.0; |
| 867 | if (!(*s == 'j' || *s == 'J')) |
| 868 | goto parse_error; |
| 869 | s++; |
| 870 | } |
Mark Dickinson | ad476da | 2009-04-23 19:14:16 +0000 | [diff] [blame] | 871 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 872 | /* trailing whitespace and closing bracket */ |
| 873 | while (Py_ISSPACE(*s)) |
| 874 | s++; |
| 875 | if (got_bracket) { |
| 876 | /* if there was an opening parenthesis, then the corresponding |
| 877 | closing parenthesis should be right here */ |
| 878 | if (*s != ')') |
| 879 | goto parse_error; |
| 880 | s++; |
| 881 | while (Py_ISSPACE(*s)) |
| 882 | s++; |
| 883 | } |
Mark Dickinson | 6649fa4 | 2009-04-24 13:25:20 +0000 | [diff] [blame] | 884 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 885 | /* we should now be at the end of the string */ |
| 886 | if (s-start != len) |
| 887 | goto parse_error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 888 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 889 | Py_XDECREF(s_buffer); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 890 | return complex_subtype_from_doubles(type, x, y); |
Mark Dickinson | ad476da | 2009-04-23 19:14:16 +0000 | [diff] [blame] | 891 | |
Mark Dickinson | 6649fa4 | 2009-04-24 13:25:20 +0000 | [diff] [blame] | 892 | parse_error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 893 | PyErr_SetString(PyExc_ValueError, |
| 894 | "complex() arg is a malformed string"); |
Mark Dickinson | 1daebdf | 2009-10-26 22:05:06 +0000 | [diff] [blame] | 895 | error: |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 896 | Py_XDECREF(s_buffer); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 897 | return NULL; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 900 | static PyObject * |
| 901 | complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 902 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 903 | PyObject *r, *i, *tmp; |
| 904 | PyNumberMethods *nbr, *nbi = NULL; |
| 905 | Py_complex cr, ci; |
| 906 | int own_r = 0; |
| 907 | int cr_is_complex = 0; |
| 908 | int ci_is_complex = 0; |
| 909 | static char *kwlist[] = {"real", "imag", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 910 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 911 | r = Py_False; |
| 912 | i = NULL; |
| 913 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:complex", kwlist, |
| 914 | &r, &i)) |
| 915 | return NULL; |
Raymond Hettinger | 604cd6a | 2002-08-29 14:22:51 +0000 | [diff] [blame] | 916 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 917 | /* Special-case for a single argument when type(arg) is complex. */ |
| 918 | if (PyComplex_CheckExact(r) && i == NULL && |
| 919 | type == &PyComplex_Type) { |
| 920 | /* Note that we can't know whether it's safe to return |
| 921 | a complex *subclass* instance as-is, hence the restriction |
| 922 | to exact complexes here. If either the input or the |
| 923 | output is a complex subclass, it will be handled below |
| 924 | as a non-orthogonal vector. */ |
| 925 | Py_INCREF(r); |
| 926 | return r; |
| 927 | } |
| 928 | if (PyUnicode_Check(r)) { |
| 929 | if (i != NULL) { |
| 930 | PyErr_SetString(PyExc_TypeError, |
| 931 | "complex() can't take second arg" |
| 932 | " if first is a string"); |
| 933 | return NULL; |
| 934 | } |
| 935 | return complex_subtype_from_string(type, r); |
| 936 | } |
| 937 | if (i != NULL && PyUnicode_Check(i)) { |
| 938 | PyErr_SetString(PyExc_TypeError, |
| 939 | "complex() second arg can't be a string"); |
| 940 | return NULL; |
| 941 | } |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 942 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 943 | tmp = try_complex_special_method(r); |
| 944 | if (tmp) { |
| 945 | r = tmp; |
| 946 | own_r = 1; |
| 947 | } |
| 948 | else if (PyErr_Occurred()) { |
| 949 | return NULL; |
| 950 | } |
Benjamin Peterson | aea4428 | 2010-01-04 01:10:28 +0000 | [diff] [blame] | 951 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 952 | nbr = r->ob_type->tp_as_number; |
| 953 | if (i != NULL) |
| 954 | nbi = i->ob_type->tp_as_number; |
| 955 | if (nbr == NULL || nbr->nb_float == NULL || |
| 956 | ((i != NULL) && (nbi == NULL || nbi->nb_float == NULL))) { |
| 957 | PyErr_SetString(PyExc_TypeError, |
| 958 | "complex() argument must be a string or a number"); |
| 959 | if (own_r) { |
| 960 | Py_DECREF(r); |
| 961 | } |
| 962 | return NULL; |
| 963 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 964 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 965 | /* If we get this far, then the "real" and "imag" parts should |
| 966 | both be treated as numbers, and the constructor should return a |
| 967 | complex number equal to (real + imag*1j). |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 968 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 969 | Note that we do NOT assume the input to already be in canonical |
| 970 | form; the "real" and "imag" parts might themselves be complex |
| 971 | numbers, which slightly complicates the code below. */ |
| 972 | if (PyComplex_Check(r)) { |
| 973 | /* Note that if r is of a complex subtype, we're only |
| 974 | retaining its real & imag parts here, and the return |
| 975 | value is (properly) of the builtin complex type. */ |
| 976 | cr = ((PyComplexObject*)r)->cval; |
| 977 | cr_is_complex = 1; |
| 978 | if (own_r) { |
| 979 | Py_DECREF(r); |
| 980 | } |
| 981 | } |
| 982 | else { |
| 983 | /* The "real" part really is entirely real, and contributes |
| 984 | nothing in the imaginary direction. |
| 985 | Just treat it as a double. */ |
| 986 | tmp = PyNumber_Float(r); |
| 987 | if (own_r) { |
| 988 | /* r was a newly created complex number, rather |
| 989 | than the original "real" argument. */ |
| 990 | Py_DECREF(r); |
| 991 | } |
| 992 | if (tmp == NULL) |
| 993 | return NULL; |
| 994 | if (!PyFloat_Check(tmp)) { |
| 995 | PyErr_SetString(PyExc_TypeError, |
| 996 | "float(r) didn't return a float"); |
| 997 | Py_DECREF(tmp); |
| 998 | return NULL; |
| 999 | } |
| 1000 | cr.real = PyFloat_AsDouble(tmp); |
| 1001 | cr.imag = 0.0; /* Shut up compiler warning */ |
| 1002 | Py_DECREF(tmp); |
| 1003 | } |
| 1004 | if (i == NULL) { |
| 1005 | ci.real = 0.0; |
| 1006 | } |
| 1007 | else if (PyComplex_Check(i)) { |
| 1008 | ci = ((PyComplexObject*)i)->cval; |
| 1009 | ci_is_complex = 1; |
| 1010 | } else { |
| 1011 | /* The "imag" part really is entirely imaginary, and |
| 1012 | contributes nothing in the real direction. |
| 1013 | Just treat it as a double. */ |
| 1014 | tmp = (*nbi->nb_float)(i); |
| 1015 | if (tmp == NULL) |
| 1016 | return NULL; |
| 1017 | ci.real = PyFloat_AsDouble(tmp); |
| 1018 | Py_DECREF(tmp); |
| 1019 | } |
| 1020 | /* If the input was in canonical form, then the "real" and "imag" |
| 1021 | parts are real numbers, so that ci.imag and cr.imag are zero. |
| 1022 | We need this correction in case they were not real numbers. */ |
Christian Heimes | 69a7963 | 2007-11-28 10:04:30 +0000 | [diff] [blame] | 1023 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1024 | if (ci_is_complex) { |
| 1025 | cr.real -= ci.imag; |
| 1026 | } |
| 1027 | if (cr_is_complex) { |
| 1028 | ci.real += cr.imag; |
| 1029 | } |
| 1030 | return complex_subtype_from_doubles(type, cr.real, ci.real); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1033 | PyDoc_STRVAR(complex_doc, |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 1034 | "complex(real[, imag]) -> complex number\n" |
| 1035 | "\n" |
| 1036 | "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] | 1037 | "This is equivalent to (real + imag*1j) where imag defaults to 0."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1038 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1039 | static PyNumberMethods complex_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1040 | (binaryfunc)complex_add, /* nb_add */ |
| 1041 | (binaryfunc)complex_sub, /* nb_subtract */ |
| 1042 | (binaryfunc)complex_mul, /* nb_multiply */ |
| 1043 | (binaryfunc)complex_remainder, /* nb_remainder */ |
| 1044 | (binaryfunc)complex_divmod, /* nb_divmod */ |
| 1045 | (ternaryfunc)complex_pow, /* nb_power */ |
| 1046 | (unaryfunc)complex_neg, /* nb_negative */ |
| 1047 | (unaryfunc)complex_pos, /* nb_positive */ |
| 1048 | (unaryfunc)complex_abs, /* nb_absolute */ |
| 1049 | (inquiry)complex_bool, /* nb_bool */ |
| 1050 | 0, /* nb_invert */ |
| 1051 | 0, /* nb_lshift */ |
| 1052 | 0, /* nb_rshift */ |
| 1053 | 0, /* nb_and */ |
| 1054 | 0, /* nb_xor */ |
| 1055 | 0, /* nb_or */ |
| 1056 | complex_int, /* nb_int */ |
| 1057 | 0, /* nb_reserved */ |
| 1058 | complex_float, /* nb_float */ |
| 1059 | 0, /* nb_inplace_add */ |
| 1060 | 0, /* nb_inplace_subtract */ |
| 1061 | 0, /* nb_inplace_multiply*/ |
| 1062 | 0, /* nb_inplace_remainder */ |
| 1063 | 0, /* nb_inplace_power */ |
| 1064 | 0, /* nb_inplace_lshift */ |
| 1065 | 0, /* nb_inplace_rshift */ |
| 1066 | 0, /* nb_inplace_and */ |
| 1067 | 0, /* nb_inplace_xor */ |
| 1068 | 0, /* nb_inplace_or */ |
| 1069 | (binaryfunc)complex_int_div, /* nb_floor_divide */ |
| 1070 | (binaryfunc)complex_div, /* nb_true_divide */ |
| 1071 | 0, /* nb_inplace_floor_divide */ |
| 1072 | 0, /* nb_inplace_true_divide */ |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 1073 | }; |
| 1074 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1075 | PyTypeObject PyComplex_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1076 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1077 | "complex", |
| 1078 | sizeof(PyComplexObject), |
| 1079 | 0, |
| 1080 | complex_dealloc, /* tp_dealloc */ |
| 1081 | 0, /* tp_print */ |
| 1082 | 0, /* tp_getattr */ |
| 1083 | 0, /* tp_setattr */ |
| 1084 | 0, /* tp_reserved */ |
| 1085 | (reprfunc)complex_repr, /* tp_repr */ |
| 1086 | &complex_as_number, /* tp_as_number */ |
| 1087 | 0, /* tp_as_sequence */ |
| 1088 | 0, /* tp_as_mapping */ |
| 1089 | (hashfunc)complex_hash, /* tp_hash */ |
| 1090 | 0, /* tp_call */ |
Mark Dickinson | 388122d | 2010-08-04 20:56:28 +0000 | [diff] [blame] | 1091 | (reprfunc)complex_repr, /* tp_str */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1092 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1093 | 0, /* tp_setattro */ |
| 1094 | 0, /* tp_as_buffer */ |
| 1095 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 1096 | complex_doc, /* tp_doc */ |
| 1097 | 0, /* tp_traverse */ |
| 1098 | 0, /* tp_clear */ |
| 1099 | complex_richcompare, /* tp_richcompare */ |
| 1100 | 0, /* tp_weaklistoffset */ |
| 1101 | 0, /* tp_iter */ |
| 1102 | 0, /* tp_iternext */ |
| 1103 | complex_methods, /* tp_methods */ |
| 1104 | complex_members, /* tp_members */ |
| 1105 | 0, /* tp_getset */ |
| 1106 | 0, /* tp_base */ |
| 1107 | 0, /* tp_dict */ |
| 1108 | 0, /* tp_descr_get */ |
| 1109 | 0, /* tp_descr_set */ |
| 1110 | 0, /* tp_dictoffset */ |
| 1111 | 0, /* tp_init */ |
| 1112 | PyType_GenericAlloc, /* tp_alloc */ |
| 1113 | complex_new, /* tp_new */ |
| 1114 | PyObject_Del, /* tp_free */ |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 1115 | }; |