Python cmath 模块的 cmath.sqrt() 函数用于查找复数的平方根。给定数字的平方根是乘以自身得到该特定数字的因子。
语法
以下是 Python cmath.sqrt() 函数的语法 -
cmath.sqrt(x)
参数
此函数接受任何数字,即大于或等于 0。
返回值
此方法返回指定值的平方根。
示例 1
在下面的示例中,我们使用 cmath.sqrt() 函数在给定数字的平方根中传递不同的正值。
import cmath
x=cmath.sqrt(200)
y=cmath.sqrt(36)
z=cmath.sqrt(cmath.pi)
print(x,y,z)
输出
当我们运行上述程序时,它会产生以下结果——
(14.142135623730951+0j) (6+0j) (1.7724538509055159+0j)
示例 2
在这里,我们将 0 作为参数传递给 cmath.sqrt() 函数。这给出了 0j 作为结果。
import cmath
num = 0
res = cmath.sqrt(num)
print('The square root of zero is : ', res)
输出
上述程序生成以下输出 -
The square root of zero is : 0j
示例 3
现在,我们正在使用 cmath.sqrt() 函数计算给定数字的平方根中的负值。
import cmath
print(cmath.sqrt(-4))
print(cmath.sqrt(-36))
print(cmath.sqrt(-81))
输出
结果如下 -
2j
6j
9j
6j
9j
示例 4
在这里,我们使用 cmath.sqrt() 函数计算平方根中的浮点值。
import cmath
print(cmath.sqrt(0.4))
print(cmath.sqrt(3.6))
print(cmath.sqrt(0.81))
输出
这将产生以下结果 -
(0.6324555320336759+0j)
(1.8973665961010275+0j)
(0.9+0j)
(1.8973665961010275+0j)
(0.9+0j)