Skip to main content
 首页 » 编程设计

python e**(-x) 溢出错误 : (34, 'Result too large' )

2025年05月04日16freeliver54

有没有办法在 python 中将小数部分截断为 5 或 7 位?

如果不是,我怎样才能避免像 e**(-x) 这样的 float 变得太大?

谢谢

请您参考如下方法:

捕获 OverflowError 或使用 decimal 模块。 Python 不会假定您对溢出没问题。

>>> 0.0000000000000000000000000000000000000000000000000000000000000001**-30 
Traceback (most recent call last): 
  File "<stdin>", line 1, in <module> 
OverflowError: (34, 'Result too large') 
>>> d = decimal.Decimal(0.0000000000000000000000000000000000000000000000000000000000000001) 
>>> d**-30 
Decimal('1.000000000000001040827834994E+1920')