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