numpy.emath.sqrt#

emath.sqrt(x)[原始碼]#

計算 x 的平方根。

對於負輸入元素,會傳回複數值(與傳回 NaN 的 numpy.sqrt 不同)。

參數:
xarray_like

輸入值。

傳回值:
outndarray 或 純量

x 的平方根。如果 x 是純量,則 out 也是,否則會傳回陣列。

另請參閱

numpy.sqrt

範例

對於實數、非負輸入,這就像 numpy.sqrt 一樣運作

>>> import numpy as np
>>> np.emath.sqrt(1)
1.0
>>> np.emath.sqrt([1, 4])
array([1.,  2.])

但它會自動處理負輸入

>>> np.emath.sqrt(-1)
1j
>>> np.emath.sqrt([-1,4])
array([0.+1.j, 2.+0.j])

預期會有不同的結果,因為:浮點數 0.0 和 -0.0 是不同的。

為了更精確的控制,請明確地使用 complex(),如下所示

>>> np.emath.sqrt(complex(-4.0, 0.0))
2j
>>> np.emath.sqrt(complex(-4.0, -0.0))
-2j