检查Python整数是否过大无法转换为浮点数

5 投票
1 回答
3039 浏览
提问于 2025-04-16 01:06

有没有什么方法可以检查一个长整型数字在Python中是否太大,无法转换成浮点数?

1 个回答

13
>>> import sys
>>> sys.float_info.max
1.7976931348623157e+308

其实,如果你试着把一个太大的整数转换成浮点数,系统会报错。

>>> float(2 * 10**308)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C double

撰写回答