我在使用简单的线性和二次方程计算器时遇到了困难。二次曲线有效,但线性没有。它给出了浮点数的答案,但由于某种原因,它总是一些数字然后是0.例如:如果我想解决 2x + 13 = 0 ,答案将是 -7.0 ,但应该 -6.5 。我想由于某种原因它将它(天花板)四舍五入。我很确定我在某处有一些语法错误,但我找不到它。 有关如何解决这个问题的任何建议? 谢谢你的帮助。
import math
a,b,c = input("Enter the coefficients of a, b and c separated by commas: ")
d = b**2-4*a*c # discriminant
if a == 0:
x = -c/b # liner equation
x = float(x)
b = float(b)
c = float(c)
print "This linear equation has one solution:", x
elif d < 0:
print "This quadratic equation has no real solution"
elif d == 0:
x = (-b+math.sqrt(d))/(2*a)
print "This quadratic equation has one solutions:", x
else:
x1 = (-b+math.sqrt(d))/(2*a)
x2 = (-b-math.sqrt(d))/(2*a)
print "This quadratic equation has two solutions:", x1, "and", x2
答案 0 :(得分:0)
答案 1 :(得分:0)
x = -c/b
- 如果c和b都是整数,那么结果也将四舍五入为整数。
执行x = -c/float(b)