【发布时间】:2015-09-16 16:28:11
【问题描述】:
from math import sqrt
a=1e-8
b=10
c=1e-8
x1 = ((-b)-sqrt((b**2)-(4*a*c)))/(2*a)
x2 = ((-b)+sqrt((b**2)-(4*a*c)))/(2*a)
print 'x1 = {}'.format(x1)
print 'x2 = {}'.format(x2)
print (4*a*c)
print (sqrt(b**2-4*a*c))
print b**2
print 2*a
当我运行程序时,返回:
x1 = -1e+09
x2 = 0.0
4e-16
10.0
100.0
2e-08
我需要 x2 等于 -1e-9。
问题似乎出在
sqrt((b**2)-(4*a*c))
因为它给出了 10 作为结果,显然是因为 4*(10^-8)*(10^-8) 几乎等于 0,并且被 python 认为是 0。
这会导致:
sqrt((b**2)-(4*a*c)) = sqrt(b**2) = sqrt(10**2) = 10
任何帮助将不胜感激
【问题讨论】: