在C,C和Java中,整数具有一定范围.我在Python中意识到的一件事是我可以计算出很大的整数,比如pow(2,100). C,pow(2,100)中的相同等效代码显然会导致溢出,因为在32位架构中,无符号整数类型的范围为0到2 ^ 32-1. Python如何计算这些大数字?
解决方法:
基本上,Python中的大数字存储在“数字”数组中.这是正确的,因为每个’数字’本身实际上可能是一个很大的数字. )
There are two different sets of parameters: one set for 30-bit digits,
stored in an unsigned 32-bit integer type, and one set for 15-bit
digits with each digit stored in an unsigned short. The value of
PYLONG_BITS_IN_DIGIT, defined either at configure time or in pyport.h,
is used to decide which digit size to use.
/* Long integer representation.
The absolute value of a number is equal to
SUM(for i=0 through abs(ob_size)-1) ob_digit[i] * 2**(SHIFT*i)
Negative numbers are represented with ob_size < 0;
zero is represented by ob_size == 0.
In a normalized number, ob_digit[abs(ob_size)-1] (the most significant
digit) is never zero. Also, in all cases, for all valid i,
0 <= ob_digit[i] <= MASK.
The allocation function takes care of allocating extra memory
so that ob_digit[0] ... ob_digit[abs(ob_size)-1] are actually available.
*/
struct _longobject {
PyObject_VAR_HEAD
digit ob_digit[1];
};
标签:python,integer,architecture
来源: https://codeday.me/bug/20190612/1225835.html