Python 整数值可以自动提升为 long 以获得任意精度:
>>> (10**300)**2
1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
由于IEEE 浮点的限制,浮点值溢出:
>>> float(10**300)**2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: (34, 'Result too large')
Python 复合体的组件是一个 float 类,同样会溢出:
>>> isinstance(complex(1).real,float)
True
>>> isinstance(complex(1).imag,float)
True
取通常的最大双精度值:
>>> max_double=1.7976931348623157e+308
执行大多数在浮点范围内增加该值的步骤,你会得到inf
:
>>> max_double*10
inf
>>> max_double*max_double
inf
>>> max_double*max_double*max_double*max_double
inf
>>> max_double++10**(308-15)
inf
如果在 FP 窗口之外,尾数和指数不会改变:
>>> md+10**(308-17)
1.7976931348623157e+308
>>> max_double**1.0000000000000001
1.7976931348623157e+308
可以看到溢出:
>>> max_double**1.000000000000001
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: (34, 'Result too large')
但正如文件中所述,它的应用不一致:
由于 C 中浮点异常处理缺乏标准化,大多数浮点运算也没有被检查。
可以在这里看到:
>>> (1e300+1e300j)*(1e300+1e300j)
(nan+infj)
>>> (1e300+1e300j)**2
(nan+nanj)