Objects/complexobject.c (part 4)
Source:
cpython 3.14 @ ab2d84fe1023/Objects/complexobject.c
This annotation covers methods and string parsing. See objects_complexobject3_detail for complex.__add__/__mul__/__truediv__, complex.__eq__, and complex.__hash__.
Map
| Lines | Symbol | Role |
|---|---|---|
| 1-60 | complex.__abs__ | abs(z) — Euclidean magnitude |
| 61-120 | complex.conjugate | z.conjugate() — real - imag*j |
| 121-220 | complex.__getnewargs__ | Pickle support — reconstruct as complex(real, imag) |
| 221-380 | complex_from_string | Parse '1+2j', '3j', '-4.5' strings |
| 381-500 | complex.__format__ | Format spec 'f', 'e', 'g' applied to each component |
Reading
complex.__abs__
// CPython: Objects/complexobject.c:580 complex_abs
static PyObject *
complex_abs(PyComplexObject *v)
{
/* hypot(real, imag) — handles overflow in intermediate computation */
double result = _Py_c_abs(v->cval);
if (result == Py_HUGE_VAL) {
PyErr_SetString(PyExc_OverflowError,
"absolute value too large");
return NULL;
}
return PyFloat_FromDouble(result);
}
abs(3+4j) returns 5.0. _Py_c_abs is hypot(real, imag) which handles intermediate overflow (e.g., abs(1e308 + 1e308j)) better than sqrt(r*r + i*i).