複數物件

type PyComplexObject

這個 PyObject 的子型別代表一個 Python 複數物件。

Py_complex cval

The complex number value, using the C Py_complex representation.

Deprecated since version 3.15.0a0 (unreleased), will be removed in version 3.20: Use PyComplex_AsCComplex() and PyComplex_FromCComplex() to convert a Python complex number to/from the C Py_complex representation.

PyTypeObject PyComplex_Type
穩定 ABI 的一部分.

這個 PyTypeObject 的實例代表 Python 複數型別。它與 Python 層中的 complex 是同一個物件。

int PyComplex_Check(PyObject *p)

如果其引數是一個 PyComplexObject 或者是 PyComplexObject 的子型別,則會回傳 true。這個函式不會失敗。

int PyComplex_CheckExact(PyObject *p)

如果其引數是一個 PyComplexObject,但不是 PyComplexObject 的子型別,則會回傳 true。這個函式不會失敗。

PyObject *PyComplex_FromDoubles(double real, double imag)
回傳值:新的參照。穩定 ABI 的一部分.

realimag 回傳一個新的 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 的一部分.

op 的虛部作為 C 的 double 回傳。

如果 op 不是 Python 複數物件,但有一個 __complex__() 方法,則首先會呼叫該方法將 op 轉換為 Python 複數物件。如果 __complex__() 並未定義,那麼它會回退到呼叫 PyFloat_AsDouble() 並於成功時回傳 0.0

失敗時,此方法回傳 -1.0 並設定例外,因此應該呼叫 PyErr_Occurred() 來檢查錯誤。

在 3.13 版的變更: 如果可用則使用 __complex__()

type Py_complex

This C structure defines export format for a Python complex number object.

double real
double imag

該結構被定義為:

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)

回傳複數 opPy_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)

以 C 的 Py_complex 表示形式來回傳複數 num 的相反數 (negation)。

在 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 為 null,則此方法會回傳零並將 errno 設定為 EDOM

在 3.15 版之後被棄用.

Py_complex _Py_c_pow(Py_complex num, Py_complex exp)

以 C 的 Py_complex 表示形式來回傳 numexp 次方的結果。

如果 num 為 null 且 exp 不是正實數,則此方法會回傳零並將 errno 設定為 EDOM

Set errno to ERANGE on overflows.

在 3.15 版之後被棄用.

double _Py_c_abs(Py_complex num)

Return the absolute value of the complex number num.

Set errno to ERANGE on overflows.

在 3.15 版之後被棄用.