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