检查python int是否太大而无法转换为float

时间:2023-02-03 18:20:28

Is there any way to check if a long integer is too large to convert to a float in python?

有没有办法检查一个长整数是否太大,无法转换为python中的浮点数?

1 个解决方案

#1


13  

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

Actually, if you try to convert an integer too big to a float, an exception will be raised.

实际上,如果您尝试将一个太大的整数转换为浮点数,则会引发异常。

>>> 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

#1


13  

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

Actually, if you try to convert an integer too big to a float, an exception will be raised.

实际上,如果您尝试将一个太大的整数转换为浮点数,则会引发异常。

>>> 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