python - 二次根函数中的数学域错误

标签 python python-3.x

我的代码:

def root1(a,b,c):
    return (-b+sqrt((b**2)-4*a*c))/2*a
def root2(a,b,c):
    return (-b-sqrt((b**2)-4*a*c))/2*a

我遇到了一个ValueError: math domain error,我不知道为什么。

最佳答案

大概你有类似的东西

from math import sqrt

代替导入,使用

from cmath import sqrt

cmath 中的sqrt 函数可以处理否定参数。

例如,

>>> from math import sqrt
>>> sqrt(-4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: math domain error

>>> from cmath import sqrt
>>> sqrt(-4)
2j

关于python - 二次根函数中的数学域错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33054093/

相关文章:

python - flake8 和 "old style class declaration"

python - 使用多个模块 Python

python - 如何使用 Python 3.2 电子邮件模块发送以 utf-8 编码并带有 quoted-printable 的 unicode 消息?

python - Python 的 itertools.groupby() 文档中 keyfunc 的用途是什么?

python - 如何从 Python 中具有不同长度的列表列表创建数据框?

python - 创建递归路径 Python 的有效方法

Python:使用 Self 并即时向对象添加方法

python - 在 python 中构建一个调用图,包括模块和函数?

python-3.x - 每当我在 CMD 中运行 pip -V 时,Windows 就会崩溃

python-3.x - 存档中的数据库还原错误 : "There is no item named ' dump. sql'”