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