作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
print("input a, b and c for a quadratic equation ax^2+bx+c=0")
a = float(input("a ="))
b = float(input("b ="))
c = float(input("c ="))
D = (b**2) - (4*a*c)
if D>0:
s1 = (-b+D**0.5)/(2*a)
s2 = (-b-D**0.5)/(2*a)
print("the two solutions are: {} and {}".format(s1,s2))
elif D==0:
s3 = (-b)/(2*a)
print("the one solution is: {}".format(s3))
elif D<0:
print("no solution")
最佳答案
您可以使用 Python 的内置 abs
函数或 NumPy 的 isclose
功能。
您可以添加另一个 if
条件为 abs(a - c) < tol
的语句或 np.isclose(a, c)
, 同时指定 atol
(绝对公差)或 rtol
(相对容差)可选参数为所需值。
引用:https://numpy.org/doc/stable/reference/generated/numpy.isclose.html
关于Python:仅打印二次方程容差范围内的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60333389/
我是一名优秀的程序员,十分优秀!