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