Python - AI 助手

Python cmath.sqrt() 函数



Python 的 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

这里我们将零作为参数传递给 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
python_modules.htm

示例 3

现在,我们使用 cmath.sqrt() 函数计算给定数字的平方根中的负值。

import cmath
print(cmath.sqrt(-4))
print(cmath.sqrt(-36))
print(cmath.sqrt(-81))

输出

结果如下获得 -

2j
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)
python_modules.htm
广告
© . All rights reserved.