Python log10

Python 中的 log10 数学函数用于计算给定数字以 10 为底的对数值。此 log10 函数比 math.log(x, 10) 更准确。math log10 函数的语法是:

math.log10(number);

数字:有效的数字表达式,并且

  • 如果 number 参数是正数,则 log10 函数将返回输出。
  • 如果 number 参数是零或负数,则返回 ValueError。
  • 如果不是数字,它将返回 TypeError。

Python log10 函数示例

log10 函数用于计算给定数字以 10 为底的对数值。在此示例中,我们查找不同数据类型的以 10 为底的对数值并显示输出。

  1. 在前两个语句中,我们直接在正整数和十进制值上使用了 Python log10 函数。
  2. 接下来的两个语句,我们将其用于元组列表项。如果您观察上面的Python 屏幕截图,此数学函数以 10 为底计算对数。
  3. 在下一个语句中,我们尝试直接对多个值进行操作。
  4. 接下来,我们尝试了字符串值,log10 返回 TypeError: a float is required(类型错误:需要浮点数)。
  5. 我们尝试了负值,math.log10 返回 ValueError: math domain error(值错误:数学域错误)。
  6. 最后,我们尝试了零值。它返回 ValueError: math domain error(值错误:数学域错误)。请参阅对数文章以了解对数函数。
import math

Tup = (1, 2, 3, -4 , 5) # Tuple Declaration
Lis = [-1, 2, -3.5, -4 , 5] # List Declaration

print('Logarithm value of Positive Number = %.2f' %math.log10(1))
print('Logarithm value of Positive Decimal = %.2f' %math.log10(2.5))

print('Logarithm value of Tuple Item = %.2f' %math.log10(Tup[2]))
print('Logarithm value of List Item = %.2f' %math.log10(Lis[4]))

print('Logarithm value of Multiple Number = %.2f' %math.log10(2 + 7 - 5))
print('Logarithm value of String Number = ', math.log10('Hello'))
LOG10 Function