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
廣告