为什么在python中整数的值没有限制_python-为什么math.log接受大整数值?
我終于鉆入python math lib source code,發現了這一點:
/* A decent logarithm is easy to compute even for huge ints, but libm can't
do that by itself -- loghelper can. func is log or log10, and name is
"log" or "log10". Note that overflow of the result isn't possible: an int
can contain no more than INT_MAX * SHIFT bits, so has value certainly less
than 2**(2**64 * 2**16) == 2**2**80, and log2 of that is 2**80, which is
small enough to fit in an IEEE single. log and log10 are even smaller.
However, intermediate overflow is possible for an int if the number of bits
in that int is larger than PY_SSIZE_T_MAX. */
static PyObject*
loghelper(PyObject* arg, double (*func)(double), const char *funcname)
{
/* If it is int, do it ourselves. */
if (PyLong_Check(arg)) {
double x, result;
Py_ssize_t e;
...
我會保留其余的源代碼(檢查鏈接),但是據我了解,Python會檢查傳遞的參數是否為整數,如果是,則不要使用math lib(如果為int,自己做.)評論.另外:即使對于很大的整數,也很容易計算出不錯的對數,但是libm本身不能做到這一點-loghelper可以
如果是雙精度數,則調用本地數學庫.
從源注釋中可以看出,即使發生溢出,Python也會盡最大努力提供結果(此處轉換為兩次溢出,但仍可以計算日志.清除異常并繼續)
因此,由于使用了log函數的python包裝,Python能夠計算巨大整數的對數(特定于某些函數,因為sqrt等其他函數無法做到),并且已在文檔中進行了記載,但僅在源代碼中有記錄.正如Jon所暗示的那樣,使其成為實現細節.
總結
以上是生活随笔為你收集整理的为什么在python中整数的值没有限制_python-为什么math.log接受大整数值?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql load data infi
- 下一篇: 主成分分析法案例_因子分析案例及操作解析