复数对象¶
-
type PyComplexObject¶
这个C类型
PyObject的子类型代表一个 Python 复数对象。-
Py_complex cval¶
The complex number value, using the C
Py_complexrepresentation.Deprecated since version 3.15, will be removed in version 3.20: Use
PyComplex_AsCComplex()andPyComplex_FromCComplex()to convert a Python complex number to/from the CPy_complexrepresentation.
-
Py_complex cval¶
-
PyTypeObject PyComplex_Type¶
- 属于 稳定 ABI.
这是个属于
PyTypeObject的代表Python复数类型的实例。在Python层面的类型complex是同一个对象。
-
int PyComplex_Check(PyObject *p)¶
如果它的参数是一个
PyComplexObject或者PyComplexObject的子类型则返回真值。 此函数总是会成功执行。
-
int PyComplex_CheckExact(PyObject *p)¶
如果它的参数是一个
PyComplexObject但不是PyComplexObject的子类型则返回真值。 此函数总是会成功执行。
-
PyObject *PyComplex_FromDoubles(double real, double imag)¶
- 返回值:新的引用。 属于 稳定 ABI.
根据 real 和 imag 返回一个新的
PyComplexObject对象。 当发生错误时将返回NULL并设置一个异常。
-
double PyComplex_RealAsDouble(PyObject *op)¶
- 属于 稳定 ABI.
以 C 类型 double 返回 op 的实部。
如果 op 不是一个 Python 复数对象但是具有
__complex__()方法,则会先调用该方法将 op 转换为 Python 复数对象。 如果未定义__complex__()则将回退为调用PyFloat_AsDouble()并返回其结果。当失败时,此方法将返回
-1.0并设置一个异常,因此开发者应当调用PyErr_Occurred()来检查错误。在 3.13 版本发生变更: 如果可能将使用
__complex__()。
-
double PyComplex_ImagAsDouble(PyObject *op)¶
- 属于 稳定 ABI.
以 C 类型 double 返回 op 的虚部。
如果 op 不是一个 Python 复数对象但是具有
__complex__()方法,则会先调用该方法将 op 转换为 Python 复数对象。 如果未定义__complex__()则将回退为调用PyFloat_AsDouble()并在成功时返回0.0。当失败时,此方法将返回
-1.0并设置一个异常,因此开发者应当调用PyErr_Occurred()来检查错误。在 3.13 版本发生变更: 如果可能将使用
__complex__()。
-
type Py_complex¶
This C structure defines an export format for a Python complex number object.
其结构定义如下:
typedef struct { double real; double imag; } Py_complex;
-
PyObject *PyComplex_FromCComplex(Py_complex v)¶
- 返回值:新的引用。
根据一个 C
Py_complex值新建 Python 复数对象。 当发生错误时将返回NULL并设置一个异常。
-
Py_complex PyComplex_AsCComplex(PyObject *op)¶
返回复数 op 的C类型
Py_complex值。如果 op 不是一个 Python 复数对象但是具有
__complex__()方法,则会先调用该方法将 op 转换为 Python 复数对象。 如果未定义__complex__()则将回退至__float__()。 如果未定义__float__()则将回退至__index__()。当失败时,此方法将返回
Py_complex其中real为-1.0并设置一个异常,因此开发者应当调用PyErr_Occurred()来检查错误。在 3.8 版本发生变更: 如果可能将使用
__index__()。
表示复数的C结构体¶
The API also provides functions for working with complex numbers, using the
Py_complex representation. Note that the functions which accept
these structures as parameters and return them as results do so by value
rather than dereferencing them through pointers.
Please note, that these functions are soft deprecated since Python 3.15. Avoid using this API in a new code to do complex arithmetic: either use the Number Protocol API or use native complex types, like double complex.
-
Py_complex _Py_c_sum(Py_complex left, Py_complex right)¶
返回两个复数的和,用 C 类型
Py_complex表示。自 3.15 版本弃用.
-
Py_complex _Py_c_diff(Py_complex left, Py_complex right)¶
返回两个复数的差,用 C 类型
Py_complex表示。自 3.15 版本弃用.
-
Py_complex _Py_c_neg(Py_complex num)¶
返回复数 num 的负值,用 C
Py_complex表示。自 3.15 版本弃用.
-
Py_complex _Py_c_prod(Py_complex left, Py_complex right)¶
返回两个复数的乘积,用 C 类型
Py_complex表示。自 3.15 版本弃用.
-
Py_complex _Py_c_quot(Py_complex dividend, Py_complex divisor)¶
返回两个复数的商,用 C 类型
Py_complex表示。如果 divisor 为空,则此方法将返回零并将
errno设为EDOM。自 3.15 版本弃用.
-
Py_complex _Py_c_pow(Py_complex num, Py_complex exp)¶
返回 num 的 exp 次幂,用 C 类型
Py_complex表示。如果 num 为空且 exp 不是正实数,则此方法将返回零并将
errno设为EDOM。当溢出时将
errno设为ERANGE。自 3.15 版本弃用.
-
double _Py_c_abs(Py_complex num)¶
Return the absolute value of the complex number num.
当溢出时将
errno设为ERANGE。自 3.15 版本弃用.