Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 2 | /* Complex object implementation */ |
| 3 | |
| 4 | /* Borrows heavily from floatobject.c */ |
| 5 | |
Guido van Rossum | 9678394 | 1997-05-20 18:21:34 +0000 | [diff] [blame] | 6 | /* Submitted by Jim Hugunin */ |
| 7 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 8 | #include "Python.h" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 9 | #include "structmember.h" |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 11 | /* elementary operations on complex numbers */ |
| 12 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 13 | static Py_complex c_1 = {1., 0.}; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 14 | |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 15 | Py_complex |
Antoine Pitrou | 1eee8e5 | 2014-07-07 18:49:30 -0400 | [diff] [blame] | 16 | _Py_c_sum(Py_complex a, Py_complex b) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 17 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 18 | Py_complex r; |
| 19 | r.real = a.real + b.real; |
| 20 | r.imag = a.imag + b.imag; |
| 21 | return r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 22 | } |
| 23 | |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 24 | Py_complex |
Antoine Pitrou | 1eee8e5 | 2014-07-07 18:49:30 -0400 | [diff] [blame] | 25 | _Py_c_diff(Py_complex a, Py_complex b) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 26 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 27 | Py_complex r; |
| 28 | r.real = a.real - b.real; |
| 29 | r.imag = a.imag - b.imag; |
| 30 | return r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 33 | Py_complex |
Antoine Pitrou | 1eee8e5 | 2014-07-07 18:49:30 -0400 | [diff] [blame] | 34 | _Py_c_neg(Py_complex a) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 35 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 36 | Py_complex r; |
| 37 | r.real = -a.real; |
| 38 | r.imag = -a.imag; |
| 39 | return r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 42 | Py_complex |
Antoine Pitrou | 1eee8e5 | 2014-07-07 18:49:30 -0400 | [diff] [blame] | 43 | _Py_c_prod(Py_complex a, Py_complex b) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 44 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 45 | Py_complex r; |
| 46 | r.real = a.real*b.real - a.imag*b.imag; |
| 47 | r.imag = a.real*b.imag + a.imag*b.real; |
| 48 | return r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 51 | Py_complex |
Antoine Pitrou | 1eee8e5 | 2014-07-07 18:49:30 -0400 | [diff] [blame] | 52 | _Py_c_quot(Py_complex a, Py_complex b) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 53 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 54 | /****************************************************************** |
| 55 | This was the original algorithm. It's grossly prone to spurious |
| 56 | overflow and underflow errors. It also merrily divides by 0 despite |
| 57 | checking for that(!). The code still serves a doc purpose here, as |
| 58 | the algorithm following is a simple by-cases transformation of this |
| 59 | one: |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 60 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 61 | Py_complex r; |
| 62 | double d = b.real*b.real + b.imag*b.imag; |
| 63 | if (d == 0.) |
| 64 | errno = EDOM; |
| 65 | r.real = (a.real*b.real + a.imag*b.imag)/d; |
| 66 | r.imag = (a.imag*b.real - a.real*b.imag)/d; |
| 67 | return r; |
| 68 | ******************************************************************/ |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 69 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 70 | /* This algorithm is better, and is pretty obvious: first divide the |
| 71 | * numerators and denominator by whichever of {b.real, b.imag} has |
| 72 | * larger magnitude. The earliest reference I found was to CACM |
| 73 | * Algorithm 116 (Complex Division, Robert L. Smith, Stanford |
| 74 | * University). As usual, though, we're still ignoring all IEEE |
| 75 | * endcases. |
| 76 | */ |
| 77 | Py_complex r; /* the result */ |
| 78 | const double abs_breal = b.real < 0 ? -b.real : b.real; |
| 79 | const double abs_bimag = b.imag < 0 ? -b.imag : b.imag; |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 80 | |
Antoine Pitrou | 9086f92 | 2014-10-10 23:49:32 +0200 | [diff] [blame] | 81 | if (abs_breal >= abs_bimag) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 82 | /* divide tops and bottom by b.real */ |
| 83 | if (abs_breal == 0.0) { |
| 84 | errno = EDOM; |
| 85 | r.real = r.imag = 0.0; |
| 86 | } |
| 87 | else { |
| 88 | const double ratio = b.imag / b.real; |
| 89 | const double denom = b.real + b.imag * ratio; |
| 90 | r.real = (a.real + a.imag * ratio) / denom; |
| 91 | r.imag = (a.imag - a.real * ratio) / denom; |
| 92 | } |
| 93 | } |
Antoine Pitrou | 9086f92 | 2014-10-10 23:49:32 +0200 | [diff] [blame] | 94 | else if (abs_bimag >= abs_breal) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 95 | /* divide tops and bottom by b.imag */ |
| 96 | const double ratio = b.real / b.imag; |
| 97 | const double denom = b.real * ratio + b.imag; |
| 98 | assert(b.imag != 0.0); |
| 99 | r.real = (a.real * ratio + a.imag) / denom; |
| 100 | r.imag = (a.imag * ratio - a.real) / denom; |
| 101 | } |
Antoine Pitrou | 9086f92 | 2014-10-10 23:49:32 +0200 | [diff] [blame] | 102 | else { |
| 103 | /* At least one of b.real or b.imag is a NaN */ |
| 104 | r.real = r.imag = Py_NAN; |
| 105 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 106 | return r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 109 | Py_complex |
Antoine Pitrou | 1eee8e5 | 2014-07-07 18:49:30 -0400 | [diff] [blame] | 110 | _Py_c_pow(Py_complex a, Py_complex b) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 111 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 112 | Py_complex r; |
| 113 | double vabs,len,at,phase; |
| 114 | if (b.real == 0. && b.imag == 0.) { |
| 115 | r.real = 1.; |
| 116 | r.imag = 0.; |
| 117 | } |
| 118 | else if (a.real == 0. && a.imag == 0.) { |
| 119 | if (b.imag != 0. || b.real < 0.) |
| 120 | errno = EDOM; |
| 121 | r.real = 0.; |
| 122 | r.imag = 0.; |
| 123 | } |
| 124 | else { |
| 125 | vabs = hypot(a.real,a.imag); |
| 126 | len = pow(vabs,b.real); |
| 127 | at = atan2(a.imag, a.real); |
| 128 | phase = at*b.real; |
| 129 | if (b.imag != 0.0) { |
| 130 | len /= exp(at*b.imag); |
| 131 | phase += b.imag*log(vabs); |
| 132 | } |
| 133 | r.real = len*cos(phase); |
| 134 | r.imag = len*sin(phase); |
| 135 | } |
| 136 | return r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 139 | static Py_complex |
| 140 | c_powu(Py_complex x, long n) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 141 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 142 | Py_complex r, p; |
| 143 | long mask = 1; |
| 144 | r = c_1; |
| 145 | p = x; |
| 146 | while (mask > 0 && n >= mask) { |
| 147 | if (n & mask) |
Antoine Pitrou | 1eee8e5 | 2014-07-07 18:49:30 -0400 | [diff] [blame] | 148 | r = _Py_c_prod(r,p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 149 | mask <<= 1; |
Antoine Pitrou | 1eee8e5 | 2014-07-07 18:49:30 -0400 | [diff] [blame] | 150 | p = _Py_c_prod(p,p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 151 | } |
| 152 | return r; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 155 | static Py_complex |
| 156 | c_powi(Py_complex x, long n) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 157 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 158 | Py_complex cn; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 159 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 160 | if (n > 100 || n < -100) { |
| 161 | cn.real = (double) n; |
| 162 | cn.imag = 0.; |
Antoine Pitrou | 1eee8e5 | 2014-07-07 18:49:30 -0400 | [diff] [blame] | 163 | return _Py_c_pow(x,cn); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 164 | } |
| 165 | else if (n > 0) |
| 166 | return c_powu(x,n); |
| 167 | else |
Antoine Pitrou | 1eee8e5 | 2014-07-07 18:49:30 -0400 | [diff] [blame] | 168 | return _Py_c_quot(c_1, c_powu(x,-n)); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 169 | |
| 170 | } |
| 171 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 172 | double |
Antoine Pitrou | 1eee8e5 | 2014-07-07 18:49:30 -0400 | [diff] [blame] | 173 | _Py_c_abs(Py_complex z) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 174 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 175 | /* sets errno = ERANGE on overflow; otherwise errno = 0 */ |
| 176 | double result; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 177 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 178 | if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) { |
| 179 | /* C99 rules: if either the real or the imaginary part is an |
| 180 | infinity, return infinity, even if the other part is a |
| 181 | NaN. */ |
| 182 | if (Py_IS_INFINITY(z.real)) { |
| 183 | result = fabs(z.real); |
| 184 | errno = 0; |
| 185 | return result; |
| 186 | } |
| 187 | if (Py_IS_INFINITY(z.imag)) { |
| 188 | result = fabs(z.imag); |
| 189 | errno = 0; |
| 190 | return result; |
| 191 | } |
| 192 | /* either the real or imaginary part is a NaN, |
| 193 | and neither is infinite. Result should be NaN. */ |
| 194 | return Py_NAN; |
| 195 | } |
| 196 | result = hypot(z.real, z.imag); |
| 197 | if (!Py_IS_FINITE(result)) |
| 198 | errno = ERANGE; |
| 199 | else |
| 200 | errno = 0; |
| 201 | return result; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 204 | static PyObject * |
| 205 | complex_subtype_from_c_complex(PyTypeObject *type, Py_complex cval) |
| 206 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 207 | PyObject *op; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 209 | op = type->tp_alloc(type, 0); |
| 210 | if (op != NULL) |
| 211 | ((PyComplexObject *)op)->cval = cval; |
| 212 | return op; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 215 | PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 216 | PyComplex_FromCComplex(Py_complex cval) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 217 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 218 | PyComplexObject *op; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 219 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 220 | /* Inline PyObject_New */ |
| 221 | op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject)); |
| 222 | if (op == NULL) |
| 223 | return PyErr_NoMemory(); |
Christian Heimes | d3afe78 | 2013-12-04 09:27:47 +0100 | [diff] [blame] | 224 | (void)PyObject_INIT(op, &PyComplex_Type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 225 | op->cval = cval; |
| 226 | return (PyObject *) op; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 229 | static PyObject * |
| 230 | complex_subtype_from_doubles(PyTypeObject *type, double real, double imag) |
| 231 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 232 | Py_complex c; |
| 233 | c.real = real; |
| 234 | c.imag = imag; |
| 235 | return complex_subtype_from_c_complex(type, c); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 238 | PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 239 | PyComplex_FromDoubles(double real, double imag) |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 240 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 241 | Py_complex c; |
| 242 | c.real = real; |
| 243 | c.imag = imag; |
| 244 | return PyComplex_FromCComplex(c); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | double |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 248 | PyComplex_RealAsDouble(PyObject *op) |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 249 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 250 | if (PyComplex_Check(op)) { |
| 251 | return ((PyComplexObject *)op)->cval.real; |
| 252 | } |
| 253 | else { |
| 254 | return PyFloat_AsDouble(op); |
| 255 | } |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | double |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 259 | PyComplex_ImagAsDouble(PyObject *op) |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 260 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 261 | if (PyComplex_Check(op)) { |
| 262 | return ((PyComplexObject *)op)->cval.imag; |
| 263 | } |
| 264 | else { |
| 265 | return 0.0; |
| 266 | } |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Benjamin Peterson | aea4428 | 2010-01-04 01:10:28 +0000 | [diff] [blame] | 269 | static PyObject * |
| 270 | try_complex_special_method(PyObject *op) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 271 | PyObject *f; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 272 | _Py_IDENTIFIER(__complex__); |
Benjamin Peterson | aea4428 | 2010-01-04 01:10:28 +0000 | [diff] [blame] | 273 | |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 274 | f = _PyObject_LookupSpecial(op, &PyId___complex__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 275 | if (f) { |
| 276 | PyObject *res = PyObject_CallFunctionObjArgs(f, NULL); |
| 277 | Py_DECREF(f); |
Mark Dickinson | d20fb82 | 2012-11-14 17:08:31 +0000 | [diff] [blame] | 278 | if (res != NULL && !PyComplex_Check(res)) { |
| 279 | PyErr_SetString(PyExc_TypeError, |
| 280 | "__complex__ should return a complex object"); |
| 281 | Py_DECREF(res); |
| 282 | return NULL; |
| 283 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 284 | return res; |
| 285 | } |
| 286 | return NULL; |
Benjamin Peterson | aea4428 | 2010-01-04 01:10:28 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Guido van Rossum | 9e720e3 | 1996-07-21 02:31:35 +0000 | [diff] [blame] | 289 | Py_complex |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 290 | PyComplex_AsCComplex(PyObject *op) |
Guido van Rossum | 926518b | 1996-08-19 19:30:45 +0000 | [diff] [blame] | 291 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 292 | Py_complex cv; |
| 293 | PyObject *newop = NULL; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 294 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 295 | assert(op); |
| 296 | /* If op is already of type PyComplex_Type, return its value */ |
| 297 | if (PyComplex_Check(op)) { |
| 298 | return ((PyComplexObject *)op)->cval; |
| 299 | } |
| 300 | /* If not, use op's __complex__ method, if it exists */ |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 301 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 302 | /* return -1 on failure */ |
| 303 | cv.real = -1.; |
| 304 | cv.imag = 0.; |
| 305 | |
| 306 | newop = try_complex_special_method(op); |
| 307 | |
| 308 | if (newop) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 309 | cv = ((PyComplexObject *)newop)->cval; |
| 310 | Py_DECREF(newop); |
| 311 | return cv; |
| 312 | } |
| 313 | else if (PyErr_Occurred()) { |
| 314 | return cv; |
| 315 | } |
| 316 | /* If neither of the above works, interpret op as a float giving the |
| 317 | real part of the result, and fill in the imaginary part as 0. */ |
| 318 | else { |
| 319 | /* PyFloat_AsDouble will return -1 on failure */ |
| 320 | cv.real = PyFloat_AsDouble(op); |
| 321 | return cv; |
| 322 | } |
Guido van Rossum | cf3d108 | 1996-01-12 01:21:14 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 325 | static void |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 326 | complex_dealloc(PyObject *op) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 327 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 328 | op->ob_type->tp_free(op); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 331 | static PyObject * |
Eric Smith | 70099a1 | 2010-12-04 13:27:34 +0000 | [diff] [blame] | 332 | complex_repr(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 333 | { |
Eric Smith | 70099a1 | 2010-12-04 13:27:34 +0000 | [diff] [blame] | 334 | int precision = 0; |
| 335 | char format_code = 'r'; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 336 | PyObject *result = NULL; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 337 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 338 | /* If these are non-NULL, they'll need to be freed. */ |
| 339 | char *pre = NULL; |
| 340 | char *im = NULL; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 341 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 342 | /* These do not need to be freed. re is either an alias |
| 343 | for pre or a pointer to a constant. lead and tail |
| 344 | are pointers to constants. */ |
| 345 | char *re = NULL; |
| 346 | char *lead = ""; |
| 347 | char *tail = ""; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 348 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 349 | if (v->cval.real == 0. && copysign(1.0, v->cval.real)==1.0) { |
Eric Smith | 70099a1 | 2010-12-04 13:27:34 +0000 | [diff] [blame] | 350 | /* Real part is +0: just output the imaginary part and do not |
| 351 | include parens. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 352 | re = ""; |
| 353 | im = PyOS_double_to_string(v->cval.imag, format_code, |
| 354 | precision, 0, NULL); |
| 355 | if (!im) { |
| 356 | PyErr_NoMemory(); |
| 357 | goto done; |
| 358 | } |
| 359 | } else { |
Eric Smith | 70099a1 | 2010-12-04 13:27:34 +0000 | [diff] [blame] | 360 | /* Format imaginary part with sign, real part without. Include |
| 361 | parens in the result. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 362 | pre = PyOS_double_to_string(v->cval.real, format_code, |
| 363 | precision, 0, NULL); |
| 364 | if (!pre) { |
| 365 | PyErr_NoMemory(); |
| 366 | goto done; |
| 367 | } |
| 368 | re = pre; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 | im = PyOS_double_to_string(v->cval.imag, format_code, |
| 371 | precision, Py_DTSF_SIGN, NULL); |
| 372 | if (!im) { |
| 373 | PyErr_NoMemory(); |
| 374 | goto done; |
| 375 | } |
| 376 | lead = "("; |
| 377 | tail = ")"; |
| 378 | } |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 379 | result = PyUnicode_FromFormat("%s%s%sj%s", lead, re, im, tail); |
Mark Dickinson | ad476da | 2009-04-23 19:14:16 +0000 | [diff] [blame] | 380 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 381 | PyMem_Free(im); |
| 382 | PyMem_Free(pre); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 383 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 384 | return result; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 387 | static Py_hash_t |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 388 | complex_hash(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 389 | { |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 390 | Py_uhash_t hashreal, hashimag, combined; |
| 391 | hashreal = (Py_uhash_t)_Py_HashDouble(v->cval.real); |
| 392 | if (hashreal == (Py_uhash_t)-1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 393 | return -1; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 394 | hashimag = (Py_uhash_t)_Py_HashDouble(v->cval.imag); |
| 395 | if (hashimag == (Py_uhash_t)-1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 396 | return -1; |
| 397 | /* Note: if the imaginary part is 0, hashimag is 0 now, |
| 398 | * so the following returns hashreal unchanged. This is |
| 399 | * important because numbers of different types that |
| 400 | * compare equal must have the same hash value, so that |
| 401 | * hash(x + 0*j) must equal hash(x). |
| 402 | */ |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 403 | combined = hashreal + _PyHASH_IMAG * hashimag; |
Benjamin Peterson | 8035bc5 | 2010-10-23 16:20:50 +0000 | [diff] [blame] | 404 | if (combined == (Py_uhash_t)-1) |
| 405 | combined = (Py_uhash_t)-2; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 406 | return (Py_hash_t)combined; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 409 | /* This macro may return! */ |
| 410 | #define TO_COMPLEX(obj, c) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 411 | if (PyComplex_Check(obj)) \ |
| 412 | c = ((PyComplexObject *)(obj))->cval; \ |
| 413 | else if (to_complex(&(obj), &(c)) < 0) \ |
| 414 | return (obj) |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 415 | |
| 416 | static int |
| 417 | to_complex(PyObject **pobj, Py_complex *pc) |
| 418 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 419 | PyObject *obj = *pobj; |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 420 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 421 | pc->real = pc->imag = 0.0; |
| 422 | if (PyLong_Check(obj)) { |
| 423 | pc->real = PyLong_AsDouble(obj); |
| 424 | if (pc->real == -1.0 && PyErr_Occurred()) { |
| 425 | *pobj = NULL; |
| 426 | return -1; |
| 427 | } |
| 428 | return 0; |
| 429 | } |
| 430 | if (PyFloat_Check(obj)) { |
| 431 | pc->real = PyFloat_AsDouble(obj); |
| 432 | return 0; |
| 433 | } |
| 434 | Py_INCREF(Py_NotImplemented); |
| 435 | *pobj = Py_NotImplemented; |
| 436 | return -1; |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 437 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 438 | |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 439 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 440 | static PyObject * |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 441 | complex_add(PyObject *v, PyObject *w) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 442 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 443 | Py_complex result; |
| 444 | Py_complex a, b; |
| 445 | TO_COMPLEX(v, a); |
| 446 | TO_COMPLEX(w, b); |
| 447 | PyFPE_START_PROTECT("complex_add", return 0) |
Antoine Pitrou | 1eee8e5 | 2014-07-07 18:49:30 -0400 | [diff] [blame] | 448 | result = _Py_c_sum(a, b); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 449 | PyFPE_END_PROTECT(result) |
| 450 | return PyComplex_FromCComplex(result); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 453 | static PyObject * |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 454 | complex_sub(PyObject *v, PyObject *w) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 455 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 456 | Py_complex result; |
| 457 | Py_complex a, b; |
| 458 | TO_COMPLEX(v, a); |
| 459 | TO_COMPLEX(w, b); |
| 460 | PyFPE_START_PROTECT("complex_sub", return 0) |
Antoine Pitrou | 1eee8e5 | 2014-07-07 18:49:30 -0400 | [diff] [blame] | 461 | result = _Py_c_diff(a, b); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 462 | PyFPE_END_PROTECT(result) |
| 463 | return PyComplex_FromCComplex(result); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 466 | static PyObject * |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 467 | complex_mul(PyObject *v, PyObject *w) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 468 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 469 | Py_complex result; |
| 470 | Py_complex a, b; |
| 471 | TO_COMPLEX(v, a); |
| 472 | TO_COMPLEX(w, b); |
| 473 | PyFPE_START_PROTECT("complex_mul", return 0) |
Antoine Pitrou | 1eee8e5 | 2014-07-07 18:49:30 -0400 | [diff] [blame] | 474 | result = _Py_c_prod(a, b); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 475 | PyFPE_END_PROTECT(result) |
| 476 | return PyComplex_FromCComplex(result); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 479 | static PyObject * |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 480 | complex_div(PyObject *v, PyObject *w) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 481 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 482 | Py_complex quot; |
| 483 | Py_complex a, b; |
| 484 | TO_COMPLEX(v, a); |
| 485 | TO_COMPLEX(w, b); |
| 486 | PyFPE_START_PROTECT("complex_div", return 0) |
| 487 | errno = 0; |
Antoine Pitrou | 1eee8e5 | 2014-07-07 18:49:30 -0400 | [diff] [blame] | 488 | quot = _Py_c_quot(a, b); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 489 | PyFPE_END_PROTECT(quot) |
| 490 | if (errno == EDOM) { |
| 491 | PyErr_SetString(PyExc_ZeroDivisionError, "complex division by zero"); |
| 492 | return NULL; |
| 493 | } |
| 494 | return PyComplex_FromCComplex(quot); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 497 | static PyObject * |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 498 | complex_remainder(PyObject *v, PyObject *w) |
Guido van Rossum | ee09fc1 | 1996-09-11 13:55:55 +0000 | [diff] [blame] | 499 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 500 | PyErr_SetString(PyExc_TypeError, |
| 501 | "can't mod complex numbers."); |
| 502 | return NULL; |
Guido van Rossum | ee09fc1 | 1996-09-11 13:55:55 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Guido van Rossum | ee09fc1 | 1996-09-11 13:55:55 +0000 | [diff] [blame] | 505 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 506 | static PyObject * |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 507 | complex_divmod(PyObject *v, PyObject *w) |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 508 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 509 | PyErr_SetString(PyExc_TypeError, |
| 510 | "can't take floor or mod of complex number."); |
| 511 | return NULL; |
Guido van Rossum | 3be12e9 | 1996-09-12 20:56:18 +0000 | [diff] [blame] | 512 | } |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 513 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 514 | static PyObject * |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 515 | complex_pow(PyObject *v, PyObject *w, PyObject *z) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 516 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 517 | Py_complex p; |
| 518 | Py_complex exponent; |
| 519 | long int_exponent; |
| 520 | Py_complex a, b; |
| 521 | TO_COMPLEX(v, a); |
| 522 | TO_COMPLEX(w, b); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 523 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 524 | if (z != Py_None) { |
| 525 | PyErr_SetString(PyExc_ValueError, "complex modulo"); |
| 526 | return NULL; |
| 527 | } |
| 528 | PyFPE_START_PROTECT("complex_pow", return 0) |
| 529 | errno = 0; |
| 530 | exponent = b; |
| 531 | int_exponent = (long)exponent.real; |
| 532 | if (exponent.imag == 0. && exponent.real == int_exponent) |
| 533 | p = c_powi(a, int_exponent); |
| 534 | else |
Antoine Pitrou | 1eee8e5 | 2014-07-07 18:49:30 -0400 | [diff] [blame] | 535 | p = _Py_c_pow(a, exponent); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 536 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 537 | PyFPE_END_PROTECT(p) |
| 538 | Py_ADJUST_ERANGE2(p.real, p.imag); |
| 539 | if (errno == EDOM) { |
| 540 | PyErr_SetString(PyExc_ZeroDivisionError, |
| 541 | "0.0 to a negative or complex power"); |
| 542 | return NULL; |
| 543 | } |
| 544 | else if (errno == ERANGE) { |
| 545 | PyErr_SetString(PyExc_OverflowError, |
| 546 | "complex exponentiation"); |
| 547 | return NULL; |
| 548 | } |
| 549 | return PyComplex_FromCComplex(p); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 552 | static PyObject * |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 553 | complex_int_div(PyObject *v, PyObject *w) |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 554 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 555 | PyErr_SetString(PyExc_TypeError, |
| 556 | "can't take floor of complex number."); |
| 557 | return NULL; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 561 | complex_neg(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 562 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 563 | Py_complex neg; |
| 564 | neg.real = -v->cval.real; |
| 565 | neg.imag = -v->cval.imag; |
| 566 | return PyComplex_FromCComplex(neg); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 569 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 570 | complex_pos(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 571 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 572 | if (PyComplex_CheckExact(v)) { |
| 573 | Py_INCREF(v); |
| 574 | return (PyObject *)v; |
| 575 | } |
| 576 | else |
| 577 | return PyComplex_FromCComplex(v->cval); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 580 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 581 | complex_abs(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 582 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 583 | double result; |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 584 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 585 | PyFPE_START_PROTECT("complex_abs", return 0) |
Antoine Pitrou | 1eee8e5 | 2014-07-07 18:49:30 -0400 | [diff] [blame] | 586 | result = _Py_c_abs(v->cval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 587 | PyFPE_END_PROTECT(result) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 588 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 589 | if (errno == ERANGE) { |
| 590 | PyErr_SetString(PyExc_OverflowError, |
| 591 | "absolute value too large"); |
| 592 | return NULL; |
| 593 | } |
| 594 | return PyFloat_FromDouble(result); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | static int |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 598 | complex_bool(PyComplexObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 599 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 600 | return v->cval.real != 0.0 || v->cval.imag != 0.0; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 603 | static PyObject * |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 604 | complex_richcompare(PyObject *v, PyObject *w, int op) |
| 605 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 606 | PyObject *res; |
Mark Dickinson | cc6a982 | 2010-05-21 14:55:26 +0000 | [diff] [blame] | 607 | Py_complex i; |
| 608 | int equal; |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 609 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 610 | if (op != Py_EQ && op != Py_NE) { |
Mark Dickinson | cc6a982 | 2010-05-21 14:55:26 +0000 | [diff] [blame] | 611 | goto Unimplemented; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 612 | } |
Guido van Rossum | 2205642 | 2001-09-24 17:52:04 +0000 | [diff] [blame] | 613 | |
Mark Dickinson | cc6a982 | 2010-05-21 14:55:26 +0000 | [diff] [blame] | 614 | assert(PyComplex_Check(v)); |
| 615 | TO_COMPLEX(v, i); |
| 616 | |
| 617 | if (PyLong_Check(w)) { |
| 618 | /* Check for 0.0 imaginary part first to avoid the rich |
| 619 | * comparison when possible. |
| 620 | */ |
| 621 | if (i.imag == 0.0) { |
| 622 | PyObject *j, *sub_res; |
| 623 | j = PyFloat_FromDouble(i.real); |
| 624 | if (j == NULL) |
| 625 | return NULL; |
| 626 | |
| 627 | sub_res = PyObject_RichCompare(j, w, op); |
| 628 | Py_DECREF(j); |
| 629 | return sub_res; |
| 630 | } |
| 631 | else { |
| 632 | equal = 0; |
| 633 | } |
| 634 | } |
| 635 | else if (PyFloat_Check(w)) { |
| 636 | equal = (i.real == PyFloat_AsDouble(w) && i.imag == 0.0); |
| 637 | } |
| 638 | else if (PyComplex_Check(w)) { |
| 639 | Py_complex j; |
| 640 | |
| 641 | TO_COMPLEX(w, j); |
| 642 | equal = (i.real == j.real && i.imag == j.imag); |
| 643 | } |
| 644 | else { |
| 645 | goto Unimplemented; |
| 646 | } |
| 647 | |
| 648 | if (equal == (op == Py_EQ)) |
| 649 | res = Py_True; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 650 | else |
Mark Dickinson | cc6a982 | 2010-05-21 14:55:26 +0000 | [diff] [blame] | 651 | res = Py_False; |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 652 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 653 | Py_INCREF(res); |
| 654 | return res; |
Mark Dickinson | cc6a982 | 2010-05-21 14:55:26 +0000 | [diff] [blame] | 655 | |
| 656 | Unimplemented: |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 657 | Py_RETURN_NOTIMPLEMENTED; |
Guido van Rossum | be4cbb1 | 2001-01-18 01:12:39 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 661 | complex_int(PyObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 662 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 663 | PyErr_SetString(PyExc_TypeError, |
| 664 | "can't convert complex to int"); |
| 665 | return NULL; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 668 | static PyObject * |
Fred Drake | 4288c80 | 2000-07-09 04:36:04 +0000 | [diff] [blame] | 669 | complex_float(PyObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 670 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 671 | PyErr_SetString(PyExc_TypeError, |
| 672 | "can't convert complex to float"); |
| 673 | return NULL; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 676 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 677 | complex_conjugate(PyObject *self) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 678 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 679 | Py_complex c; |
| 680 | c = ((PyComplexObject *)self)->cval; |
| 681 | c.imag = -c.imag; |
| 682 | return PyComplex_FromCComplex(c); |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Guido van Rossum | 46334cd | 2007-08-01 17:43:15 +0000 | [diff] [blame] | 685 | PyDoc_STRVAR(complex_conjugate_doc, |
| 686 | "complex.conjugate() -> complex\n" |
| 687 | "\n" |
Ezio Melotti | 488d244 | 2013-10-06 00:39:18 +0300 | [diff] [blame] | 688 | "Return the complex conjugate of its argument. (3-4j).conjugate() == 3+4j."); |
Guido van Rossum | 46334cd | 2007-08-01 17:43:15 +0000 | [diff] [blame] | 689 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 690 | static PyObject * |
| 691 | complex_getnewargs(PyComplexObject *v) |
| 692 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 693 | Py_complex c = v->cval; |
| 694 | return Py_BuildValue("(dd)", c.real, c.imag); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Eric Smith | 58a4224 | 2009-04-30 01:00:33 +0000 | [diff] [blame] | 697 | PyDoc_STRVAR(complex__format__doc, |
| 698 | "complex.__format__() -> str\n" |
| 699 | "\n" |
Ezio Melotti | 488d244 | 2013-10-06 00:39:18 +0300 | [diff] [blame] | 700 | "Convert to a string according to format_spec."); |
Eric Smith | 58a4224 | 2009-04-30 01:00:33 +0000 | [diff] [blame] | 701 | |
| 702 | static PyObject * |
| 703 | complex__format__(PyObject* self, PyObject* args) |
| 704 | { |
| 705 | PyObject *format_spec; |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 706 | _PyUnicodeWriter writer; |
| 707 | int ret; |
Eric Smith | 58a4224 | 2009-04-30 01:00:33 +0000 | [diff] [blame] | 708 | |
| 709 | if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 710 | return NULL; |
| 711 | |
Victor Stinner | 8f674cc | 2013-04-17 23:02:17 +0200 | [diff] [blame] | 712 | _PyUnicodeWriter_Init(&writer); |
Victor Stinner | d3f0882 | 2012-05-29 12:57:52 +0200 | [diff] [blame] | 713 | ret = _PyComplex_FormatAdvancedWriter( |
| 714 | &writer, |
| 715 | self, |
| 716 | format_spec, 0, PyUnicode_GET_LENGTH(format_spec)); |
| 717 | if (ret == -1) { |
| 718 | _PyUnicodeWriter_Dealloc(&writer); |
| 719 | return NULL; |
| 720 | } |
| 721 | return _PyUnicodeWriter_Finish(&writer); |
Eric Smith | 58a4224 | 2009-04-30 01:00:33 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 724 | #if 0 |
| 725 | static PyObject * |
| 726 | complex_is_finite(PyObject *self) |
| 727 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 728 | Py_complex c; |
| 729 | c = ((PyComplexObject *)self)->cval; |
| 730 | return PyBool_FromLong((long)(Py_IS_FINITE(c.real) && |
| 731 | Py_IS_FINITE(c.imag))); |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | PyDoc_STRVAR(complex_is_finite_doc, |
| 735 | "complex.is_finite() -> bool\n" |
| 736 | "\n" |
| 737 | "Returns True if the real and the imaginary part is finite."); |
| 738 | #endif |
| 739 | |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 740 | static PyMethodDef complex_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 741 | {"conjugate", (PyCFunction)complex_conjugate, METH_NOARGS, |
| 742 | complex_conjugate_doc}, |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 743 | #if 0 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 744 | {"is_finite", (PyCFunction)complex_is_finite, METH_NOARGS, |
| 745 | complex_is_finite_doc}, |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 746 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 747 | {"__getnewargs__", (PyCFunction)complex_getnewargs, METH_NOARGS}, |
| 748 | {"__format__", (PyCFunction)complex__format__, |
| 749 | METH_VARARGS, complex__format__doc}, |
| 750 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 751 | }; |
| 752 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 753 | static PyMemberDef complex_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 754 | {"real", T_DOUBLE, offsetof(PyComplexObject, cval.real), READONLY, |
| 755 | "the real part of a complex number"}, |
| 756 | {"imag", T_DOUBLE, offsetof(PyComplexObject, cval.imag), READONLY, |
| 757 | "the imaginary part of a complex number"}, |
| 758 | {0}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 759 | }; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 760 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 761 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 762 | complex_subtype_from_string(PyTypeObject *type, PyObject *v) |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 763 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 764 | const char *s, *start; |
| 765 | char *end; |
| 766 | double x=0.0, y=0.0, z; |
| 767 | int got_bracket=0; |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 768 | PyObject *s_buffer = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 769 | Py_ssize_t len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 770 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 771 | if (PyUnicode_Check(v)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 772 | s_buffer = _PyUnicode_TransformDecimalAndSpaceToASCII(v); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 773 | if (s_buffer == NULL) |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 774 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 775 | s = PyUnicode_AsUTF8AndSize(s_buffer, &len); |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 776 | if (s == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 777 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 778 | } |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 779 | else { |
Ezio Melotti | a5b9599 | 2013-11-07 19:18:34 +0200 | [diff] [blame] | 780 | PyErr_Format(PyExc_TypeError, |
| 781 | "complex() argument must be a string or a number, not '%.200s'", |
| 782 | Py_TYPE(v)->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 783 | return NULL; |
| 784 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 785 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 786 | /* position on first nonblank */ |
| 787 | start = s; |
| 788 | while (Py_ISSPACE(*s)) |
| 789 | s++; |
| 790 | if (*s == '(') { |
| 791 | /* Skip over possible bracket from repr(). */ |
| 792 | got_bracket = 1; |
| 793 | s++; |
| 794 | while (Py_ISSPACE(*s)) |
| 795 | s++; |
| 796 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 797 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 798 | /* a valid complex string usually takes one of the three forms: |
Mark Dickinson | 6649fa4 | 2009-04-24 13:25:20 +0000 | [diff] [blame] | 799 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 800 | <float> - real part only |
| 801 | <float>j - imaginary part only |
| 802 | <float><signed-float>j - real and imaginary parts |
Mark Dickinson | 6649fa4 | 2009-04-24 13:25:20 +0000 | [diff] [blame] | 803 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 804 | where <float> represents any numeric string that's accepted by the |
| 805 | float constructor (including 'nan', 'inf', 'infinity', etc.), and |
| 806 | <signed-float> is any string of the form <float> whose first |
| 807 | character is '+' or '-'. |
Mark Dickinson | 6649fa4 | 2009-04-24 13:25:20 +0000 | [diff] [blame] | 808 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 809 | For backwards compatibility, the extra forms |
Mark Dickinson | 6649fa4 | 2009-04-24 13:25:20 +0000 | [diff] [blame] | 810 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 811 | <float><sign>j |
| 812 | <sign>j |
| 813 | j |
Mark Dickinson | 6649fa4 | 2009-04-24 13:25:20 +0000 | [diff] [blame] | 814 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 815 | are also accepted, though support for these forms may be removed from |
| 816 | a future version of Python. |
| 817 | */ |
Mark Dickinson | 6649fa4 | 2009-04-24 13:25:20 +0000 | [diff] [blame] | 818 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 819 | /* first look for forms starting with <float> */ |
| 820 | z = PyOS_string_to_double(s, &end, NULL); |
| 821 | if (z == -1.0 && PyErr_Occurred()) { |
| 822 | if (PyErr_ExceptionMatches(PyExc_ValueError)) |
| 823 | PyErr_Clear(); |
| 824 | else |
| 825 | goto error; |
| 826 | } |
| 827 | if (end != s) { |
| 828 | /* all 4 forms starting with <float> land here */ |
| 829 | s = end; |
| 830 | if (*s == '+' || *s == '-') { |
| 831 | /* <float><signed-float>j | <float><sign>j */ |
| 832 | x = z; |
| 833 | y = PyOS_string_to_double(s, &end, NULL); |
| 834 | if (y == -1.0 && PyErr_Occurred()) { |
| 835 | if (PyErr_ExceptionMatches(PyExc_ValueError)) |
| 836 | PyErr_Clear(); |
| 837 | else |
| 838 | goto error; |
| 839 | } |
| 840 | if (end != s) |
| 841 | /* <float><signed-float>j */ |
| 842 | s = end; |
| 843 | else { |
| 844 | /* <float><sign>j */ |
| 845 | y = *s == '+' ? 1.0 : -1.0; |
| 846 | s++; |
| 847 | } |
| 848 | if (!(*s == 'j' || *s == 'J')) |
| 849 | goto parse_error; |
| 850 | s++; |
| 851 | } |
| 852 | else if (*s == 'j' || *s == 'J') { |
| 853 | /* <float>j */ |
| 854 | s++; |
| 855 | y = z; |
| 856 | } |
| 857 | else |
| 858 | /* <float> */ |
| 859 | x = z; |
| 860 | } |
| 861 | else { |
| 862 | /* not starting with <float>; must be <sign>j or j */ |
| 863 | if (*s == '+' || *s == '-') { |
| 864 | /* <sign>j */ |
| 865 | y = *s == '+' ? 1.0 : -1.0; |
| 866 | s++; |
| 867 | } |
| 868 | else |
| 869 | /* j */ |
| 870 | y = 1.0; |
| 871 | if (!(*s == 'j' || *s == 'J')) |
| 872 | goto parse_error; |
| 873 | s++; |
| 874 | } |
Mark Dickinson | ad476da | 2009-04-23 19:14:16 +0000 | [diff] [blame] | 875 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 876 | /* trailing whitespace and closing bracket */ |
| 877 | while (Py_ISSPACE(*s)) |
| 878 | s++; |
| 879 | if (got_bracket) { |
| 880 | /* if there was an opening parenthesis, then the corresponding |
| 881 | closing parenthesis should be right here */ |
| 882 | if (*s != ')') |
| 883 | goto parse_error; |
| 884 | s++; |
| 885 | while (Py_ISSPACE(*s)) |
| 886 | s++; |
| 887 | } |
Mark Dickinson | 6649fa4 | 2009-04-24 13:25:20 +0000 | [diff] [blame] | 888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 889 | /* we should now be at the end of the string */ |
| 890 | if (s-start != len) |
| 891 | goto parse_error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 892 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 893 | Py_XDECREF(s_buffer); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 894 | return complex_subtype_from_doubles(type, x, y); |
Mark Dickinson | ad476da | 2009-04-23 19:14:16 +0000 | [diff] [blame] | 895 | |
Mark Dickinson | 6649fa4 | 2009-04-24 13:25:20 +0000 | [diff] [blame] | 896 | parse_error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 897 | PyErr_SetString(PyExc_ValueError, |
| 898 | "complex() arg is a malformed string"); |
Mark Dickinson | 1daebdf | 2009-10-26 22:05:06 +0000 | [diff] [blame] | 899 | error: |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 900 | Py_XDECREF(s_buffer); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 901 | return NULL; |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 902 | } |
| 903 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 904 | static PyObject * |
| 905 | complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 906 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 907 | PyObject *r, *i, *tmp; |
| 908 | PyNumberMethods *nbr, *nbi = NULL; |
| 909 | Py_complex cr, ci; |
| 910 | int own_r = 0; |
| 911 | int cr_is_complex = 0; |
| 912 | int ci_is_complex = 0; |
| 913 | static char *kwlist[] = {"real", "imag", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 914 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 915 | r = Py_False; |
| 916 | i = NULL; |
| 917 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:complex", kwlist, |
| 918 | &r, &i)) |
| 919 | return NULL; |
Raymond Hettinger | 604cd6a | 2002-08-29 14:22:51 +0000 | [diff] [blame] | 920 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 921 | /* Special-case for a single argument when type(arg) is complex. */ |
| 922 | if (PyComplex_CheckExact(r) && i == NULL && |
| 923 | type == &PyComplex_Type) { |
| 924 | /* Note that we can't know whether it's safe to return |
| 925 | a complex *subclass* instance as-is, hence the restriction |
| 926 | to exact complexes here. If either the input or the |
| 927 | output is a complex subclass, it will be handled below |
| 928 | as a non-orthogonal vector. */ |
| 929 | Py_INCREF(r); |
| 930 | return r; |
| 931 | } |
| 932 | if (PyUnicode_Check(r)) { |
| 933 | if (i != NULL) { |
| 934 | PyErr_SetString(PyExc_TypeError, |
| 935 | "complex() can't take second arg" |
| 936 | " if first is a string"); |
| 937 | return NULL; |
| 938 | } |
| 939 | return complex_subtype_from_string(type, r); |
| 940 | } |
| 941 | if (i != NULL && PyUnicode_Check(i)) { |
| 942 | PyErr_SetString(PyExc_TypeError, |
| 943 | "complex() second arg can't be a string"); |
| 944 | return NULL; |
| 945 | } |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 946 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 947 | tmp = try_complex_special_method(r); |
| 948 | if (tmp) { |
| 949 | r = tmp; |
| 950 | own_r = 1; |
| 951 | } |
| 952 | else if (PyErr_Occurred()) { |
| 953 | return NULL; |
| 954 | } |
Benjamin Peterson | aea4428 | 2010-01-04 01:10:28 +0000 | [diff] [blame] | 955 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 956 | nbr = r->ob_type->tp_as_number; |
| 957 | if (i != NULL) |
| 958 | nbi = i->ob_type->tp_as_number; |
| 959 | if (nbr == NULL || nbr->nb_float == NULL || |
| 960 | ((i != NULL) && (nbi == NULL || nbi->nb_float == NULL))) { |
Ezio Melotti | a5b9599 | 2013-11-07 19:18:34 +0200 | [diff] [blame] | 961 | PyErr_Format(PyExc_TypeError, |
| 962 | "complex() argument must be a string or a number, not '%.200s'", |
| 963 | Py_TYPE(r)->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 964 | if (own_r) { |
| 965 | Py_DECREF(r); |
| 966 | } |
| 967 | return NULL; |
| 968 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 969 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 970 | /* If we get this far, then the "real" and "imag" parts should |
| 971 | both be treated as numbers, and the constructor should return a |
| 972 | complex number equal to (real + imag*1j). |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 973 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 974 | Note that we do NOT assume the input to already be in canonical |
| 975 | form; the "real" and "imag" parts might themselves be complex |
| 976 | numbers, which slightly complicates the code below. */ |
| 977 | if (PyComplex_Check(r)) { |
| 978 | /* Note that if r is of a complex subtype, we're only |
| 979 | retaining its real & imag parts here, and the return |
| 980 | value is (properly) of the builtin complex type. */ |
| 981 | cr = ((PyComplexObject*)r)->cval; |
| 982 | cr_is_complex = 1; |
| 983 | if (own_r) { |
| 984 | Py_DECREF(r); |
| 985 | } |
| 986 | } |
| 987 | else { |
| 988 | /* The "real" part really is entirely real, and contributes |
| 989 | nothing in the imaginary direction. |
| 990 | Just treat it as a double. */ |
| 991 | tmp = PyNumber_Float(r); |
| 992 | if (own_r) { |
| 993 | /* r was a newly created complex number, rather |
| 994 | than the original "real" argument. */ |
| 995 | Py_DECREF(r); |
| 996 | } |
| 997 | if (tmp == NULL) |
| 998 | return NULL; |
| 999 | if (!PyFloat_Check(tmp)) { |
| 1000 | PyErr_SetString(PyExc_TypeError, |
| 1001 | "float(r) didn't return a float"); |
| 1002 | Py_DECREF(tmp); |
| 1003 | return NULL; |
| 1004 | } |
| 1005 | cr.real = PyFloat_AsDouble(tmp); |
| 1006 | cr.imag = 0.0; /* Shut up compiler warning */ |
| 1007 | Py_DECREF(tmp); |
| 1008 | } |
| 1009 | if (i == NULL) { |
| 1010 | ci.real = 0.0; |
| 1011 | } |
| 1012 | else if (PyComplex_Check(i)) { |
| 1013 | ci = ((PyComplexObject*)i)->cval; |
| 1014 | ci_is_complex = 1; |
| 1015 | } else { |
| 1016 | /* The "imag" part really is entirely imaginary, and |
| 1017 | contributes nothing in the real direction. |
| 1018 | Just treat it as a double. */ |
| 1019 | tmp = (*nbi->nb_float)(i); |
| 1020 | if (tmp == NULL) |
| 1021 | return NULL; |
| 1022 | ci.real = PyFloat_AsDouble(tmp); |
| 1023 | Py_DECREF(tmp); |
| 1024 | } |
| 1025 | /* If the input was in canonical form, then the "real" and "imag" |
| 1026 | parts are real numbers, so that ci.imag and cr.imag are zero. |
| 1027 | We need this correction in case they were not real numbers. */ |
Christian Heimes | 69a7963 | 2007-11-28 10:04:30 +0000 | [diff] [blame] | 1028 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1029 | if (ci_is_complex) { |
| 1030 | cr.real -= ci.imag; |
| 1031 | } |
| 1032 | if (cr_is_complex) { |
| 1033 | ci.real += cr.imag; |
| 1034 | } |
| 1035 | return complex_subtype_from_doubles(type, cr.real, ci.real); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1038 | PyDoc_STRVAR(complex_doc, |
Tim Peters | 2400fa4 | 2001-09-12 19:12:49 +0000 | [diff] [blame] | 1039 | "complex(real[, imag]) -> complex number\n" |
| 1040 | "\n" |
| 1041 | "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] | 1042 | "This is equivalent to (real + imag*1j) where imag defaults to 0."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1043 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1044 | static PyNumberMethods complex_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1045 | (binaryfunc)complex_add, /* nb_add */ |
| 1046 | (binaryfunc)complex_sub, /* nb_subtract */ |
| 1047 | (binaryfunc)complex_mul, /* nb_multiply */ |
| 1048 | (binaryfunc)complex_remainder, /* nb_remainder */ |
| 1049 | (binaryfunc)complex_divmod, /* nb_divmod */ |
| 1050 | (ternaryfunc)complex_pow, /* nb_power */ |
| 1051 | (unaryfunc)complex_neg, /* nb_negative */ |
| 1052 | (unaryfunc)complex_pos, /* nb_positive */ |
| 1053 | (unaryfunc)complex_abs, /* nb_absolute */ |
| 1054 | (inquiry)complex_bool, /* nb_bool */ |
| 1055 | 0, /* nb_invert */ |
| 1056 | 0, /* nb_lshift */ |
| 1057 | 0, /* nb_rshift */ |
| 1058 | 0, /* nb_and */ |
| 1059 | 0, /* nb_xor */ |
| 1060 | 0, /* nb_or */ |
| 1061 | complex_int, /* nb_int */ |
| 1062 | 0, /* nb_reserved */ |
| 1063 | complex_float, /* nb_float */ |
| 1064 | 0, /* nb_inplace_add */ |
| 1065 | 0, /* nb_inplace_subtract */ |
| 1066 | 0, /* nb_inplace_multiply*/ |
| 1067 | 0, /* nb_inplace_remainder */ |
| 1068 | 0, /* nb_inplace_power */ |
| 1069 | 0, /* nb_inplace_lshift */ |
| 1070 | 0, /* nb_inplace_rshift */ |
| 1071 | 0, /* nb_inplace_and */ |
| 1072 | 0, /* nb_inplace_xor */ |
| 1073 | 0, /* nb_inplace_or */ |
| 1074 | (binaryfunc)complex_int_div, /* nb_floor_divide */ |
| 1075 | (binaryfunc)complex_div, /* nb_true_divide */ |
| 1076 | 0, /* nb_inplace_floor_divide */ |
| 1077 | 0, /* nb_inplace_true_divide */ |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 1078 | }; |
| 1079 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1080 | PyTypeObject PyComplex_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1081 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1082 | "complex", |
| 1083 | sizeof(PyComplexObject), |
| 1084 | 0, |
| 1085 | complex_dealloc, /* tp_dealloc */ |
| 1086 | 0, /* tp_print */ |
| 1087 | 0, /* tp_getattr */ |
| 1088 | 0, /* tp_setattr */ |
| 1089 | 0, /* tp_reserved */ |
| 1090 | (reprfunc)complex_repr, /* tp_repr */ |
| 1091 | &complex_as_number, /* tp_as_number */ |
| 1092 | 0, /* tp_as_sequence */ |
| 1093 | 0, /* tp_as_mapping */ |
| 1094 | (hashfunc)complex_hash, /* tp_hash */ |
| 1095 | 0, /* tp_call */ |
Mark Dickinson | 388122d | 2010-08-04 20:56:28 +0000 | [diff] [blame] | 1096 | (reprfunc)complex_repr, /* tp_str */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1097 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1098 | 0, /* tp_setattro */ |
| 1099 | 0, /* tp_as_buffer */ |
| 1100 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 1101 | complex_doc, /* tp_doc */ |
| 1102 | 0, /* tp_traverse */ |
| 1103 | 0, /* tp_clear */ |
| 1104 | complex_richcompare, /* tp_richcompare */ |
| 1105 | 0, /* tp_weaklistoffset */ |
| 1106 | 0, /* tp_iter */ |
| 1107 | 0, /* tp_iternext */ |
| 1108 | complex_methods, /* tp_methods */ |
| 1109 | complex_members, /* tp_members */ |
| 1110 | 0, /* tp_getset */ |
| 1111 | 0, /* tp_base */ |
| 1112 | 0, /* tp_dict */ |
| 1113 | 0, /* tp_descr_get */ |
| 1114 | 0, /* tp_descr_set */ |
| 1115 | 0, /* tp_dictoffset */ |
| 1116 | 0, /* tp_init */ |
| 1117 | PyType_GenericAlloc, /* tp_alloc */ |
| 1118 | complex_new, /* tp_new */ |
| 1119 | PyObject_Del, /* tp_free */ |
Guido van Rossum | f9fca92 | 1996-01-12 00:47:05 +0000 | [diff] [blame] | 1120 | }; |