gpt4 book ai didi

python - 如何解决溢出错误: math range error

转载 作者:太空宇宙 更新时间:2023-11-03 21:20:38 25 4
gpt4 key购买 nike

我收到溢出错误

import numpy as np

pi = np.pi

from scipy.integrate import quad
from math import exp

hbar = 1.055e-34
boltz = 1.381e-23
c = 2.998e8

def z(x):
return (x**3)/(exp(x)-1)

B=quad(z,0,np.inf)
A= ((boltz**4)*B)/(4*(pi**2)*(c**2)*(hbar**3))

print (A)

它在第 11 行给了我一个溢出错误,即 return (x**3)/(exp(x)-1)

最佳答案

你达到了机器精度,Python 就崩溃了。

>>> def z(x):
... return (x**3)/(exp(x)-1)
...
>>> z(709)
4.336616682334302e-300
>>> z(710)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in z
OverflowError: math range error

只要积分到 ~700 就可以了。

关于python - 如何解决溢出错误: math range error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54283309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com