Python - AI 助手

Python cmath.log10() 函式



Python 的 cmath.log10() 函式用於獲取給定複數以 10 為底的對數。如果將字串值作為引數傳遞給此函式,則會生成 TypeError。

語法

以下是 cmath.log10() 函式的基本語法:

cmath.log10(z)

引數

此函式接受複數的數值表示式作為引數。

返回值

此方法返回 z 的 以 10 為底 的對數,z > (0,-∞)。

示例 1

讓我們討論一下使用複數計算 cmath.log10() 值的基本 Python 程式。

import cmath
x=2+3j
y=cmath.log10(x)
print(y)

輸出

執行上述程式時,會產生以下結果:

(0.5569716761534184+0.42682189085546657j)

示例 2

在下面的程式碼中,我們建立了數字的元組和列表。然後,在 log10 方法中,我們將使用 cmath.log10() 查詢元組中索引 2 處的數值和列表中索引 3 處的數值的對數。

import cmath
Tuple = (6,11,-24,35,-65)
List = [5,15,-25,45.67,-75]
print('The log10() value of Tuple is:',cmath.log10(Tuple[2]))
print('The log10() value of List is:',cmath.log10(List[3]))

輸出

上述程式會生成以下輸出:

The log10() value of Tuple is: (1.380211241711606+1.3643763538418412j)
The log10() value of List is: (1.6596310116070006+0j)

示例 3

如果將字串作為引數傳遞,則此方法返回 TypeError。在下面的程式碼中,我們獲取字串的 cmath.log10() 值。

import cmath
y='TutorialsPoint'
print('The log10() value of string is:',math.log10(y))

輸出

執行程式碼時,我們將獲得如下所示的輸出:

Traceback (most recent call last):
  File "/home/cg/root/83980/main.py", line 6, in <module>
    print('The log10() value of string is:',cmath.log10(y))
TypeError: must be real number, not str

示例 4

如果將零作為引數傳遞給此方法,則會引發 ValueError。

import cmath
x=0.0
res=cmath.log10(x)
print('The result for x is:',res)

輸出

獲得的輸出如下:

Traceback (most recent call last):
  File "/home/cg/root/91202/main.py", line 3, in <module>
    res=cmath.log10(x)
ValueError: math domain error
python_modules.htm
廣告