本篇內容主要講解“Python虛擬機中復數的實現原理是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Python虛擬機中復數的實現原理是什么”吧!
在 cpython 當中對于復數的數據結構實現如下所示:
typedef struct { double real; double imag; } Py_complex; #define PyObject_HEAD PyObject ob_base; typedef struct { PyObject_HEAD Py_complex cval; } PyComplexObject; typedef struct _object { _PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt; struct _typeobject *ob_type; } PyObject;
上面的數據結構圖示如下:
復數的數據在整個 cpython 虛擬機當中來說應該算是比較簡單的了,除了一個 PyObject 頭部之外就是實部和虛部了。
ob_refcnt,表示對象的引用記數的個數,這個對于垃圾回收很有用處,后面我們分析虛擬機中垃圾回收部分在深入分析。
ob_type,表示這個對象的數據類型是什么,在 python 當中有時候需要對數據的數據類型進行判斷比如 isinstance, type 這兩個關鍵字就會使用到這個字段。
real,表示復數的實部。
imag,表示復數的虛部。
下面是 cpython 當中對于復數加法的實現,為了簡潔刪除了部分無用代碼。
static PyObject * complex_add(PyObject *v, PyObject *w) { Py_complex result; Py_complex a, b; TO_COMPLEX(v, a); // TO_COMPLEX 這個宏的作用就是將一個 PyComplexObject 中的 Py_complex 對象存儲到 a 當中 TO_COMPLEX(w, b); result = _Py_c_sum(a, b); // 這個函數的具體實現在下方 return PyComplex_FromCComplex(result); // 這個函數的具體實現在下方 } // 真正實現復數加法的函數 Py_complex _Py_c_sum(Py_complex a, Py_complex b) { Py_complex r; r.real = a.real + b.real; r.imag = a.imag + b.imag; return r; } PyObject * PyComplex_FromCComplex(Py_complex cval) { PyComplexObject *op; /* Inline PyObject_New */ // 申請內存空間 op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject)); if (op == NULL) return PyErr_NoMemory(); // 將這個對象的引用計數設置成 1 (void)PyObject_INIT(op, &PyComplex_Type); // 將復數結構體保存下來 op->cval = cval; return (PyObject *) op; }
上面代碼的整體過程比較簡單:
首先先從 PyComplexObject 提取真正的復數部分。
將提取到的兩個復數進行相加操作。
根據得到的結果在創建一個 PyComplexObject 對象,并且將這個對象返回。
復數取反操作就是將實部和虛部取相反數就可以了,這個操作也比較簡單。
static PyObject * complex_neg(PyComplexObject *v) { Py_complex neg; neg.real = -v->cval.real; neg.imag = -v->cval.imag; return PyComplex_FromCComplex(neg); } PyObject * PyComplex_FromCComplex(Py_complex cval) { PyComplexObject *op; /* Inline PyObject_New */ op = (PyComplexObject *) PyObject_MALLOC(sizeof(PyComplexObject)); if (op == NULL) return PyErr_NoMemory(); (void)PyObject_INIT(op, &PyComplex_Type); op->cval = cval; return (PyObject *) op; }
我們現在來介紹一下一個有趣的方法,就是復數類型的 repr 函數,這個和類的 __repr__ 函數是作用是一樣的我們看一下復數的輸出是什么:
>>> data = complex(0, 1) >>> data 1j >>> data = complex(1, 1) >>> data (1+1j) >>> print(data) (1+1j)
復數的 repr 對應的 C 函數如下所示:
static PyObject * complex_repr(PyComplexObject *v) { int precision = 0; char format_code = 'r'; PyObject *result = NULL; /* If these are non-NULL, they'll need to be freed. */ char *pre = NULL; char *im = NULL; /* These do not need to be freed. re is either an alias for pre or a pointer to a constant. lead and tail are pointers to constants. */ char *re = NULL; char *lead = ""; char *tail = ""; // 對應實部等于 0 虛部大于 0 的情況 if (v->cval.real == 0. && copysign(1.0, v->cval.real)==1.0) { /* Real part is +0: just output the imaginary part and do not include parens. */ re = ""; im = PyOS_double_to_string(v->cval.imag, format_code, precision, 0, NULL); if (!im) { PyErr_NoMemory(); goto done; } } else { /* Format imaginary part with sign, real part without. Include parens in the result. */ // 將實部浮點數變成字符串 pre = PyOS_double_to_string(v->cval.real, format_code, precision, 0, NULL); if (!pre) { PyErr_NoMemory(); goto done; } re = pre; // 將虛部浮點數變成字符串 im = PyOS_double_to_string(v->cval.imag, format_code, precision, Py_DTSF_SIGN, NULL); if (!im) { PyErr_NoMemory(); goto done; } // 用什么括號包圍起來 lead = "("; tail = ")"; } result = PyUnicode_FromFormat("%s%s%sj%s", lead, re, im, tail); done: PyMem_Free(im); PyMem_Free(pre); return result; }
我們現在修改源程序將上面的 () 兩個括號變成 [],編譯之后執行的結果如下所示:
可以看到括號變成了 [] 。
到此,相信大家對“Python虛擬機中復數的實現原理是什么”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。