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