Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
| 16 | |
| 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
| 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 32 | /* Complex object implementation */ |
| 33 | |
| 34 | /* Borrows heavily from floatobject.c */ |
| 35 | |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 36 | /* Submitted by Jim Hugunin */ |
| 37 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 38 | #ifndef WITHOUT_COMPLEX |
| 39 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 40 | #include "Python.h" |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 41 | #include "mymath.h" |
| 42 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 43 | #ifdef HAVE_LIMITS_H |
| 44 | #include <limits.h> |
| 45 | #endif |
| 46 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 47 | |
| 48 | /* elementary operations on complex numbers */ |
| 49 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 50 | static Py_complex c_1 = {1., 0.}; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 52 | Py_complex c_sum(a,b) |
| 53 | Py_complex a,b; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 54 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 55 | Py_complex r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 56 | r.real = a.real + b.real; |
| 57 | r.imag = a.imag + b.imag; |
| 58 | return r; |
| 59 | } |
| 60 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 61 | Py_complex c_diff(a,b) |
| 62 | Py_complex a,b; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 63 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 64 | Py_complex r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 65 | r.real = a.real - b.real; |
| 66 | r.imag = a.imag - b.imag; |
| 67 | return r; |
| 68 | } |
| 69 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 70 | Py_complex c_neg(a) |
| 71 | Py_complex a; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 72 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 73 | Py_complex r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 74 | r.real = -a.real; |
| 75 | r.imag = -a.imag; |
| 76 | return r; |
| 77 | } |
| 78 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 79 | Py_complex c_prod(a,b) |
| 80 | Py_complex a,b; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 81 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 82 | Py_complex r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 83 | r.real = a.real*b.real - a.imag*b.imag; |
| 84 | r.imag = a.real*b.imag + a.imag*b.real; |
| 85 | return r; |
| 86 | } |
| 87 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 88 | Py_complex c_quot(a,b) |
| 89 | Py_complex a,b; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 90 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 91 | Py_complex r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 92 | double d = b.real*b.real + b.imag*b.imag; |
| 93 | if (d == 0.) |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 94 | errno = EDOM; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 95 | r.real = (a.real*b.real + a.imag*b.imag)/d; |
| 96 | r.imag = (a.imag*b.real - a.real*b.imag)/d; |
| 97 | return r; |
| 98 | } |
| 99 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 100 | Py_complex c_pow(a,b) |
| 101 | Py_complex a,b; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 102 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 103 | Py_complex r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 104 | double vabs,len,at,phase; |
| 105 | if (b.real == 0. && b.imag == 0.) { |
| 106 | r.real = 1.; |
| 107 | r.imag = 0.; |
| 108 | } |
| 109 | else if (a.real == 0. && a.imag == 0.) { |
| 110 | if (b.imag != 0. || b.real < 0.) |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 111 | errno = ERANGE; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 112 | r.real = 0.; |
| 113 | r.imag = 0.; |
| 114 | } |
| 115 | else { |
| 116 | vabs = hypot(a.real,a.imag); |
| 117 | len = pow(vabs,b.real); |
| 118 | at = atan2(a.imag, a.real); |
| 119 | phase = at*b.real; |
| 120 | if (b.imag != 0.0) { |
| 121 | len /= exp(at*b.imag); |
| 122 | phase += b.imag*log(vabs); |
| 123 | } |
| 124 | r.real = len*cos(phase); |
| 125 | r.imag = len*sin(phase); |
| 126 | } |
| 127 | return r; |
| 128 | } |
| 129 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 130 | static Py_complex c_powu(x, n) |
| 131 | Py_complex x; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 132 | long n; |
| 133 | { |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 134 | Py_complex r, p; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 135 | long mask = 1; |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 136 | r = c_1; |
| 137 | p = x; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 138 | while (mask > 0 && n >= mask) { |
| 139 | if (n & mask) |
| 140 | r = c_prod(r,p); |
| 141 | mask <<= 1; |
| 142 | p = c_prod(p,p); |
| 143 | } |
| 144 | return r; |
| 145 | } |
| 146 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 147 | static Py_complex c_powi(x, n) |
| 148 | Py_complex x; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 149 | long n; |
| 150 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 151 | Py_complex cn; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 152 | |
| 153 | if (n > 100 || n < -100) { |
| 154 | cn.real = (double) n; |
| 155 | cn.imag = 0.; |
| 156 | return c_pow(x,cn); |
| 157 | } |
| 158 | else if (n > 0) |
| 159 | return c_powu(x,n); |
| 160 | else |
| 161 | return c_quot(c_1,c_powu(x,-n)); |
| 162 | |
| 163 | } |
| 164 | |
| 165 | PyObject * |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 166 | PyComplex_FromCComplex(cval) |
| 167 | Py_complex cval; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 168 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 169 | register PyComplexObject *op = |
| 170 | (PyComplexObject *) malloc(sizeof(PyComplexObject)); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 171 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 172 | return PyErr_NoMemory(); |
| 173 | op->ob_type = &PyComplex_Type; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 174 | op->cval = cval; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 175 | _Py_NewReference(op); |
| 176 | return (PyObject *) op; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | PyObject * |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 180 | PyComplex_FromDoubles(real, imag) |
| 181 | double real, imag; |
| 182 | { |
| 183 | Py_complex c; |
| 184 | c.real = real; |
| 185 | c.imag = imag; |
| 186 | return PyComplex_FromCComplex(c); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | double |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 190 | PyComplex_RealAsDouble(op) |
| 191 | PyObject *op; |
| 192 | { |
| 193 | if (PyComplex_Check(op)) { |
| 194 | return ((PyComplexObject *)op)->cval.real; |
| 195 | } else { |
| 196 | return PyFloat_AsDouble(op); |
| 197 | } |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | double |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 201 | PyComplex_ImagAsDouble(op) |
| 202 | PyObject *op; |
| 203 | { |
| 204 | if (PyComplex_Check(op)) { |
| 205 | return ((PyComplexObject *)op)->cval.imag; |
| 206 | } else { |
| 207 | return 0.0; |
| 208 | } |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 211 | Py_complex |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 212 | PyComplex_AsCComplex(op) |
| 213 | PyObject *op; |
| 214 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 215 | Py_complex cv; |
Guido van Rossum | cf3d108 | 1996-01-12 01:21:14 +0000 | [diff] [blame] | 216 | if (PyComplex_Check(op)) { |
| 217 | return ((PyComplexObject *)op)->cval; |
| 218 | } else { |
| 219 | cv.real = PyFloat_AsDouble(op); |
| 220 | cv.imag = 0.; |
| 221 | return cv; |
| 222 | } |
| 223 | } |
| 224 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 225 | static void |
| 226 | complex_dealloc(op) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 227 | PyObject *op; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 228 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 229 | PyMem_DEL(op); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | |
Guido van Rossum | 363078a | 1996-05-24 20:45:01 +0000 | [diff] [blame] | 233 | static void |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 234 | complex_buf_repr(buf, v) |
| 235 | char *buf; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 236 | PyComplexObject *v; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 237 | { |
| 238 | if (v->cval.real == 0.) |
Guido van Rossum | 7241879 | 1996-01-25 16:21:31 +0000 | [diff] [blame] | 239 | sprintf(buf, "%.12gj", v->cval.imag); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 240 | else |
Guido van Rossum | 7241879 | 1996-01-25 16:21:31 +0000 | [diff] [blame] | 241 | sprintf(buf, "(%.12g%+.12gj)", v->cval.real, v->cval.imag); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | static int |
| 245 | complex_print(v, fp, flags) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 246 | PyComplexObject *v; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 247 | FILE *fp; |
| 248 | int flags; /* Not used but required by interface */ |
| 249 | { |
| 250 | char buf[100]; |
| 251 | complex_buf_repr(buf, v); |
| 252 | fputs(buf, fp); |
| 253 | return 0; |
| 254 | } |
| 255 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 256 | static PyObject * |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 257 | complex_repr(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 258 | PyComplexObject *v; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 259 | { |
| 260 | char buf[100]; |
| 261 | complex_buf_repr(buf, v); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 262 | return PyString_FromString(buf); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | static int |
| 266 | complex_compare(v, w) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 267 | PyComplexObject *v, *w; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 268 | { |
| 269 | /* Note: "greater" and "smaller" have no meaning for complex numbers, |
| 270 | but Python requires that they be defined nevertheless. */ |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 271 | Py_complex i, j; |
| 272 | i = v->cval; |
| 273 | j = w->cval; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 274 | if (i.real == j.real && i.imag == j.imag) |
| 275 | return 0; |
| 276 | else if (i.real != j.real) |
| 277 | return (i.real < j.real) ? -1 : 1; |
| 278 | else |
| 279 | return (i.imag < j.imag) ? -1 : 1; |
| 280 | } |
| 281 | |
| 282 | static long |
| 283 | complex_hash(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 284 | PyComplexObject *v; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 285 | { |
| 286 | double intpart, fractpart; |
| 287 | int expo; |
Guido van Rossum | 919cf1a | 1997-01-11 19:26:21 +0000 | [diff] [blame] | 288 | long hipart, x; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 289 | /* This is designed so that Python numbers with the same |
| 290 | value hash to the same value, otherwise comparisons |
| 291 | of mapping keys will turn out weird */ |
| 292 | |
| 293 | #ifdef MPW /* MPW C modf expects pointer to extended as second argument */ |
| 294 | { |
| 295 | extended e; |
| 296 | fractpart = modf(v->cval.real, &e); |
| 297 | intpart = e; |
| 298 | } |
| 299 | #else |
| 300 | fractpart = modf(v->cval.real, &intpart); |
| 301 | #endif |
| 302 | |
Guido van Rossum | 919cf1a | 1997-01-11 19:26:21 +0000 | [diff] [blame] | 303 | if (fractpart == 0.0 && v->cval.imag == 0.0) { |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 304 | if (intpart > 0x7fffffffL || -intpart > 0x7fffffffL) { |
| 305 | /* Convert to long int and use its hash... */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 306 | PyObject *w = PyLong_FromDouble(v->cval.real); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 307 | if (w == NULL) |
| 308 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 309 | x = PyObject_Hash(w); |
| 310 | Py_DECREF(w); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 311 | return x; |
| 312 | } |
| 313 | x = (long)intpart; |
| 314 | } |
| 315 | else { |
| 316 | fractpart = frexp(fractpart, &expo); |
Guido van Rossum | 919cf1a | 1997-01-11 19:26:21 +0000 | [diff] [blame] | 317 | fractpart = fractpart * 2147483648.0; /* 2**31 */ |
| 318 | hipart = (long)fractpart; /* Take the top 32 bits */ |
| 319 | fractpart = (fractpart - (double)hipart) * 2147483648.0; |
| 320 | /* Get the next 32 bits */ |
| 321 | x = hipart + (long)fractpart + (long)intpart + (expo << 15); |
| 322 | /* Combine everything */ |
| 323 | |
| 324 | if (v->cval.imag != 0.0) { /* Hash the imaginary part */ |
| 325 | /* XXX Note that this hashes complex(x, y) |
| 326 | to the same value as complex(y, x). |
| 327 | Still better than it used to be :-) */ |
| 328 | #ifdef MPW |
| 329 | { |
| 330 | extended e; |
| 331 | fractpart = modf(v->cval.imag, &e); |
| 332 | intpart = e; |
| 333 | } |
| 334 | #else |
| 335 | fractpart = modf(v->cval.imag, &intpart); |
| 336 | #endif |
| 337 | fractpart = frexp(fractpart, &expo); |
| 338 | fractpart = fractpart * 2147483648.0; /* 2**31 */ |
| 339 | hipart = (long)fractpart; /* Take the top 32 bits */ |
| 340 | fractpart = |
| 341 | (fractpart - (double)hipart) * 2147483648.0; |
| 342 | /* Get the next 32 bits */ |
| 343 | x ^= hipart + (long)fractpart + |
| 344 | (long)intpart + (expo << 15); |
| 345 | /* Combine everything */ |
| 346 | } |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 347 | } |
| 348 | if (x == -1) |
| 349 | x = -2; |
| 350 | return x; |
| 351 | } |
| 352 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 353 | static PyObject * |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 354 | complex_add(v, w) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 355 | PyComplexObject *v; |
| 356 | PyComplexObject *w; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 357 | { |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 358 | Py_complex result; |
| 359 | PyFPE_START_PROTECT("complex_add", return 0) |
| 360 | result = c_sum(v->cval,w->cval); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 361 | PyFPE_END_PROTECT(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 362 | return PyComplex_FromCComplex(result); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 365 | static PyObject * |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 366 | complex_sub(v, w) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 367 | PyComplexObject *v; |
| 368 | PyComplexObject *w; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 369 | { |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 370 | Py_complex result; |
| 371 | PyFPE_START_PROTECT("complex_sub", return 0) |
| 372 | result = c_diff(v->cval,w->cval); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 373 | PyFPE_END_PROTECT(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 374 | return PyComplex_FromCComplex(result); |
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 | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 378 | complex_mul(v, w) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 379 | PyComplexObject *v; |
| 380 | PyComplexObject *w; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 381 | { |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 382 | Py_complex result; |
| 383 | PyFPE_START_PROTECT("complex_mul", return 0) |
| 384 | result = c_prod(v->cval,w->cval); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 385 | PyFPE_END_PROTECT(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 386 | return PyComplex_FromCComplex(result); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 389 | static PyObject * |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 390 | complex_div(v, w) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 391 | PyComplexObject *v; |
| 392 | PyComplexObject *w; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 393 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 394 | Py_complex quot; |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 395 | PyFPE_START_PROTECT("complex_div", return 0) |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 396 | errno = 0; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 397 | quot = c_quot(v->cval,w->cval); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 398 | PyFPE_END_PROTECT(quot) |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 399 | if (errno == EDOM) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 400 | PyErr_SetString(PyExc_ZeroDivisionError, "complex division"); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 401 | return NULL; |
| 402 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 403 | return PyComplex_FromCComplex(quot); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 406 | static PyObject * |
Guido van Rossum | ee09fc1 | 1996-09-11 13:55:55 +0000 | [diff] [blame] | 407 | complex_remainder(v, w) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 408 | PyComplexObject *v; |
| 409 | PyComplexObject *w; |
Guido van Rossum | ee09fc1 | 1996-09-11 13:55:55 +0000 | [diff] [blame] | 410 | { |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 411 | Py_complex div, mod; |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 412 | errno = 0; |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 413 | div = c_quot(v->cval,w->cval); /* The raw divisor value. */ |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 414 | if (errno == EDOM) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 415 | PyErr_SetString(PyExc_ZeroDivisionError, "complex remainder"); |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 416 | return NULL; |
| 417 | } |
| 418 | div.real = floor(div.real); /* Use the floor of the real part. */ |
| 419 | div.imag = 0.0; |
| 420 | mod = c_diff(v->cval, c_prod(w->cval, div)); |
| 421 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 422 | return PyComplex_FromCComplex(mod); |
Guido van Rossum | ee09fc1 | 1996-09-11 13:55:55 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Guido van Rossum | ee09fc1 | 1996-09-11 13:55:55 +0000 | [diff] [blame] | 425 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 426 | static PyObject * |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 427 | complex_divmod(v, w) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 428 | PyComplexObject *v; |
| 429 | PyComplexObject *w; |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 430 | { |
| 431 | Py_complex div, mod; |
| 432 | PyObject *d, *m, *z; |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 433 | errno = 0; |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 434 | div = c_quot(v->cval,w->cval); /* The raw divisor value. */ |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 435 | if (errno == EDOM) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 436 | PyErr_SetString(PyExc_ZeroDivisionError, "complex divmod()"); |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 437 | return NULL; |
| 438 | } |
| 439 | div.real = floor(div.real); /* Use the floor of the real part. */ |
| 440 | div.imag = 0.0; |
| 441 | mod = c_diff(v->cval, c_prod(w->cval, div)); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 442 | d = PyComplex_FromCComplex(div); |
| 443 | m = PyComplex_FromCComplex(mod); |
| 444 | z = Py_BuildValue("(OO)", d, m); |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 445 | Py_XDECREF(d); |
| 446 | Py_XDECREF(m); |
| 447 | return z; |
| 448 | } |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 449 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 450 | static PyObject * |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 451 | complex_pow(v, w, z) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 452 | PyComplexObject *v; |
| 453 | PyObject *w; |
| 454 | PyComplexObject *z; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 455 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 456 | Py_complex p; |
| 457 | Py_complex exponent; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 458 | long int_exponent; |
| 459 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 460 | if ((PyObject *)z!=Py_None) { |
| 461 | PyErr_SetString(PyExc_ValueError, "complex modulo"); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 462 | return NULL; |
| 463 | } |
| 464 | |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 465 | PyFPE_START_PROTECT("complex_pow", return 0) |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 466 | errno = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 467 | exponent = ((PyComplexObject*)w)->cval; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 468 | int_exponent = (long)exponent.real; |
| 469 | if (exponent.imag == 0. && exponent.real == int_exponent) |
| 470 | p = c_powi(v->cval,int_exponent); |
| 471 | else |
| 472 | p = c_pow(v->cval,exponent); |
| 473 | |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 474 | PyFPE_END_PROTECT(p) |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 475 | if (errno == ERANGE) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 476 | PyErr_SetString(PyExc_ValueError, |
| 477 | "0.0 to a negative or complex power"); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 478 | return NULL; |
| 479 | } |
| 480 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 481 | return PyComplex_FromCComplex(p); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 484 | static PyObject * |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 485 | complex_neg(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 486 | PyComplexObject *v; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 487 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 488 | Py_complex neg; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 489 | neg.real = -v->cval.real; |
| 490 | neg.imag = -v->cval.imag; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 491 | return PyComplex_FromCComplex(neg); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 494 | static PyObject * |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 495 | complex_pos(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 496 | PyComplexObject *v; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 497 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 498 | Py_INCREF(v); |
| 499 | return (PyObject *)v; |
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 * |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 503 | complex_abs(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 504 | PyComplexObject *v; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 505 | { |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 506 | double result; |
| 507 | PyFPE_START_PROTECT("complex_abs", return 0) |
| 508 | result = hypot(v->cval.real,v->cval.imag); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 509 | PyFPE_END_PROTECT(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 510 | return PyFloat_FromDouble(result); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | static int |
| 514 | complex_nonzero(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 515 | PyComplexObject *v; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 516 | { |
| 517 | return v->cval.real != 0.0 && v->cval.imag != 0.0; |
| 518 | } |
| 519 | |
| 520 | static int |
| 521 | complex_coerce(pv, pw) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 522 | PyObject **pv; |
| 523 | PyObject **pw; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 524 | { |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 525 | Py_complex cval; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 526 | cval.imag = 0.; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 527 | if (PyInt_Check(*pw)) { |
| 528 | cval.real = (double)PyInt_AsLong(*pw); |
| 529 | *pw = PyComplex_FromCComplex(cval); |
| 530 | Py_INCREF(*pv); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 531 | return 0; |
| 532 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 533 | else if (PyLong_Check(*pw)) { |
| 534 | cval.real = PyLong_AsDouble(*pw); |
| 535 | *pw = PyComplex_FromCComplex(cval); |
| 536 | Py_INCREF(*pv); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 537 | return 0; |
| 538 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 539 | else if (PyFloat_Check(*pw)) { |
| 540 | cval.real = PyFloat_AsDouble(*pw); |
| 541 | *pw = PyComplex_FromCComplex(cval); |
| 542 | Py_INCREF(*pv); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 543 | return 0; |
| 544 | } |
| 545 | return 1; /* Can't do it */ |
| 546 | } |
| 547 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 548 | static PyObject * |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 549 | complex_int(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 550 | PyObject *v; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 551 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 552 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | d4ab3cd | 1996-09-11 22:54:37 +0000 | [diff] [blame] | 553 | "can't convert complex to int; use e.g. int(abs(z))"); |
| 554 | return NULL; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 557 | static PyObject * |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 558 | complex_long(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 559 | PyObject *v; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 560 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 561 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | d4ab3cd | 1996-09-11 22:54:37 +0000 | [diff] [blame] | 562 | "can't convert complex to long; use e.g. long(abs(z))"); |
| 563 | return NULL; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 566 | static PyObject * |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 567 | complex_float(v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 568 | PyObject *v; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 569 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 570 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | d4ab3cd | 1996-09-11 22:54:37 +0000 | [diff] [blame] | 571 | "can't convert complex to float; use e.g. abs(z)"); |
| 572 | return NULL; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 575 | static PyObject * |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 576 | complex_conjugate(self) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 577 | PyObject *self; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 578 | { |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 579 | Py_complex c; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 580 | c = ((PyComplexObject *)self)->cval; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 581 | c.imag = -c.imag; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 582 | return PyComplex_FromCComplex(c); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | static PyMethodDef complex_methods[] = { |
| 586 | {"conjugate", (PyCFunction)complex_conjugate, 1}, |
| 587 | {NULL, NULL} /* sentinel */ |
| 588 | }; |
| 589 | |
| 590 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 591 | static PyObject * |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 592 | complex_getattr(self, name) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 593 | PyComplexObject *self; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 594 | char *name; |
| 595 | { |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 596 | if (strcmp(name, "real") == 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 597 | return (PyObject *)PyFloat_FromDouble(self->cval.real); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 598 | else if (strcmp(name, "imag") == 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 599 | return (PyObject *)PyFloat_FromDouble(self->cval.imag); |
Guido van Rossum | c054d70 | 1997-04-01 03:12:33 +0000 | [diff] [blame] | 600 | else if (strcmp(name, "__members__") == 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 601 | return Py_BuildValue("[ss]", "imag", "real"); |
| 602 | return Py_FindMethod(complex_methods, (PyObject *)self, name); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 605 | static PyNumberMethods complex_as_number = { |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 606 | (binaryfunc)complex_add, /*nb_add*/ |
| 607 | (binaryfunc)complex_sub, /*nb_subtract*/ |
| 608 | (binaryfunc)complex_mul, /*nb_multiply*/ |
| 609 | (binaryfunc)complex_div, /*nb_divide*/ |
Guido van Rossum | ee09fc1 | 1996-09-11 13:55:55 +0000 | [diff] [blame] | 610 | (binaryfunc)complex_remainder, /*nb_remainder*/ |
| 611 | (binaryfunc)complex_divmod, /*nb_divmod*/ |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 612 | (ternaryfunc)complex_pow, /*nb_power*/ |
| 613 | (unaryfunc)complex_neg, /*nb_negative*/ |
| 614 | (unaryfunc)complex_pos, /*nb_positive*/ |
| 615 | (unaryfunc)complex_abs, /*nb_absolute*/ |
| 616 | (inquiry)complex_nonzero, /*nb_nonzero*/ |
| 617 | 0, /*nb_invert*/ |
| 618 | 0, /*nb_lshift*/ |
| 619 | 0, /*nb_rshift*/ |
| 620 | 0, /*nb_and*/ |
| 621 | 0, /*nb_xor*/ |
| 622 | 0, /*nb_or*/ |
| 623 | (coercion)complex_coerce, /*nb_coerce*/ |
| 624 | (unaryfunc)complex_int, /*nb_int*/ |
| 625 | (unaryfunc)complex_long, /*nb_long*/ |
| 626 | (unaryfunc)complex_float, /*nb_float*/ |
| 627 | 0, /*nb_oct*/ |
| 628 | 0, /*nb_hex*/ |
| 629 | }; |
| 630 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 631 | PyTypeObject PyComplex_Type = { |
| 632 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 633 | 0, |
| 634 | "complex", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 635 | sizeof(PyComplexObject), |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 636 | 0, |
| 637 | (destructor)complex_dealloc, /*tp_dealloc*/ |
| 638 | (printfunc)complex_print, /*tp_print*/ |
| 639 | (getattrfunc)complex_getattr, /*tp_getattr*/ |
| 640 | 0, /*tp_setattr*/ |
| 641 | (cmpfunc)complex_compare, /*tp_compare*/ |
| 642 | (reprfunc)complex_repr, /*tp_repr*/ |
| 643 | &complex_as_number, /*tp_as_number*/ |
| 644 | 0, /*tp_as_sequence*/ |
| 645 | 0, /*tp_as_mapping*/ |
| 646 | (hashfunc)complex_hash, /*tp_hash*/ |
| 647 | }; |
| 648 | |
| 649 | #endif |