为什么我会收到此错误?
我正在尝试解这样的方程:
ax^2 + bx + c
Traceback (most recent call last):
File "I:/Taller de Programacion I/Clase 5/11.py", line 6, in <module>
x1 = (-b + sqrt(b ** 2 - a * c)) / (2 * a)
ValueError: math domain error
The math
模块(我想您正在使用)不支持复数。要么使用cmath
(python2 和 python3)或幂运算符**
(python3)。
无论判别式的符号如何,这都应该始终有效:
x1 = (-b + (b ** 2 - 4 * a * c) ** .5) / 2 / a
Example:
>>> b = 1
>>> a = 2
>>> c = 3
>>> (-b + (b ** 2 - 4 * a * c) ** .5) / 2 / a
(-0.24999999999999992+1.1989578808281798j)
使用时math.sqrt
具有相同的值会引发所描述的错误:
>>> (-b + sqrt(b ** 2 - a * c)) / (2 * a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: math domain error
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)