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 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 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 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 40 | Py_complex r; |
| 41 | r.real = a.real + b.real; |
| 42 | r.imag = a.imag + b.imag; |
| 43 | return r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 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 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 49 | Py_complex r; |
| 50 | r.real = a.real - b.real; |
| 51 | r.imag = a.imag - b.imag; |
| 52 | return r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 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 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 58 | Py_complex r; |
| 59 | r.real = -a.real; |
| 60 | r.imag = -a.imag; |
| 61 | return r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 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 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 67 | Py_complex r; |
| 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; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 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 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +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: |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 82 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 83 | Py_complex r; |
| 84 | double d = b.real*b.real + b.imag*b.imag; |
| 85 | if (d == 0.) |
| 86 | errno = EDOM; |
| 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; |
| 90 | ******************************************************************/ |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 91 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 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; |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 102 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 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 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 130 | Py_complex r; |
| 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.) |
| 138 | errno = EDOM; |
| 139 | r.real = 0.; |
| 140 | r.imag = 0.; |
| 141 | } |
| 142 | else { |
| 143 | vabs = hypot(a.real,a.imag); |
| 144 | len = pow(vabs,b.real); |
| 145 | at = atan2(a.imag, a.real); |
| 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; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 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 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 160 | Py_complex r, p; |
| 161 | long mask = 1; |
| 162 | r = c_1; |
| 163 | p = x; |
| 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; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 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 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 176 | Py_complex cn; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 177 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 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)); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 187 | |
| 188 | } |
| 189 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 190 | double |
| 191 | c_abs(Py_complex z) |
| 192 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 193 | /* sets errno = ERANGE on overflow; otherwise errno = 0 */ |
| 194 | double result; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 195 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 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; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 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 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 225 | PyObject *op; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 226 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 227 | op = type->tp_alloc(type, 0); |
| 228 | if (op != NULL) |
| 229 | ((PyComplexObject *)op)->cval = cval; |
| 230 | return op; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 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 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 236 | register PyComplexObject *op; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 237 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 238 | /* Inline PyObject_New */ |
| 239 | op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject)); |
| 240 | if (op == NULL) |
| 241 | return PyErr_NoMemory(); |
| 242 | PyObject_INIT(op, &PyComplex_Type); |
| 243 | op->cval = cval; |
| 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 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 250 | Py_complex c; |
| 251 | c.real = real; |
| 252 | c.imag = imag; |
| 253 | return complex_subtype_from_c_complex(type, c); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 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 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 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 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 268 | if (PyComplex_Check(op)) { |
| 269 | return ((PyComplexObject *)op)->cval.real; |
| 270 | } |
| 271 | else { |
| 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 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 279 | if (PyComplex_Check(op)) { |
| 280 | return ((PyComplexObject *)op)->cval.imag; |
| 281 | } |
| 282 | else { |
| 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 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 290 | Py_complex cv; |
| 291 | PyObject *newop = NULL; |
| 292 | static PyObject *complex_str = NULL; |
Georg Brandl | 2b86994 | 2007-03-17 16:08:45 +0000 | [diff] [blame] | 293 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 294 | assert(op); |
| 295 | /* If op is already of type PyComplex_Type, return its value */ |
| 296 | if (PyComplex_Check(op)) { |
| 297 | return ((PyComplexObject *)op)->cval; |
| 298 | } |
| 299 | /* If not, use op's __complex__ method, if it exists */ |
Christian Heimes | d7e1b2b | 2008-01-28 02:07:53 +0000 | [diff] [blame] | 300 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +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 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 305 | if (complex_str == NULL) { |
| 306 | if (!(complex_str = PyString_InternFromString("__complex__"))) |
| 307 | return cv; |
| 308 | } |
Georg Brandl | 2b86994 | 2007-03-17 16:08:45 +0000 | [diff] [blame] | 309 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 310 | if (PyInstance_Check(op)) { |
| 311 | /* this can go away in python 3000 */ |
| 312 | if (PyObject_HasAttr(op, complex_str)) { |
| 313 | newop = PyObject_CallMethod(op, "__complex__", NULL); |
| 314 | if (!newop) |
| 315 | return cv; |
| 316 | } |
| 317 | /* else try __float__ */ |
| 318 | } else { |
| 319 | PyObject *complexfunc; |
| 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. */ |
| 342 | else { |
| 343 | /* PyFloat_AsDouble will return -1 on failure */ |
| 344 | cv.real = PyFloat_AsDouble(op); |
| 345 | return cv; |
| 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 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +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 | |
Guido van Rossum | 363078a | 1996-05-24 20:45:01 +0000 | [diff] [blame] | 356 | static void |
Barry Warsaw | 01d697a | 2001-11-28 20:50:56 +0000 | [diff] [blame] | 357 | complex_to_buf(char *buf, int bufsz, PyComplexObject *v, int precision) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 358 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 359 | char format[32]; |
| 360 | if (v->cval.real == 0.) { |
| 361 | if (!Py_IS_FINITE(v->cval.imag)) { |
| 362 | if (Py_IS_NAN(v->cval.imag)) |
| 363 | strncpy(buf, "nan*j", 6); |
| 364 | else if (copysign(1, v->cval.imag) == 1) |
| 365 | strncpy(buf, "inf*j", 6); |
| 366 | else |
| 367 | strncpy(buf, "-inf*j", 7); |
| 368 | } |
| 369 | else { |
| 370 | PyOS_snprintf(format, sizeof(format), "%%.%ig", precision); |
| 371 | PyOS_ascii_formatd(buf, bufsz - 1, format, v->cval.imag); |
| 372 | strncat(buf, "j", 1); |
| 373 | } |
| 374 | } else { |
| 375 | char re[64], im[64]; |
| 376 | /* Format imaginary part with sign, real part without */ |
| 377 | if (!Py_IS_FINITE(v->cval.real)) { |
| 378 | if (Py_IS_NAN(v->cval.real)) |
| 379 | strncpy(re, "nan", 4); |
| 380 | /* else if (copysign(1, v->cval.real) == 1) */ |
| 381 | else if (v->cval.real > 0) |
| 382 | strncpy(re, "inf", 4); |
| 383 | else |
| 384 | strncpy(re, "-inf", 5); |
| 385 | } |
| 386 | else { |
| 387 | PyOS_snprintf(format, sizeof(format), "%%.%ig", precision); |
| 388 | PyOS_ascii_formatd(re, sizeof(re), format, v->cval.real); |
| 389 | } |
| 390 | if (!Py_IS_FINITE(v->cval.imag)) { |
| 391 | if (Py_IS_NAN(v->cval.imag)) |
| 392 | strncpy(im, "+nan*", 6); |
| 393 | /* else if (copysign(1, v->cval.imag) == 1) */ |
| 394 | else if (v->cval.imag > 0) |
| 395 | strncpy(im, "+inf*", 6); |
| 396 | else |
| 397 | strncpy(im, "-inf*", 6); |
| 398 | } |
| 399 | else { |
| 400 | PyOS_snprintf(format, sizeof(format), "%%+.%ig", precision); |
| 401 | PyOS_ascii_formatd(im, sizeof(im), format, v->cval.imag); |
| 402 | } |
| 403 | PyOS_snprintf(buf, bufsz, "(%s%sj)", re, im); |
| 404 | } |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | static int |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 408 | complex_print(PyComplexObject *v, FILE *fp, int flags) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 409 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 410 | char buf[100]; |
| 411 | complex_to_buf(buf, sizeof(buf), v, |
| 412 | (flags & Py_PRINT_RAW) ? PREC_STR : PREC_REPR); |
| 413 | Py_BEGIN_ALLOW_THREADS |
| 414 | fputs(buf, fp); |
| 415 | Py_END_ALLOW_THREADS |
| 416 | return 0; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 419 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 420 | complex_repr(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 421 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 422 | char buf[100]; |
| 423 | complex_to_buf(buf, sizeof(buf), v, PREC_REPR); |
| 424 | return PyString_FromString(buf); |
Tim Peters | 7069512 | 2001-03-11 08:37:29 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | static PyObject * |
| 428 | complex_str(PyComplexObject *v) |
| 429 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 430 | char buf[100]; |
| 431 | complex_to_buf(buf, sizeof(buf), v, PREC_STR); |
| 432 | return PyString_FromString(buf); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 435 | static long |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 436 | complex_hash(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 437 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 438 | long hashreal, hashimag, combined; |
| 439 | hashreal = _Py_HashDouble(v->cval.real); |
| 440 | if (hashreal == -1) |
| 441 | return -1; |
| 442 | hashimag = _Py_HashDouble(v->cval.imag); |
| 443 | if (hashimag == -1) |
| 444 | return -1; |
| 445 | /* Note: if the imaginary part is 0, hashimag is 0 now, |
| 446 | * so the following returns hashreal unchanged. This is |
| 447 | * important because numbers of different types that |
| 448 | * compare equal must have the same hash value, so that |
| 449 | * hash(x + 0*j) must equal hash(x). |
| 450 | */ |
| 451 | combined = hashreal + 1000003 * hashimag; |
| 452 | if (combined == -1) |
| 453 | combined = -2; |
| 454 | return combined; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 457 | /* This macro may return! */ |
| 458 | #define TO_COMPLEX(obj, c) \ |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 459 | if (PyComplex_Check(obj)) \ |
| 460 | c = ((PyComplexObject *)(obj))->cval; \ |
| 461 | else if (to_complex(&(obj), &(c)) < 0) \ |
| 462 | return (obj) |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 463 | |
| 464 | static int |
| 465 | to_complex(PyObject **pobj, Py_complex *pc) |
| 466 | { |
| 467 | PyObject *obj = *pobj; |
| 468 | |
| 469 | pc->real = pc->imag = 0.0; |
| 470 | if (PyInt_Check(obj)) { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 471 | pc->real = PyInt_AS_LONG(obj); |
| 472 | return 0; |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 473 | } |
| 474 | if (PyLong_Check(obj)) { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 475 | pc->real = PyLong_AsDouble(obj); |
| 476 | if (pc->real == -1.0 && PyErr_Occurred()) { |
| 477 | *pobj = NULL; |
| 478 | return -1; |
| 479 | } |
| 480 | return 0; |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 481 | } |
| 482 | if (PyFloat_Check(obj)) { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 483 | pc->real = PyFloat_AsDouble(obj); |
| 484 | return 0; |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 485 | } |
| 486 | Py_INCREF(Py_NotImplemented); |
| 487 | *pobj = Py_NotImplemented; |
| 488 | return -1; |
| 489 | } |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 490 | |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 491 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 492 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 493 | complex_add(PyComplexObject *v, PyComplexObject *w) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 494 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 495 | Py_complex result; |
| 496 | PyFPE_START_PROTECT("complex_add", return 0) |
| 497 | result = c_sum(v->cval,w->cval); |
| 498 | PyFPE_END_PROTECT(result) |
| 499 | return PyComplex_FromCComplex(result); |
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_sub(PyComplexObject *v, PyComplexObject *w) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 504 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 505 | Py_complex result; |
| 506 | PyFPE_START_PROTECT("complex_sub", return 0) |
| 507 | result = c_diff(v->cval,w->cval); |
| 508 | PyFPE_END_PROTECT(result) |
| 509 | return PyComplex_FromCComplex(result); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 512 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 513 | complex_mul(PyComplexObject *v, PyComplexObject *w) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 514 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 515 | Py_complex result; |
| 516 | PyFPE_START_PROTECT("complex_mul", return 0) |
| 517 | result = c_prod(v->cval,w->cval); |
| 518 | PyFPE_END_PROTECT(result) |
| 519 | return PyComplex_FromCComplex(result); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 522 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 523 | complex_div(PyComplexObject *v, PyComplexObject *w) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 524 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 525 | Py_complex quot; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 526 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 527 | PyFPE_START_PROTECT("complex_div", return 0) |
| 528 | errno = 0; |
| 529 | quot = c_quot(v->cval,w->cval); |
| 530 | PyFPE_END_PROTECT(quot) |
| 531 | if (errno == EDOM) { |
| 532 | PyErr_SetString(PyExc_ZeroDivisionError, "complex division"); |
| 533 | return NULL; |
| 534 | } |
| 535 | return PyComplex_FromCComplex(quot); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 538 | static PyObject * |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 539 | complex_classic_div(PyComplexObject *v, PyComplexObject *w) |
| 540 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 541 | Py_complex quot; |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 542 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 543 | if (Py_DivisionWarningFlag >= 2 && |
| 544 | PyErr_Warn(PyExc_DeprecationWarning, |
| 545 | "classic complex division") < 0) |
| 546 | return NULL; |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 547 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 548 | PyFPE_START_PROTECT("complex_classic_div", return 0) |
| 549 | errno = 0; |
| 550 | quot = c_quot(v->cval,w->cval); |
| 551 | PyFPE_END_PROTECT(quot) |
| 552 | if (errno == EDOM) { |
| 553 | PyErr_SetString(PyExc_ZeroDivisionError, "complex division"); |
| 554 | return NULL; |
| 555 | } |
| 556 | return PyComplex_FromCComplex(quot); |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 560 | complex_remainder(PyComplexObject *v, PyComplexObject *w) |
Guido van Rossum | ee09fc1 | 1996-09-11 13:55:55 +0000 | [diff] [blame] | 561 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 562 | Py_complex div, mod; |
Guido van Rossum | 69cf3c7 | 2002-04-15 12:39:12 +0000 | [diff] [blame] | 563 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 564 | if (PyErr_Warn(PyExc_DeprecationWarning, |
| 565 | "complex divmod(), // and % are deprecated") < 0) |
| 566 | return NULL; |
Guido van Rossum | 69cf3c7 | 2002-04-15 12:39:12 +0000 | [diff] [blame] | 567 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 568 | errno = 0; |
| 569 | div = c_quot(v->cval,w->cval); /* The raw divisor value. */ |
| 570 | if (errno == EDOM) { |
| 571 | PyErr_SetString(PyExc_ZeroDivisionError, "complex remainder"); |
| 572 | return NULL; |
| 573 | } |
| 574 | div.real = floor(div.real); /* Use the floor of the real part. */ |
| 575 | div.imag = 0.0; |
| 576 | mod = c_diff(v->cval, c_prod(w->cval, div)); |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 577 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 578 | return PyComplex_FromCComplex(mod); |
Guido van Rossum | ee09fc1 | 1996-09-11 13:55:55 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Guido van Rossum | ee09fc1 | 1996-09-11 13:55:55 +0000 | [diff] [blame] | 581 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 582 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 583 | complex_divmod(PyComplexObject *v, PyComplexObject *w) |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 584 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 585 | Py_complex div, mod; |
| 586 | PyObject *d, *m, *z; |
Guido van Rossum | 9ec4c78 | 2002-04-15 01:41:56 +0000 | [diff] [blame] | 587 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 588 | if (PyErr_Warn(PyExc_DeprecationWarning, |
| 589 | "complex divmod(), // and % are deprecated") < 0) |
| 590 | return NULL; |
Guido van Rossum | 9ec4c78 | 2002-04-15 01:41:56 +0000 | [diff] [blame] | 591 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 592 | errno = 0; |
| 593 | div = c_quot(v->cval,w->cval); /* The raw divisor value. */ |
| 594 | if (errno == EDOM) { |
| 595 | PyErr_SetString(PyExc_ZeroDivisionError, "complex divmod()"); |
| 596 | return NULL; |
| 597 | } |
| 598 | div.real = floor(div.real); /* Use the floor of the real part. */ |
| 599 | div.imag = 0.0; |
| 600 | mod = c_diff(v->cval, c_prod(w->cval, div)); |
| 601 | d = PyComplex_FromCComplex(div); |
| 602 | m = PyComplex_FromCComplex(mod); |
| 603 | z = PyTuple_Pack(2, d, m); |
| 604 | Py_XDECREF(d); |
| 605 | Py_XDECREF(m); |
| 606 | return z; |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 607 | } |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 608 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 609 | static PyObject * |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 610 | complex_pow(PyObject *v, PyObject *w, PyObject *z) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 611 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 612 | Py_complex p; |
| 613 | Py_complex exponent; |
| 614 | long int_exponent; |
| 615 | Py_complex a, b; |
| 616 | TO_COMPLEX(v, a); |
| 617 | TO_COMPLEX(w, b); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 618 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 619 | if (z!=Py_None) { |
| 620 | PyErr_SetString(PyExc_ValueError, "complex modulo"); |
| 621 | return NULL; |
| 622 | } |
| 623 | PyFPE_START_PROTECT("complex_pow", return 0) |
| 624 | errno = 0; |
| 625 | exponent = b; |
| 626 | int_exponent = (long)exponent.real; |
| 627 | if (exponent.imag == 0. && exponent.real == int_exponent) |
| 628 | p = c_powi(a,int_exponent); |
| 629 | else |
| 630 | p = c_pow(a,exponent); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 631 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 632 | PyFPE_END_PROTECT(p) |
| 633 | Py_ADJUST_ERANGE2(p.real, p.imag); |
| 634 | if (errno == EDOM) { |
| 635 | PyErr_SetString(PyExc_ZeroDivisionError, |
| 636 | "0.0 to a negative or complex power"); |
| 637 | return NULL; |
| 638 | } |
| 639 | else if (errno == ERANGE) { |
| 640 | PyErr_SetString(PyExc_OverflowError, |
| 641 | "complex exponentiation"); |
| 642 | return NULL; |
| 643 | } |
| 644 | return PyComplex_FromCComplex(p); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 647 | static PyObject * |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 648 | complex_int_div(PyComplexObject *v, PyComplexObject *w) |
| 649 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 650 | PyObject *t, *r; |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 651 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 652 | if (PyErr_Warn(PyExc_DeprecationWarning, |
| 653 | "complex divmod(), // and % are deprecated") < 0) |
| 654 | return NULL; |
| 655 | |
| 656 | t = complex_divmod(v, w); |
| 657 | if (t != NULL) { |
| 658 | r = PyTuple_GET_ITEM(t, 0); |
| 659 | Py_INCREF(r); |
| 660 | Py_DECREF(t); |
| 661 | return r; |
| 662 | } |
| 663 | return NULL; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 667 | complex_neg(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 668 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 669 | Py_complex neg; |
| 670 | neg.real = -v->cval.real; |
| 671 | neg.imag = -v->cval.imag; |
| 672 | return PyComplex_FromCComplex(neg); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 675 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 676 | complex_pos(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 677 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 678 | if (PyComplex_CheckExact(v)) { |
| 679 | Py_INCREF(v); |
| 680 | return (PyObject *)v; |
| 681 | } |
| 682 | else |
| 683 | return PyComplex_FromCComplex(v->cval); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 686 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 687 | complex_abs(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 688 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 689 | double result; |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 690 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 691 | PyFPE_START_PROTECT("complex_abs", return 0) |
| 692 | result = c_abs(v->cval); |
| 693 | PyFPE_END_PROTECT(result) |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 694 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 695 | if (errno == ERANGE) { |
| 696 | PyErr_SetString(PyExc_OverflowError, |
| 697 | "absolute value too large"); |
| 698 | return NULL; |
| 699 | } |
| 700 | return PyFloat_FromDouble(result); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | static int |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 704 | complex_nonzero(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 705 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 706 | return v->cval.real != 0.0 || v->cval.imag != 0.0; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | static int |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 710 | complex_coerce(PyObject **pv, PyObject **pw) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 711 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 712 | Py_complex cval; |
| 713 | cval.imag = 0.; |
| 714 | if (PyInt_Check(*pw)) { |
| 715 | cval.real = (double)PyInt_AsLong(*pw); |
| 716 | *pw = PyComplex_FromCComplex(cval); |
| 717 | Py_INCREF(*pv); |
| 718 | return 0; |
| 719 | } |
| 720 | else if (PyLong_Check(*pw)) { |
| 721 | cval.real = PyLong_AsDouble(*pw); |
| 722 | if (cval.real == -1.0 && PyErr_Occurred()) |
| 723 | return -1; |
| 724 | *pw = PyComplex_FromCComplex(cval); |
| 725 | Py_INCREF(*pv); |
| 726 | return 0; |
| 727 | } |
| 728 | else if (PyFloat_Check(*pw)) { |
| 729 | cval.real = PyFloat_AsDouble(*pw); |
| 730 | *pw = PyComplex_FromCComplex(cval); |
| 731 | Py_INCREF(*pv); |
| 732 | return 0; |
| 733 | } |
| 734 | else if (PyComplex_Check(*pw)) { |
| 735 | Py_INCREF(*pv); |
| 736 | Py_INCREF(*pw); |
| 737 | return 0; |
| 738 | } |
| 739 | return 1; /* Can't do it */ |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 742 | static PyObject * |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 743 | complex_richcompare(PyObject *v, PyObject *w, int op) |
| 744 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 745 | int c; |
| 746 | Py_complex i, j; |
| 747 | PyObject *res; |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 748 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 749 | c = PyNumber_CoerceEx(&v, &w); |
| 750 | if (c < 0) |
| 751 | return NULL; |
| 752 | if (c > 0) { |
| 753 | Py_INCREF(Py_NotImplemented); |
| 754 | return Py_NotImplemented; |
| 755 | } |
| 756 | /* Make sure both arguments are complex. */ |
| 757 | if (!(PyComplex_Check(v) && PyComplex_Check(w))) { |
| 758 | Py_DECREF(v); |
| 759 | Py_DECREF(w); |
| 760 | Py_INCREF(Py_NotImplemented); |
| 761 | return Py_NotImplemented; |
| 762 | } |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 763 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 764 | i = ((PyComplexObject *)v)->cval; |
| 765 | j = ((PyComplexObject *)w)->cval; |
| 766 | Py_DECREF(v); |
| 767 | Py_DECREF(w); |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 768 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 769 | if (op != Py_EQ && op != Py_NE) { |
| 770 | PyErr_SetString(PyExc_TypeError, |
| 771 | "no ordering relation is defined for complex numbers"); |
| 772 | return NULL; |
| 773 | } |
Guido van Rossum | 2205642 | 2001-09-24 17:52:04 +0000 | [diff] [blame] | 774 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 775 | if ((i.real == j.real && i.imag == j.imag) == (op == Py_EQ)) |
| 776 | res = Py_True; |
| 777 | else |
| 778 | res = Py_False; |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 779 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 780 | Py_INCREF(res); |
| 781 | return res; |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 785 | complex_int(PyObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 786 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 787 | PyErr_SetString(PyExc_TypeError, |
| 788 | "can't convert complex to int"); |
| 789 | return NULL; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 790 | } |
| 791 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 792 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 793 | complex_long(PyObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 794 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 795 | PyErr_SetString(PyExc_TypeError, |
| 796 | "can't convert complex to long"); |
| 797 | return NULL; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 800 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 801 | complex_float(PyObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 802 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 803 | PyErr_SetString(PyExc_TypeError, |
| 804 | "can't convert complex to float"); |
| 805 | return NULL; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 808 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 809 | complex_conjugate(PyObject *self) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 810 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 811 | Py_complex c; |
| 812 | c = ((PyComplexObject *)self)->cval; |
| 813 | c.imag = -c.imag; |
| 814 | return PyComplex_FromCComplex(c); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 817 | PyDoc_STRVAR(complex_conjugate_doc, |
| 818 | "complex.conjugate() -> complex\n" |
| 819 | "\n" |
| 820 | "Returns the complex conjugate of its argument. (3-4j).conjugate() == 3+4j."); |
| 821 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 822 | static PyObject * |
| 823 | complex_getnewargs(PyComplexObject *v) |
| 824 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 825 | Py_complex c = v->cval; |
| 826 | return Py_BuildValue("(dd)", c.real, c.imag); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 829 | #if 0 |
| 830 | static PyObject * |
| 831 | complex_is_finite(PyObject *self) |
| 832 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 833 | Py_complex c; |
| 834 | c = ((PyComplexObject *)self)->cval; |
| 835 | return PyBool_FromLong((long)(Py_IS_FINITE(c.real) && |
| 836 | Py_IS_FINITE(c.imag))); |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | PyDoc_STRVAR(complex_is_finite_doc, |
| 840 | "complex.is_finite() -> bool\n" |
| 841 | "\n" |
| 842 | "Returns True if the real and the imaginary part is finite."); |
| 843 | #endif |
| 844 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 845 | static PyMethodDef complex_methods[] = { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 846 | {"conjugate", (PyCFunction)complex_conjugate, METH_NOARGS, |
| 847 | complex_conjugate_doc}, |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 848 | #if 0 |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 849 | {"is_finite", (PyCFunction)complex_is_finite, METH_NOARGS, |
| 850 | complex_is_finite_doc}, |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 851 | #endif |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 852 | {"__getnewargs__", (PyCFunction)complex_getnewargs, METH_NOARGS}, |
| 853 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 854 | }; |
| 855 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 856 | static PyMemberDef complex_members[] = { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 857 | {"real", T_DOUBLE, offsetof(PyComplexObject, cval.real), READONLY, |
| 858 | "the real part of a complex number"}, |
| 859 | {"imag", T_DOUBLE, offsetof(PyComplexObject, cval.imag), READONLY, |
| 860 | "the imaginary part of a complex number"}, |
| 861 | {0}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 862 | }; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 863 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 864 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 865 | complex_subtype_from_string(PyTypeObject *type, PyObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 866 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 867 | const char *s, *start; |
| 868 | char *end; |
| 869 | double x=0.0, y=0.0, z; |
| 870 | int got_re=0, got_im=0, got_bracket=0, done=0; |
| 871 | int digit_or_dot; |
| 872 | int sw_error=0; |
| 873 | int sign; |
| 874 | char buffer[256]; /* For errors */ |
Guido van Rossum | 70e3688 | 2001-10-25 18:07:22 +0000 | [diff] [blame] | 875 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 876 | char s_buffer[256]; |
Guido van Rossum | 70e3688 | 2001-10-25 18:07:22 +0000 | [diff] [blame] | 877 | #endif |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 878 | Py_ssize_t len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 879 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 880 | if (PyString_Check(v)) { |
| 881 | s = PyString_AS_STRING(v); |
| 882 | len = PyString_GET_SIZE(v); |
| 883 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 884 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 885 | else if (PyUnicode_Check(v)) { |
| 886 | if (PyUnicode_GET_SIZE(v) >= (Py_ssize_t)sizeof(s_buffer)) { |
| 887 | PyErr_SetString(PyExc_ValueError, |
| 888 | "complex() literal too large to convert"); |
| 889 | return NULL; |
| 890 | } |
| 891 | if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v), |
| 892 | PyUnicode_GET_SIZE(v), |
| 893 | s_buffer, |
| 894 | NULL)) |
| 895 | return NULL; |
| 896 | s = s_buffer; |
| 897 | len = strlen(s); |
| 898 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 899 | #endif |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 900 | else if (PyObject_AsCharBuffer(v, &s, &len)) { |
| 901 | PyErr_SetString(PyExc_TypeError, |
| 902 | "complex() arg is not a string"); |
| 903 | return NULL; |
| 904 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 905 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 906 | /* position on first nonblank */ |
| 907 | start = s; |
| 908 | while (*s && isspace(Py_CHARMASK(*s))) |
| 909 | s++; |
| 910 | if (s[0] == '\0') { |
| 911 | PyErr_SetString(PyExc_ValueError, |
| 912 | "complex() arg is an empty string"); |
| 913 | return NULL; |
| 914 | } |
| 915 | if (s[0] == '(') { |
| 916 | /* Skip over possible bracket from repr(). */ |
| 917 | got_bracket = 1; |
| 918 | s++; |
| 919 | while (*s && isspace(Py_CHARMASK(*s))) |
| 920 | s++; |
| 921 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 922 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 923 | z = -1.0; |
| 924 | sign = 1; |
| 925 | do { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 926 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 927 | switch (*s) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 928 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 929 | case '\0': |
| 930 | if (s-start != len) { |
| 931 | PyErr_SetString( |
| 932 | PyExc_ValueError, |
| 933 | "complex() arg contains a null byte"); |
| 934 | return NULL; |
| 935 | } |
| 936 | if(!done) sw_error=1; |
| 937 | break; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 938 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 939 | case ')': |
| 940 | if (!got_bracket || !(got_re || got_im)) { |
| 941 | sw_error=1; |
| 942 | break; |
| 943 | } |
| 944 | got_bracket=0; |
| 945 | done=1; |
| 946 | s++; |
| 947 | while (*s && isspace(Py_CHARMASK(*s))) |
| 948 | s++; |
| 949 | if (*s) sw_error=1; |
| 950 | break; |
Collin Winter | e38051d | 2007-03-09 20:33:07 +0000 | [diff] [blame] | 951 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 952 | case '-': |
| 953 | sign = -1; |
| 954 | /* Fallthrough */ |
| 955 | case '+': |
| 956 | if (done) sw_error=1; |
| 957 | s++; |
| 958 | if ( *s=='\0'||*s=='+'||*s=='-'||*s==')'|| |
| 959 | isspace(Py_CHARMASK(*s)) ) sw_error=1; |
| 960 | break; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 961 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 962 | case 'J': |
| 963 | case 'j': |
| 964 | if (got_im || done) { |
| 965 | sw_error = 1; |
| 966 | break; |
| 967 | } |
| 968 | if (z<0.0) { |
| 969 | y=sign; |
| 970 | } |
| 971 | else{ |
| 972 | y=sign*z; |
| 973 | } |
| 974 | got_im=1; |
| 975 | s++; |
| 976 | if (*s!='+' && *s!='-' ) |
| 977 | done=1; |
| 978 | break; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 979 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 980 | default: |
| 981 | if (isspace(Py_CHARMASK(*s))) { |
| 982 | while (*s && isspace(Py_CHARMASK(*s))) |
| 983 | s++; |
| 984 | if (*s && *s != ')') |
| 985 | sw_error=1; |
| 986 | else |
| 987 | done = 1; |
| 988 | break; |
| 989 | } |
| 990 | digit_or_dot = |
| 991 | (*s=='.' || isdigit(Py_CHARMASK(*s))); |
| 992 | if (done||!digit_or_dot) { |
| 993 | sw_error=1; |
| 994 | break; |
| 995 | } |
| 996 | errno = 0; |
| 997 | PyFPE_START_PROTECT("strtod", return 0) |
| 998 | z = PyOS_ascii_strtod(s, &end) ; |
| 999 | PyFPE_END_PROTECT(z) |
| 1000 | if (errno == ERANGE && fabs(z) >= 1.0) { |
| 1001 | PyOS_snprintf(buffer, sizeof(buffer), |
| 1002 | "float() out of range: %.150s", s); |
| 1003 | PyErr_SetString( |
| 1004 | PyExc_ValueError, |
| 1005 | buffer); |
| 1006 | return NULL; |
| 1007 | } |
| 1008 | s=end; |
| 1009 | if (*s=='J' || *s=='j') { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1010 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 1011 | break; |
| 1012 | } |
| 1013 | if (got_re) { |
| 1014 | sw_error=1; |
| 1015 | break; |
| 1016 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1017 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 1018 | /* accept a real part */ |
| 1019 | x=sign*z; |
| 1020 | got_re=1; |
| 1021 | if (got_im) done=1; |
| 1022 | z = -1.0; |
| 1023 | sign = 1; |
| 1024 | break; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1025 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 1026 | } /* end of switch */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1027 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 1028 | } while (s - start < len && !sw_error); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1029 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 1030 | if (sw_error || got_bracket) { |
| 1031 | PyErr_SetString(PyExc_ValueError, |
| 1032 | "complex() arg is a malformed string"); |
| 1033 | return NULL; |
| 1034 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1035 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 1036 | return complex_subtype_from_doubles(type, x, y); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1039 | static PyObject * |
| 1040 | complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1041 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 1042 | PyObject *r, *i, *tmp, *f; |
| 1043 | PyNumberMethods *nbr, *nbi = NULL; |
| 1044 | Py_complex cr, ci; |
| 1045 | int own_r = 0; |
| 1046 | int cr_is_complex = 0; |
| 1047 | int ci_is_complex = 0; |
| 1048 | static PyObject *complexstr; |
| 1049 | static char *kwlist[] = {"real", "imag", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1050 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 1051 | r = Py_False; |
| 1052 | i = NULL; |
| 1053 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:complex", kwlist, |
| 1054 | &r, &i)) |
| 1055 | return NULL; |
Raymond Hettinger | 604cd6a | 2002-08-29 14:22:51 +0000 | [diff] [blame] | 1056 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 1057 | /* Special-case for a single argument when type(arg) is complex. */ |
| 1058 | if (PyComplex_CheckExact(r) && i == NULL && |
| 1059 | type == &PyComplex_Type) { |
| 1060 | /* Note that we can't know whether it's safe to return |
| 1061 | a complex *subclass* instance as-is, hence the restriction |
| 1062 | to exact complexes here. If either the input or the |
| 1063 | output is a complex subclass, it will be handled below |
| 1064 | as a non-orthogonal vector. */ |
| 1065 | Py_INCREF(r); |
| 1066 | return r; |
| 1067 | } |
| 1068 | if (PyString_Check(r) || PyUnicode_Check(r)) { |
| 1069 | if (i != NULL) { |
| 1070 | PyErr_SetString(PyExc_TypeError, |
| 1071 | "complex() can't take second arg" |
| 1072 | " if first is a string"); |
| 1073 | return NULL; |
| 1074 | } |
| 1075 | return complex_subtype_from_string(type, r); |
| 1076 | } |
| 1077 | if (i != NULL && (PyString_Check(i) || PyUnicode_Check(i))) { |
| 1078 | PyErr_SetString(PyExc_TypeError, |
| 1079 | "complex() second arg can't be a string"); |
| 1080 | return NULL; |
| 1081 | } |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 1082 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 1083 | /* XXX Hack to support classes with __complex__ method */ |
| 1084 | if (complexstr == NULL) { |
| 1085 | complexstr = PyString_InternFromString("__complex__"); |
| 1086 | if (complexstr == NULL) |
| 1087 | return NULL; |
| 1088 | } |
| 1089 | f = PyObject_GetAttr(r, complexstr); |
| 1090 | if (f == NULL) |
| 1091 | PyErr_Clear(); |
| 1092 | else { |
| 1093 | PyObject *args = PyTuple_New(0); |
| 1094 | if (args == NULL) |
| 1095 | return NULL; |
| 1096 | r = PyEval_CallObject(f, args); |
| 1097 | Py_DECREF(args); |
| 1098 | Py_DECREF(f); |
| 1099 | if (r == NULL) |
| 1100 | return NULL; |
| 1101 | own_r = 1; |
| 1102 | } |
| 1103 | nbr = r->ob_type->tp_as_number; |
| 1104 | if (i != NULL) |
| 1105 | nbi = i->ob_type->tp_as_number; |
| 1106 | if (nbr == NULL || nbr->nb_float == NULL || |
| 1107 | ((i != NULL) && (nbi == NULL || nbi->nb_float == NULL))) { |
| 1108 | PyErr_SetString(PyExc_TypeError, |
| 1109 | "complex() argument must be a string or a number"); |
| 1110 | if (own_r) { |
| 1111 | Py_DECREF(r); |
| 1112 | } |
| 1113 | return NULL; |
| 1114 | } |
Georg Brandl | 8f032cb | 2007-03-13 07:57:51 +0000 | [diff] [blame] | 1115 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 1116 | /* If we get this far, then the "real" and "imag" parts should |
| 1117 | both be treated as numbers, and the constructor should return a |
| 1118 | complex number equal to (real + imag*1j). |
Georg Brandl | 8f032cb | 2007-03-13 07:57:51 +0000 | [diff] [blame] | 1119 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 1120 | Note that we do NOT assume the input to already be in canonical |
| 1121 | form; the "real" and "imag" parts might themselves be complex |
| 1122 | numbers, which slightly complicates the code below. */ |
| 1123 | if (PyComplex_Check(r)) { |
| 1124 | /* Note that if r is of a complex subtype, we're only |
| 1125 | retaining its real & imag parts here, and the return |
| 1126 | value is (properly) of the builtin complex type. */ |
| 1127 | cr = ((PyComplexObject*)r)->cval; |
| 1128 | cr_is_complex = 1; |
| 1129 | if (own_r) { |
| 1130 | Py_DECREF(r); |
| 1131 | } |
| 1132 | } |
| 1133 | else { |
| 1134 | /* The "real" part really is entirely real, and contributes |
| 1135 | nothing in the imaginary direction. |
| 1136 | Just treat it as a double. */ |
| 1137 | tmp = PyNumber_Float(r); |
| 1138 | if (own_r) { |
| 1139 | /* r was a newly created complex number, rather |
| 1140 | than the original "real" argument. */ |
| 1141 | Py_DECREF(r); |
| 1142 | } |
| 1143 | if (tmp == NULL) |
| 1144 | return NULL; |
| 1145 | if (!PyFloat_Check(tmp)) { |
| 1146 | PyErr_SetString(PyExc_TypeError, |
| 1147 | "float(r) didn't return a float"); |
| 1148 | Py_DECREF(tmp); |
| 1149 | return NULL; |
| 1150 | } |
| 1151 | cr.real = PyFloat_AsDouble(tmp); |
| 1152 | cr.imag = 0.0; /* Shut up compiler warning */ |
| 1153 | Py_DECREF(tmp); |
| 1154 | } |
| 1155 | if (i == NULL) { |
| 1156 | ci.real = 0.0; |
| 1157 | } |
| 1158 | else if (PyComplex_Check(i)) { |
| 1159 | ci = ((PyComplexObject*)i)->cval; |
| 1160 | ci_is_complex = 1; |
| 1161 | } else { |
| 1162 | /* The "imag" part really is entirely imaginary, and |
| 1163 | contributes nothing in the real direction. |
| 1164 | Just treat it as a double. */ |
| 1165 | tmp = (*nbi->nb_float)(i); |
| 1166 | if (tmp == NULL) |
| 1167 | return NULL; |
| 1168 | ci.real = PyFloat_AsDouble(tmp); |
| 1169 | Py_DECREF(tmp); |
| 1170 | } |
| 1171 | /* If the input was in canonical form, then the "real" and "imag" |
| 1172 | parts are real numbers, so that ci.imag and cr.imag are zero. |
| 1173 | We need this correction in case they were not real numbers. */ |
Guido van Rossum | 715ec18 | 2007-11-27 22:38:36 +0000 | [diff] [blame] | 1174 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 1175 | if (ci_is_complex) { |
| 1176 | cr.real -= ci.imag; |
| 1177 | } |
| 1178 | if (cr_is_complex) { |
| 1179 | ci.real += cr.imag; |
| 1180 | } |
| 1181 | return complex_subtype_from_doubles(type, cr.real, ci.real); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1184 | PyDoc_STRVAR(complex_doc, |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 1185 | "complex(real[, imag]) -> complex number\n" |
| 1186 | "\n" |
| 1187 | "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] | 1188 | "This is equivalent to (real + imag*1j) where imag defaults to 0."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1189 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1190 | static PyNumberMethods complex_as_number = { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 1191 | (binaryfunc)complex_add, /* nb_add */ |
| 1192 | (binaryfunc)complex_sub, /* nb_subtract */ |
| 1193 | (binaryfunc)complex_mul, /* nb_multiply */ |
| 1194 | (binaryfunc)complex_classic_div, /* nb_divide */ |
| 1195 | (binaryfunc)complex_remainder, /* nb_remainder */ |
| 1196 | (binaryfunc)complex_divmod, /* nb_divmod */ |
| 1197 | (ternaryfunc)complex_pow, /* nb_power */ |
| 1198 | (unaryfunc)complex_neg, /* nb_negative */ |
| 1199 | (unaryfunc)complex_pos, /* nb_positive */ |
| 1200 | (unaryfunc)complex_abs, /* nb_absolute */ |
| 1201 | (inquiry)complex_nonzero, /* nb_nonzero */ |
| 1202 | 0, /* nb_invert */ |
| 1203 | 0, /* nb_lshift */ |
| 1204 | 0, /* nb_rshift */ |
| 1205 | 0, /* nb_and */ |
| 1206 | 0, /* nb_xor */ |
| 1207 | 0, /* nb_or */ |
| 1208 | complex_coerce, /* nb_coerce */ |
| 1209 | complex_int, /* nb_int */ |
| 1210 | complex_long, /* nb_long */ |
| 1211 | complex_float, /* nb_float */ |
| 1212 | 0, /* nb_oct */ |
| 1213 | 0, /* nb_hex */ |
| 1214 | 0, /* nb_inplace_add */ |
| 1215 | 0, /* nb_inplace_subtract */ |
| 1216 | 0, /* nb_inplace_multiply*/ |
| 1217 | 0, /* nb_inplace_divide */ |
| 1218 | 0, /* nb_inplace_remainder */ |
| 1219 | 0, /* nb_inplace_power */ |
| 1220 | 0, /* nb_inplace_lshift */ |
| 1221 | 0, /* nb_inplace_rshift */ |
| 1222 | 0, /* nb_inplace_and */ |
| 1223 | 0, /* nb_inplace_xor */ |
| 1224 | 0, /* nb_inplace_or */ |
| 1225 | (binaryfunc)complex_int_div, /* nb_floor_divide */ |
| 1226 | (binaryfunc)complex_div, /* nb_true_divide */ |
| 1227 | 0, /* nb_inplace_floor_divide */ |
| 1228 | 0, /* nb_inplace_true_divide */ |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 1229 | }; |
| 1230 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1231 | PyTypeObject PyComplex_Type = { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 1232 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1233 | "complex", |
| 1234 | sizeof(PyComplexObject), |
| 1235 | 0, |
| 1236 | complex_dealloc, /* tp_dealloc */ |
| 1237 | (printfunc)complex_print, /* tp_print */ |
| 1238 | 0, /* tp_getattr */ |
| 1239 | 0, /* tp_setattr */ |
| 1240 | 0, /* tp_compare */ |
| 1241 | (reprfunc)complex_repr, /* tp_repr */ |
| 1242 | &complex_as_number, /* tp_as_number */ |
| 1243 | 0, /* tp_as_sequence */ |
| 1244 | 0, /* tp_as_mapping */ |
| 1245 | (hashfunc)complex_hash, /* tp_hash */ |
| 1246 | 0, /* tp_call */ |
| 1247 | (reprfunc)complex_str, /* tp_str */ |
| 1248 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1249 | 0, /* tp_setattro */ |
| 1250 | 0, /* tp_as_buffer */ |
| 1251 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 1252 | complex_doc, /* tp_doc */ |
| 1253 | 0, /* tp_traverse */ |
| 1254 | 0, /* tp_clear */ |
| 1255 | complex_richcompare, /* tp_richcompare */ |
| 1256 | 0, /* tp_weaklistoffset */ |
| 1257 | 0, /* tp_iter */ |
| 1258 | 0, /* tp_iternext */ |
| 1259 | complex_methods, /* tp_methods */ |
| 1260 | complex_members, /* tp_members */ |
| 1261 | 0, /* tp_getset */ |
| 1262 | 0, /* tp_base */ |
| 1263 | 0, /* tp_dict */ |
| 1264 | 0, /* tp_descr_get */ |
| 1265 | 0, /* tp_descr_set */ |
| 1266 | 0, /* tp_dictoffset */ |
| 1267 | 0, /* tp_init */ |
| 1268 | PyType_GenericAlloc, /* tp_alloc */ |
| 1269 | complex_new, /* tp_new */ |
| 1270 | PyObject_Del, /* tp_free */ |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 1271 | }; |
| 1272 | |
| 1273 | #endif |