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
广告
© . All rights reserved.