python – Quadractic Formula混淆了

所以我做了一个python代码,用二次公式求解x.除了标志之外,一切都在最终解决.例如,如果你想要x ^ 2 10x 25因子,当答案应该是5,5时,我的代码输出-5,-5.

def quadratic_formula():
    a = int(input("a = "))
    b = int(input("b = "))
    c = int(input("c = "))
    bsq = b * b
    fourac = 4 * a * c
    sqrt = (bsq - fourac) ** (.5)
    oppb = -b
    numerator_add = (oppb) + (sqrt)
    numerator_sub = (oppb) - (sqrt)
    twoa = 2 * a
    addition_answer = (numerator_add) / (twoa)
    subtraction_answer = (numerator_sub) / (twoa)
    print(addition_answer)
    print(subtraction_answer)

解决方法:

你的解决方案很好,让我们用sympy来证明它:

>>> (x**2+10*x+25).subs(x,-5)
0

如你所见,-5是根,而5是根

>>> (x**2+10*x+25).subs(x,5)
100

不是,现在…如果你扩展你的2根[-5,-5]像:

>>> ((x+5)*(x+5)).expand()
x**2 + 10*x + 25

你可以看到结果匹配.

实际上,您还可以确认显示二次方程的根是正确的:

python  –  Quadractic Formula混淆了

我强烈建议你回顾一下The Quadratic Formula的概念,当它清楚地回到编码时

上一篇:heroku空间部署步骤


下一篇:javascript – 四边形2D细分的细分