Python溢出误差乘法
Python Overflow error multiplication
我正在处理的方程式的代码结构是这样的。
import matplotlib.pyplot as plt
for x in range (0 ,20):
temp = x * (-1*10**(-9)*(2.73**(x/0.0258*0.8) - 1) + 3.1)
P.append(temp)
Q.append(x)
print temp
plt.plot(Q,P)
plt.show()
打印 temp
给了我这个
4.759377049180328889121938118
-33447.32349862001706705983714
-2238083697441414267.104517188
-1.123028419942448387512537968E+32
-5.009018636753031534804021565E+45
-2.094526332030486492065138064E+59
-8.407952213322881981287736804E+72
-3.281407666305436036872349205E+86
-1.254513385166959745710275399E+100
-4.721184644539803475363811828E+113
-1.754816222227633792004755288E+127
-6.457248346728221564046430946E+140
-2.356455347384037854507854340E+154
-8.539736787129928434375037129E+167
-3.076467506425168063232368199E+181
-1.102652635599075169095479067E+195
-3.934509583907661118429424988E+208
-1.398436369682635574296418585E+222
-4.953240988408539700713401539E+235
-1.749015740500628326472633516E+249
显示的结果非常不准确。我知道这是因为,获得的图表不是我应该得到的。在 google 中快速绘制相同的方程式给了我这个
This pic shows the differences in the graphs
实际情节是 google.com 情节。
我相当确定错误是由于浮点计算引起的。有人可以帮我更正方程式吗?
从 0.7 左右开始,您的分数降为零。 Google 非常聪明地解决了这个问题并将 y 轴限制在合理的范围内。在 matplotlib 中,您必须手动设置此比例。
另请注意,您绘制的是从 0 到 19 的整数。绘制连续函数时,区间中的线性间隔点通常更有意义。
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 0.8, 100)
y = x * (-1e-9 *(2.73**(x/0.0258*0.8) - 1) + 3.1)
plt.plot(x,y)
plt.ylim(-0.5, 2)
我正在处理的方程式的代码结构是这样的。
import matplotlib.pyplot as plt
for x in range (0 ,20):
temp = x * (-1*10**(-9)*(2.73**(x/0.0258*0.8) - 1) + 3.1)
P.append(temp)
Q.append(x)
print temp
plt.plot(Q,P)
plt.show()
打印 temp
给了我这个
4.759377049180328889121938118
-33447.32349862001706705983714
-2238083697441414267.104517188
-1.123028419942448387512537968E+32
-5.009018636753031534804021565E+45
-2.094526332030486492065138064E+59
-8.407952213322881981287736804E+72
-3.281407666305436036872349205E+86
-1.254513385166959745710275399E+100
-4.721184644539803475363811828E+113
-1.754816222227633792004755288E+127
-6.457248346728221564046430946E+140
-2.356455347384037854507854340E+154
-8.539736787129928434375037129E+167
-3.076467506425168063232368199E+181
-1.102652635599075169095479067E+195
-3.934509583907661118429424988E+208
-1.398436369682635574296418585E+222
-4.953240988408539700713401539E+235
-1.749015740500628326472633516E+249
显示的结果非常不准确。我知道这是因为,获得的图表不是我应该得到的。在 google 中快速绘制相同的方程式给了我这个
This pic shows the differences in the graphs 实际情节是 google.com 情节。 我相当确定错误是由于浮点计算引起的。有人可以帮我更正方程式吗?
从 0.7 左右开始,您的分数降为零。 Google 非常聪明地解决了这个问题并将 y 轴限制在合理的范围内。在 matplotlib 中,您必须手动设置此比例。
另请注意,您绘制的是从 0 到 19 的整数。绘制连续函数时,区间中的线性间隔点通常更有意义。
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 0.8, 100)
y = x * (-1e-9 *(2.73**(x/0.0258*0.8) - 1) + 3.1)
plt.plot(x,y)
plt.ylim(-0.5, 2)