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