import math
a=float(input("Enter value for a:"))
b=float(input("Enter value for b:"))
c=float(input("Enter value for c:"))
root1=-b+math.sqrt (b**2-4*a*c)/(2*a)
root2=-b-math.sqrt (b**2-4*a*c)/(2*a)
print(("root1 % 8.2f")%(root1))
print(("root1 % 8.2f")%(root2
import math 是启动我的程序的正确方法吗?我在第五行不断收到数学域错误?缺什么 ?谢谢
math.sqrt
仅适用于非负值。要么验证一下b**2 - 4*a*c >= 0
打电话之前math.sqrt
,或使用cmath.sqrt
而不是处理复杂的根。
>>> math.sqrt(-4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: math domain error
>>> cmath.sqrt(-4)
2j
请注意,如果您使用cmath.sqrt
,打印该值变得有点复杂,因为没有复杂值的格式说明符;你需要提取实部和虚部(root1.real
and root1.imag
)并分别格式化它们。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)