Python cmath 模块的 cmath.log10() 函数用于获取以 10 为底的给定复数的对数。如果我们将字符串值作为参数传递给此函数,将生成 TypeError。
语法
以下是 cmath.log10() 函数的基本语法 -
cmath.log10(z)
参数
此函数接受复数的数值表达式作为参数。
返回值
此方法从 z>(0,-∞) 返回 z 的以 10 为底的对数。
示例 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)
The log10() value of List is: (1.6596310116070006+0j)
示例 3
如果我们将 string 作为参数传递,则此方法返回 TypeError。在下面的代码中,我们将获取字符串的 cmath.log10() 值。
import cmath
y='qikepu'
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
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
如果我们将 0 作为参数传递给此方法,它会引发 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
File "/home/cg/root/91202/main.py", line 3, in <module>
res=cmath.log10(x)
ValueError: math domain error