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 | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 8 | #ifndef WITHOUT_COMPLEX |
| 9 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 10 | #include "Python.h" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 11 | #include "structmember.h" |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 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.) |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 134 | errno = ERANGE; |
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 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 186 | static PyObject * |
| 187 | complex_subtype_from_c_complex(PyTypeObject *type, Py_complex cval) |
| 188 | { |
| 189 | PyObject *op; |
| 190 | |
| 191 | op = PyType_GenericAlloc(type, 0); |
| 192 | if (op != NULL) |
| 193 | ((PyComplexObject *)op)->cval = cval; |
| 194 | return op; |
| 195 | } |
| 196 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 197 | PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 198 | PyComplex_FromCComplex(Py_complex cval) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 199 | { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 200 | register PyComplexObject *op; |
| 201 | |
| 202 | /* PyObject_New is inlined */ |
| 203 | op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject)); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 204 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 205 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 206 | PyObject_INIT(op, &PyComplex_Type); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 207 | op->cval = cval; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 208 | return (PyObject *) op; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 211 | static PyObject * |
| 212 | complex_subtype_from_doubles(PyTypeObject *type, double real, double imag) |
| 213 | { |
| 214 | Py_complex c; |
| 215 | c.real = real; |
| 216 | c.imag = imag; |
| 217 | return complex_subtype_from_c_complex(type, c); |
| 218 | } |
| 219 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 220 | PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 221 | PyComplex_FromDoubles(double real, double imag) |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 222 | { |
| 223 | Py_complex c; |
| 224 | c.real = real; |
| 225 | c.imag = imag; |
| 226 | return PyComplex_FromCComplex(c); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | double |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 230 | PyComplex_RealAsDouble(PyObject *op) |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 231 | { |
| 232 | if (PyComplex_Check(op)) { |
| 233 | return ((PyComplexObject *)op)->cval.real; |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 234 | } |
| 235 | else { |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 236 | return PyFloat_AsDouble(op); |
| 237 | } |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | double |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 241 | PyComplex_ImagAsDouble(PyObject *op) |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 242 | { |
| 243 | if (PyComplex_Check(op)) { |
| 244 | return ((PyComplexObject *)op)->cval.imag; |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 245 | } |
| 246 | else { |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 247 | return 0.0; |
| 248 | } |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 251 | Py_complex |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 252 | PyComplex_AsCComplex(PyObject *op) |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 253 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 254 | Py_complex cv; |
Guido van Rossum | cf3d108 | 1996-01-12 01:21:14 +0000 | [diff] [blame] | 255 | if (PyComplex_Check(op)) { |
| 256 | return ((PyComplexObject *)op)->cval; |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 257 | } |
| 258 | else { |
Guido van Rossum | cf3d108 | 1996-01-12 01:21:14 +0000 | [diff] [blame] | 259 | cv.real = PyFloat_AsDouble(op); |
| 260 | cv.imag = 0.; |
| 261 | return cv; |
Tim Peters | 7069512 | 2001-03-11 08:37:29 +0000 | [diff] [blame] | 262 | } |
Guido van Rossum | cf3d108 | 1996-01-12 01:21:14 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 265 | static void |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 266 | complex_dealloc(PyObject *op) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 267 | { |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 268 | op->ob_type->tp_free(op); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | |
Guido van Rossum | 363078a | 1996-05-24 20:45:01 +0000 | [diff] [blame] | 272 | static void |
Barry Warsaw | 01d697a | 2001-11-28 20:50:56 +0000 | [diff] [blame] | 273 | complex_to_buf(char *buf, int bufsz, PyComplexObject *v, int precision) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 274 | { |
| 275 | if (v->cval.real == 0.) |
Barry Warsaw | 01d697a | 2001-11-28 20:50:56 +0000 | [diff] [blame] | 276 | PyOS_snprintf(buf, bufsz, "%.*gj", |
| 277 | precision, v->cval.imag); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 278 | else |
Barry Warsaw | 01d697a | 2001-11-28 20:50:56 +0000 | [diff] [blame] | 279 | PyOS_snprintf(buf, bufsz, "(%.*g%+.*gj)", |
| 280 | precision, v->cval.real, |
| 281 | precision, v->cval.imag); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | static int |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 285 | complex_print(PyComplexObject *v, FILE *fp, int flags) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 286 | { |
| 287 | char buf[100]; |
Barry Warsaw | 01d697a | 2001-11-28 20:50:56 +0000 | [diff] [blame] | 288 | complex_to_buf(buf, sizeof(buf), v, |
Tim Peters | 7069512 | 2001-03-11 08:37:29 +0000 | [diff] [blame] | 289 | (flags & Py_PRINT_RAW) ? PREC_STR : PREC_REPR); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 290 | fputs(buf, fp); |
| 291 | return 0; |
| 292 | } |
| 293 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 294 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 295 | complex_repr(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 296 | { |
| 297 | char buf[100]; |
Barry Warsaw | 01d697a | 2001-11-28 20:50:56 +0000 | [diff] [blame] | 298 | complex_to_buf(buf, sizeof(buf), v, PREC_REPR); |
Tim Peters | 7069512 | 2001-03-11 08:37:29 +0000 | [diff] [blame] | 299 | return PyString_FromString(buf); |
| 300 | } |
| 301 | |
| 302 | static PyObject * |
| 303 | complex_str(PyComplexObject *v) |
| 304 | { |
| 305 | char buf[100]; |
Barry Warsaw | 01d697a | 2001-11-28 20:50:56 +0000 | [diff] [blame] | 306 | complex_to_buf(buf, sizeof(buf), v, PREC_STR); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 307 | return PyString_FromString(buf); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 310 | static long |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 311 | complex_hash(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 312 | { |
Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 313 | long hashreal, hashimag, combined; |
| 314 | hashreal = _Py_HashDouble(v->cval.real); |
| 315 | if (hashreal == -1) |
| 316 | return -1; |
| 317 | hashimag = _Py_HashDouble(v->cval.imag); |
| 318 | if (hashimag == -1) |
| 319 | return -1; |
| 320 | /* Note: if the imaginary part is 0, hashimag is 0 now, |
| 321 | * so the following returns hashreal unchanged. This is |
| 322 | * important because numbers of different types that |
| 323 | * compare equal must have the same hash value, so that |
| 324 | * hash(x + 0*j) must equal hash(x). |
| 325 | */ |
| 326 | combined = hashreal + 1000003 * hashimag; |
| 327 | if (combined == -1) |
| 328 | combined = -2; |
| 329 | return combined; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 332 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 333 | complex_add(PyComplexObject *v, PyComplexObject *w) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 334 | { |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 335 | Py_complex result; |
| 336 | PyFPE_START_PROTECT("complex_add", return 0) |
| 337 | result = c_sum(v->cval,w->cval); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 338 | PyFPE_END_PROTECT(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 339 | return PyComplex_FromCComplex(result); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 342 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 343 | complex_sub(PyComplexObject *v, PyComplexObject *w) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 344 | { |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 345 | Py_complex result; |
| 346 | PyFPE_START_PROTECT("complex_sub", return 0) |
| 347 | result = c_diff(v->cval,w->cval); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 348 | PyFPE_END_PROTECT(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 349 | return PyComplex_FromCComplex(result); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 352 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 353 | complex_mul(PyComplexObject *v, PyComplexObject *w) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 354 | { |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 355 | Py_complex result; |
| 356 | PyFPE_START_PROTECT("complex_mul", return 0) |
| 357 | result = c_prod(v->cval,w->cval); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 358 | PyFPE_END_PROTECT(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 359 | return PyComplex_FromCComplex(result); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 362 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 363 | complex_div(PyComplexObject *v, PyComplexObject *w) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 364 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 365 | Py_complex quot; |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 366 | PyFPE_START_PROTECT("complex_div", return 0) |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 367 | errno = 0; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 368 | quot = c_quot(v->cval,w->cval); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 369 | PyFPE_END_PROTECT(quot) |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 370 | if (errno == EDOM) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 371 | PyErr_SetString(PyExc_ZeroDivisionError, "complex division"); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 372 | return NULL; |
| 373 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 374 | return PyComplex_FromCComplex(quot); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 377 | static PyObject * |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 378 | complex_classic_div(PyComplexObject *v, PyComplexObject *w) |
| 379 | { |
| 380 | Py_complex quot; |
| 381 | |
Guido van Rossum | 1832de4 | 2001-09-04 03:51:09 +0000 | [diff] [blame] | 382 | if (Py_DivisionWarningFlag >= 2 && |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 383 | PyErr_Warn(PyExc_DeprecationWarning, |
| 384 | "classic complex division") < 0) |
| 385 | return NULL; |
| 386 | |
| 387 | PyFPE_START_PROTECT("complex_classic_div", return 0) |
| 388 | errno = 0; |
| 389 | quot = c_quot(v->cval,w->cval); |
| 390 | PyFPE_END_PROTECT(quot) |
| 391 | if (errno == EDOM) { |
| 392 | PyErr_SetString(PyExc_ZeroDivisionError, "complex division"); |
| 393 | return NULL; |
| 394 | } |
| 395 | return PyComplex_FromCComplex(quot); |
| 396 | } |
| 397 | |
| 398 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 399 | complex_remainder(PyComplexObject *v, PyComplexObject *w) |
Guido van Rossum | ee09fc1 | 1996-09-11 13:55:55 +0000 | [diff] [blame] | 400 | { |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 401 | Py_complex div, mod; |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 402 | errno = 0; |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 403 | div = c_quot(v->cval,w->cval); /* The raw divisor value. */ |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 404 | if (errno == EDOM) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 405 | PyErr_SetString(PyExc_ZeroDivisionError, "complex remainder"); |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 406 | return NULL; |
| 407 | } |
| 408 | div.real = floor(div.real); /* Use the floor of the real part. */ |
| 409 | div.imag = 0.0; |
| 410 | mod = c_diff(v->cval, c_prod(w->cval, div)); |
| 411 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 412 | return PyComplex_FromCComplex(mod); |
Guido van Rossum | ee09fc1 | 1996-09-11 13:55:55 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Guido van Rossum | ee09fc1 | 1996-09-11 13:55:55 +0000 | [diff] [blame] | 415 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 416 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 417 | complex_divmod(PyComplexObject *v, PyComplexObject *w) |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 418 | { |
| 419 | Py_complex div, mod; |
| 420 | PyObject *d, *m, *z; |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 421 | errno = 0; |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 422 | div = c_quot(v->cval,w->cval); /* The raw divisor value. */ |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 423 | if (errno == EDOM) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 424 | PyErr_SetString(PyExc_ZeroDivisionError, "complex divmod()"); |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 425 | return NULL; |
| 426 | } |
| 427 | div.real = floor(div.real); /* Use the floor of the real part. */ |
| 428 | div.imag = 0.0; |
| 429 | mod = c_diff(v->cval, c_prod(w->cval, div)); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 430 | d = PyComplex_FromCComplex(div); |
| 431 | m = PyComplex_FromCComplex(mod); |
| 432 | z = Py_BuildValue("(OO)", d, m); |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 433 | Py_XDECREF(d); |
| 434 | Py_XDECREF(m); |
| 435 | return z; |
| 436 | } |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 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_pow(PyComplexObject *v, PyObject *w, PyComplexObject *z) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 440 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 441 | Py_complex p; |
| 442 | Py_complex exponent; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 443 | long int_exponent; |
| 444 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 445 | if ((PyObject *)z!=Py_None) { |
| 446 | PyErr_SetString(PyExc_ValueError, "complex modulo"); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 447 | return NULL; |
| 448 | } |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 449 | PyFPE_START_PROTECT("complex_pow", return 0) |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 450 | errno = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 451 | exponent = ((PyComplexObject*)w)->cval; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 452 | int_exponent = (long)exponent.real; |
| 453 | if (exponent.imag == 0. && exponent.real == int_exponent) |
| 454 | p = c_powi(v->cval,int_exponent); |
| 455 | else |
| 456 | p = c_pow(v->cval,exponent); |
| 457 | |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 458 | PyFPE_END_PROTECT(p) |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 459 | if (errno == ERANGE) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 460 | PyErr_SetString(PyExc_ValueError, |
| 461 | "0.0 to a negative or complex power"); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 462 | return NULL; |
| 463 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 464 | return PyComplex_FromCComplex(p); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 467 | static PyObject * |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 468 | complex_int_div(PyComplexObject *v, PyComplexObject *w) |
| 469 | { |
| 470 | PyObject *t, *r; |
| 471 | |
| 472 | t = complex_divmod(v, w); |
| 473 | if (t != NULL) { |
| 474 | r = PyTuple_GET_ITEM(t, 0); |
| 475 | Py_INCREF(r); |
| 476 | Py_DECREF(t); |
| 477 | return r; |
| 478 | } |
| 479 | return NULL; |
| 480 | } |
| 481 | |
| 482 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 483 | complex_neg(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 484 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 485 | Py_complex neg; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 486 | neg.real = -v->cval.real; |
| 487 | neg.imag = -v->cval.imag; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 488 | return PyComplex_FromCComplex(neg); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 491 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 492 | complex_pos(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 493 | { |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 494 | if (PyComplex_CheckExact(v)) { |
| 495 | Py_INCREF(v); |
| 496 | return (PyObject *)v; |
| 497 | } |
| 498 | else |
| 499 | return PyComplex_FromCComplex(v->cval); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 502 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 503 | complex_abs(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 504 | { |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 505 | double result; |
| 506 | PyFPE_START_PROTECT("complex_abs", return 0) |
| 507 | result = hypot(v->cval.real,v->cval.imag); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 508 | PyFPE_END_PROTECT(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 509 | return PyFloat_FromDouble(result); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | static int |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 513 | complex_nonzero(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 514 | { |
Guido van Rossum | 3bbef60 | 1999-01-25 19:42:19 +0000 | [diff] [blame] | 515 | return v->cval.real != 0.0 || v->cval.imag != 0.0; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | static int |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 519 | complex_coerce(PyObject **pv, PyObject **pw) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 520 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 521 | Py_complex cval; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 522 | cval.imag = 0.; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 523 | if (PyInt_Check(*pw)) { |
| 524 | cval.real = (double)PyInt_AsLong(*pw); |
| 525 | *pw = PyComplex_FromCComplex(cval); |
| 526 | Py_INCREF(*pv); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 527 | return 0; |
| 528 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 529 | else if (PyLong_Check(*pw)) { |
| 530 | cval.real = PyLong_AsDouble(*pw); |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 531 | if (cval.real == -1.0 && PyErr_Occurred()) |
| 532 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 533 | *pw = PyComplex_FromCComplex(cval); |
| 534 | Py_INCREF(*pv); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 535 | return 0; |
| 536 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 537 | else if (PyFloat_Check(*pw)) { |
| 538 | cval.real = PyFloat_AsDouble(*pw); |
| 539 | *pw = PyComplex_FromCComplex(cval); |
| 540 | Py_INCREF(*pv); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 541 | return 0; |
| 542 | } |
Guido van Rossum | 6380596 | 2001-09-19 01:13:10 +0000 | [diff] [blame] | 543 | else if (PyComplex_Check(*pw)) { |
| 544 | Py_INCREF(*pv); |
| 545 | Py_INCREF(*pw); |
| 546 | return 0; |
| 547 | } |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 548 | return 1; /* Can't do it */ |
| 549 | } |
| 550 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 551 | static PyObject * |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 552 | complex_richcompare(PyObject *v, PyObject *w, int op) |
| 553 | { |
| 554 | int c; |
| 555 | Py_complex i, j; |
| 556 | PyObject *res; |
| 557 | |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 558 | c = PyNumber_CoerceEx(&v, &w); |
| 559 | if (c < 0) |
| 560 | return NULL; |
| 561 | if (c > 0) { |
| 562 | Py_INCREF(Py_NotImplemented); |
| 563 | return Py_NotImplemented; |
| 564 | } |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 565 | /* Make sure both arguments are complex. */ |
| 566 | if (!(PyComplex_Check(v) && PyComplex_Check(w))) { |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 567 | Py_DECREF(v); |
| 568 | Py_DECREF(w); |
| 569 | Py_INCREF(Py_NotImplemented); |
| 570 | return Py_NotImplemented; |
| 571 | } |
| 572 | |
| 573 | i = ((PyComplexObject *)v)->cval; |
| 574 | j = ((PyComplexObject *)w)->cval; |
| 575 | Py_DECREF(v); |
| 576 | Py_DECREF(w); |
| 577 | |
Guido van Rossum | 2205642 | 2001-09-24 17:52:04 +0000 | [diff] [blame] | 578 | if (op != Py_EQ && op != Py_NE) { |
| 579 | PyErr_SetString(PyExc_TypeError, |
| 580 | "cannot compare complex numbers using <, <=, >, >="); |
| 581 | return NULL; |
| 582 | } |
| 583 | |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 584 | if ((i.real == j.real && i.imag == j.imag) == (op == Py_EQ)) |
| 585 | res = Py_True; |
| 586 | else |
| 587 | res = Py_False; |
| 588 | |
| 589 | Py_INCREF(res); |
| 590 | return res; |
| 591 | } |
| 592 | |
| 593 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 594 | complex_int(PyObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 595 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 596 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | d4ab3cd | 1996-09-11 22:54:37 +0000 | [diff] [blame] | 597 | "can't convert complex to int; use e.g. int(abs(z))"); |
| 598 | return NULL; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 601 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 602 | complex_long(PyObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 603 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 604 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | d4ab3cd | 1996-09-11 22:54:37 +0000 | [diff] [blame] | 605 | "can't convert complex to long; use e.g. long(abs(z))"); |
| 606 | return NULL; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 609 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 610 | complex_float(PyObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 611 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 612 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | d4ab3cd | 1996-09-11 22:54:37 +0000 | [diff] [blame] | 613 | "can't convert complex to float; use e.g. abs(z)"); |
| 614 | return NULL; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 617 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 618 | complex_conjugate(PyObject *self) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 619 | { |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 620 | Py_complex c; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 621 | c = ((PyComplexObject *)self)->cval; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 622 | c.imag = -c.imag; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 623 | return PyComplex_FromCComplex(c); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | static PyMethodDef complex_methods[] = { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 627 | {"conjugate", (PyCFunction)complex_conjugate, METH_NOARGS}, |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 628 | {NULL, NULL} /* sentinel */ |
| 629 | }; |
| 630 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 631 | static PyMemberDef complex_members[] = { |
| 632 | {"real", T_DOUBLE, offsetof(PyComplexObject, cval.real), 0, |
| 633 | "the real part of a complex number"}, |
| 634 | {"imag", T_DOUBLE, offsetof(PyComplexObject, cval.imag), 0, |
| 635 | "the imaginary part of a complex number"}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 636 | {0}, |
| 637 | }; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 638 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 639 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 640 | complex_subtype_from_string(PyTypeObject *type, PyObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 641 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 642 | extern double strtod(const char *, char **); |
| 643 | const char *s, *start; |
| 644 | char *end; |
| 645 | double x=0.0, y=0.0, z; |
| 646 | int got_re=0, got_im=0, done=0; |
| 647 | int digit_or_dot; |
| 648 | int sw_error=0; |
| 649 | int sign; |
| 650 | char buffer[256]; /* For errors */ |
Guido van Rossum | 70e3688 | 2001-10-25 18:07:22 +0000 | [diff] [blame] | 651 | #ifdef Py_USING_UNICODE |
| 652 | char s_buffer[256]; |
| 653 | #endif |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 654 | int len; |
| 655 | |
| 656 | if (PyString_Check(v)) { |
| 657 | s = PyString_AS_STRING(v); |
| 658 | len = PyString_GET_SIZE(v); |
| 659 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 660 | #ifdef Py_USING_UNICODE |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 661 | else if (PyUnicode_Check(v)) { |
| 662 | if (PyUnicode_GET_SIZE(v) >= sizeof(s_buffer)) { |
| 663 | PyErr_SetString(PyExc_ValueError, |
| 664 | "complex() literal too large to convert"); |
| 665 | return NULL; |
| 666 | } |
| 667 | if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v), |
| 668 | PyUnicode_GET_SIZE(v), |
| 669 | s_buffer, |
| 670 | NULL)) |
| 671 | return NULL; |
| 672 | s = s_buffer; |
| 673 | len = (int)strlen(s); |
| 674 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 675 | #endif |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 676 | else if (PyObject_AsCharBuffer(v, &s, &len)) { |
| 677 | PyErr_SetString(PyExc_TypeError, |
| 678 | "complex() arg is not a string"); |
| 679 | return NULL; |
| 680 | } |
| 681 | |
| 682 | /* position on first nonblank */ |
| 683 | start = s; |
| 684 | while (*s && isspace(Py_CHARMASK(*s))) |
| 685 | s++; |
| 686 | if (s[0] == '\0') { |
| 687 | PyErr_SetString(PyExc_ValueError, |
| 688 | "complex() arg is an empty string"); |
| 689 | return NULL; |
| 690 | } |
| 691 | |
| 692 | z = -1.0; |
| 693 | sign = 1; |
| 694 | do { |
| 695 | |
| 696 | switch (*s) { |
| 697 | |
| 698 | case '\0': |
| 699 | if (s-start != len) { |
| 700 | PyErr_SetString( |
| 701 | PyExc_ValueError, |
| 702 | "complex() arg contains a null byte"); |
| 703 | return NULL; |
| 704 | } |
| 705 | if(!done) sw_error=1; |
| 706 | break; |
| 707 | |
| 708 | case '-': |
| 709 | sign = -1; |
| 710 | /* Fallthrough */ |
| 711 | case '+': |
| 712 | if (done) sw_error=1; |
| 713 | s++; |
| 714 | if ( *s=='\0'||*s=='+'||*s=='-' || |
| 715 | isspace(Py_CHARMASK(*s)) ) sw_error=1; |
| 716 | break; |
| 717 | |
| 718 | case 'J': |
| 719 | case 'j': |
| 720 | if (got_im || done) { |
| 721 | sw_error = 1; |
| 722 | break; |
| 723 | } |
| 724 | if (z<0.0) { |
| 725 | y=sign; |
| 726 | } |
| 727 | else{ |
| 728 | y=sign*z; |
| 729 | } |
| 730 | got_im=1; |
| 731 | s++; |
| 732 | if (*s!='+' && *s!='-' ) |
| 733 | done=1; |
| 734 | break; |
| 735 | |
| 736 | default: |
| 737 | if (isspace(Py_CHARMASK(*s))) { |
| 738 | while (*s && isspace(Py_CHARMASK(*s))) |
| 739 | s++; |
| 740 | if (s[0] != '\0') |
| 741 | sw_error=1; |
| 742 | else |
| 743 | done = 1; |
| 744 | break; |
| 745 | } |
| 746 | digit_or_dot = |
| 747 | (*s=='.' || isdigit(Py_CHARMASK(*s))); |
| 748 | if (done||!digit_or_dot) { |
| 749 | sw_error=1; |
| 750 | break; |
| 751 | } |
| 752 | errno = 0; |
| 753 | PyFPE_START_PROTECT("strtod", return 0) |
| 754 | z = strtod(s, &end) ; |
| 755 | PyFPE_END_PROTECT(z) |
| 756 | if (errno != 0) { |
Barry Warsaw | 01d697a | 2001-11-28 20:50:56 +0000 | [diff] [blame] | 757 | PyOS_snprintf(buffer, sizeof(buffer), |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 758 | "float() out of range: %.150s", s); |
| 759 | PyErr_SetString( |
| 760 | PyExc_ValueError, |
| 761 | buffer); |
| 762 | return NULL; |
| 763 | } |
| 764 | s=end; |
| 765 | if (*s=='J' || *s=='j') { |
| 766 | |
| 767 | break; |
| 768 | } |
| 769 | if (got_re) { |
| 770 | sw_error=1; |
| 771 | break; |
| 772 | } |
| 773 | |
| 774 | /* accept a real part */ |
| 775 | x=sign*z; |
| 776 | got_re=1; |
| 777 | if (got_im) done=1; |
| 778 | z = -1.0; |
| 779 | sign = 1; |
| 780 | break; |
| 781 | |
| 782 | } /* end of switch */ |
| 783 | |
| 784 | } while (*s!='\0' && !sw_error); |
| 785 | |
| 786 | if (sw_error) { |
| 787 | PyErr_SetString(PyExc_ValueError, |
| 788 | "complex() arg is a malformed string"); |
| 789 | return NULL; |
| 790 | } |
| 791 | |
| 792 | return complex_subtype_from_doubles(type, x, y); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 795 | static PyObject * |
| 796 | complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 797 | { |
| 798 | PyObject *r, *i, *tmp; |
| 799 | PyNumberMethods *nbr, *nbi = NULL; |
| 800 | Py_complex cr, ci; |
| 801 | int own_r = 0; |
| 802 | static char *kwlist[] = {"real", "imag", 0}; |
| 803 | |
| 804 | r = Py_False; |
| 805 | i = NULL; |
| 806 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:complex", kwlist, |
| 807 | &r, &i)) |
| 808 | return NULL; |
| 809 | if (PyString_Check(r) || PyUnicode_Check(r)) |
| 810 | return complex_subtype_from_string(type, r); |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 811 | |
| 812 | nbr = r->ob_type->tp_as_number; |
| 813 | if (i != NULL) |
| 814 | nbi = i->ob_type->tp_as_number; |
| 815 | if (nbr == NULL || nbr->nb_float == NULL || |
| 816 | ((i != NULL) && (nbi == NULL || nbi->nb_float == NULL))) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 817 | PyErr_SetString(PyExc_TypeError, |
| 818 | "complex() arg can't be converted to complex"); |
| 819 | return NULL; |
| 820 | } |
| 821 | /* XXX Hack to support classes with __complex__ method */ |
| 822 | if (PyInstance_Check(r)) { |
| 823 | static PyObject *complexstr; |
| 824 | PyObject *f; |
| 825 | if (complexstr == NULL) { |
| 826 | complexstr = PyString_InternFromString("__complex__"); |
| 827 | if (complexstr == NULL) |
| 828 | return NULL; |
| 829 | } |
| 830 | f = PyObject_GetAttr(r, complexstr); |
| 831 | if (f == NULL) |
| 832 | PyErr_Clear(); |
| 833 | else { |
| 834 | PyObject *args = Py_BuildValue("()"); |
| 835 | if (args == NULL) |
| 836 | return NULL; |
| 837 | r = PyEval_CallObject(f, args); |
| 838 | Py_DECREF(args); |
| 839 | Py_DECREF(f); |
| 840 | if (r == NULL) |
| 841 | return NULL; |
| 842 | own_r = 1; |
| 843 | } |
| 844 | } |
| 845 | if (PyComplex_Check(r)) { |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 846 | /* Note that if r is of a complex subtype, we're only |
| 847 | retaining its real & imag parts here, and the return |
| 848 | value is (properly) of the builtin complex type. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 849 | cr = ((PyComplexObject*)r)->cval; |
| 850 | if (own_r) { |
| 851 | Py_DECREF(r); |
| 852 | } |
| 853 | } |
| 854 | else { |
| 855 | tmp = PyNumber_Float(r); |
| 856 | if (own_r) { |
| 857 | Py_DECREF(r); |
| 858 | } |
| 859 | if (tmp == NULL) |
| 860 | return NULL; |
| 861 | if (!PyFloat_Check(tmp)) { |
| 862 | PyErr_SetString(PyExc_TypeError, |
| 863 | "float(r) didn't return a float"); |
| 864 | Py_DECREF(tmp); |
| 865 | return NULL; |
| 866 | } |
| 867 | cr.real = PyFloat_AsDouble(tmp); |
| 868 | Py_DECREF(tmp); |
| 869 | cr.imag = 0.0; |
| 870 | } |
| 871 | if (i == NULL) { |
| 872 | ci.real = 0.0; |
| 873 | ci.imag = 0.0; |
| 874 | } |
| 875 | else if (PyComplex_Check(i)) |
| 876 | ci = ((PyComplexObject*)i)->cval; |
| 877 | else { |
| 878 | tmp = (*nbi->nb_float)(i); |
| 879 | if (tmp == NULL) |
| 880 | return NULL; |
| 881 | ci.real = PyFloat_AsDouble(tmp); |
| 882 | Py_DECREF(tmp); |
| 883 | ci.imag = 0.; |
| 884 | } |
| 885 | cr.real -= ci.imag; |
| 886 | cr.imag += ci.real; |
| 887 | return complex_subtype_from_c_complex(type, cr); |
| 888 | } |
| 889 | |
| 890 | static char complex_doc[] = |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 891 | "complex(real[, imag]) -> complex number\n" |
| 892 | "\n" |
| 893 | "Create a complex number from a real part and an optional imaginary part.\n" |
| 894 | "This is equivalent to (real + imag*1j) where imag defaults to 0."; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 895 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 896 | static PyNumberMethods complex_as_number = { |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 897 | (binaryfunc)complex_add, /* nb_add */ |
| 898 | (binaryfunc)complex_sub, /* nb_subtract */ |
| 899 | (binaryfunc)complex_mul, /* nb_multiply */ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 900 | (binaryfunc)complex_classic_div, /* nb_divide */ |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 901 | (binaryfunc)complex_remainder, /* nb_remainder */ |
| 902 | (binaryfunc)complex_divmod, /* nb_divmod */ |
| 903 | (ternaryfunc)complex_pow, /* nb_power */ |
| 904 | (unaryfunc)complex_neg, /* nb_negative */ |
| 905 | (unaryfunc)complex_pos, /* nb_positive */ |
| 906 | (unaryfunc)complex_abs, /* nb_absolute */ |
| 907 | (inquiry)complex_nonzero, /* nb_nonzero */ |
| 908 | 0, /* nb_invert */ |
| 909 | 0, /* nb_lshift */ |
| 910 | 0, /* nb_rshift */ |
| 911 | 0, /* nb_and */ |
| 912 | 0, /* nb_xor */ |
| 913 | 0, /* nb_or */ |
| 914 | (coercion)complex_coerce, /* nb_coerce */ |
| 915 | (unaryfunc)complex_int, /* nb_int */ |
| 916 | (unaryfunc)complex_long, /* nb_long */ |
| 917 | (unaryfunc)complex_float, /* nb_float */ |
| 918 | 0, /* nb_oct */ |
| 919 | 0, /* nb_hex */ |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 920 | 0, /* nb_inplace_add */ |
| 921 | 0, /* nb_inplace_subtract */ |
| 922 | 0, /* nb_inplace_multiply*/ |
| 923 | 0, /* nb_inplace_divide */ |
| 924 | 0, /* nb_inplace_remainder */ |
| 925 | 0, /* nb_inplace_power */ |
| 926 | 0, /* nb_inplace_lshift */ |
| 927 | 0, /* nb_inplace_rshift */ |
| 928 | 0, /* nb_inplace_and */ |
| 929 | 0, /* nb_inplace_xor */ |
| 930 | 0, /* nb_inplace_or */ |
| 931 | (binaryfunc)complex_int_div, /* nb_floor_divide */ |
| 932 | (binaryfunc)complex_div, /* nb_true_divide */ |
| 933 | 0, /* nb_inplace_floor_divide */ |
| 934 | 0, /* nb_inplace_true_divide */ |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 935 | }; |
| 936 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 937 | PyTypeObject PyComplex_Type = { |
| 938 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 939 | 0, |
| 940 | "complex", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 941 | sizeof(PyComplexObject), |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 942 | 0, |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 943 | (destructor)complex_dealloc, /* tp_dealloc */ |
| 944 | (printfunc)complex_print, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 945 | 0, /* tp_getattr */ |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 946 | 0, /* tp_setattr */ |
| 947 | 0, /* tp_compare */ |
| 948 | (reprfunc)complex_repr, /* tp_repr */ |
| 949 | &complex_as_number, /* tp_as_number */ |
| 950 | 0, /* tp_as_sequence */ |
| 951 | 0, /* tp_as_mapping */ |
| 952 | (hashfunc)complex_hash, /* tp_hash */ |
| 953 | 0, /* tp_call */ |
Tim Peters | 7069512 | 2001-03-11 08:37:29 +0000 | [diff] [blame] | 954 | (reprfunc)complex_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 955 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 956 | 0, /* tp_setattro */ |
| 957 | 0, /* tp_as_buffer */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 958 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 959 | complex_doc, /* tp_doc */ |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 960 | 0, /* tp_traverse */ |
| 961 | 0, /* tp_clear */ |
| 962 | complex_richcompare, /* tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 963 | 0, /* tp_weaklistoffset */ |
| 964 | 0, /* tp_iter */ |
| 965 | 0, /* tp_iternext */ |
| 966 | complex_methods, /* tp_methods */ |
| 967 | complex_members, /* tp_members */ |
| 968 | 0, /* tp_getset */ |
| 969 | 0, /* tp_base */ |
| 970 | 0, /* tp_dict */ |
| 971 | 0, /* tp_descr_get */ |
| 972 | 0, /* tp_descr_set */ |
| 973 | 0, /* tp_dictoffset */ |
| 974 | 0, /* tp_init */ |
| 975 | 0, /* tp_alloc */ |
| 976 | complex_new, /* tp_new */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 977 | _PyObject_Del, /* tp_free */ |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 978 | }; |
| 979 | |
| 980 | #endif |