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