Python整数值可以自动提升为长整型以实现任意精度:
>>> (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)
(1e300+1e300j) ** 100 ==> (nan+nanj); (1e300+1e300j) ** 101 ==> Traceback (most recent call last): File "<stdin>", line 1, in <module> OverflowError: complex exponentiation
- vroomfondel(1*10**300)**2
- dawg