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
更精确的十进制值当我运行程序,这将返回:我怎样才能在Python
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
任何帮助,将不胜感激