![](/img/trans.png)
[英]How to solve a Second order and Second Degree Differential Equation ( in python )?
[英]Second degree equation in python
我想在python中計算這個二階方程:
10 ^ -6x ^ 2 + 10x + 10 ^ -6 = 0
使用已知公式,它在python中看起來像這樣:
#This is just a small part of the program#
a = 10.**-6
b = 10.
c = 10.**-6
square = sqrt((b**2) - (4.*a*c))
numerator1 = -b - square
numerator2 = -b + square
denominator = 2.*a
print square
print numerator1
print numerator2
現在我的問題是:四舍五入的錯誤使我
square = 10.0
numerator1 = -20
numerator2 = -1.98951966013e-13
為什么我的第二個分子這么離譜? -b在兩種情況下都是相同的...這會使我的x2計算錯誤。 如何解決此舍入錯誤?
您的問題是由於浮點精度問題引起的。 您可以在這里閱讀更多內容-https: //docs.python.org/2/tutorial/floatingpoint.html
您可以通過四舍五入來克服這一點-
>>> round(numerator2, 3)
-0.0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.