python对数函数_Python log()函数来计算对数

python对数函数

Logarithms are used to depict and represent large numbers. The log is an inverse of the exponent. This article will dive into the Python log() functions. The logarithmic functions of Python help the users to find the log of numbers in a much easier and efficient manner.

对数用于描述和表示大数。 对数是指数的倒数。 本文将深入探讨Python log()函数 。 Python的对数函数可帮助用户以更容易有效的方式查找数字的对数。



了解Python中的log()函数 (Understanding the log() functions in Python)

In order to use the functionalities of Log functions, we need to import the math module using the below statement.

为了使用Log函数的功能,我们需要使用以下语句导入 math模块。


import math

We all need to take note of the fact that the Python Log functions cannot be accessed directly. We need to use the math module to access the log functions in the code.

我们都需要注意一个事实,即Python Log函数不能直接访问。 我们需要使用math模块来访问代码中的日志功能。

Syntax:

句法:


math.log(x)

The math.log(x) function is used to calculate the natural logarithmic value i.e. log to the base e (Euler’s number) which is about 2.71828, of the parameter value (numeric expression), passed to it.

math.log(x)函数用于计算自然对数值,即,将传递给它的参数值( 数字表达式的底数e (欧拉数)记录为约2.71828。

Example:

例:


import math   

print("Log value: ", math.log(2))

In the above snippet of code, we are requesting the logarithmic value of 2.

在上面的代码段中,我们要求对数值为2。

Output:

输出:


Log value:  0.6931471805599453


Python log()函数的变体 (Variants of Python log() Functions)

The following are the variants of the basic log function in Python:

以下是Python中基本日志功能的变体:

  • log2(x)

    log2(x)
  • log(x, Base)

    log(x,基本)
  • log10(x)

    log10(x)
  • log1p(x)

    log1p(x)


1. log2(x)–以2为底的对数 (1. log2(x) – log base 2)

The math.log2(x) function is used to calculate the logarithmic value of a numeric expression of base 2.

math.log2(x)函数用于计算以2为底的数字表达式对数值

Syntax:

句法:


math.log2(numeric expression)

Example:

例:


import math 

print ("Log value for base 2: ") 
print (math.log2(20)) 

Output:

输出:


Log value for base 2: 
4.321928094887363


2. log(n,Base)–日志基数n (2. log(n, Base) – log base n)

The math.log(x,Base) function calculates the logarithmic value of x i.e. numeric expression for a particular (desired) base value.

math.log(x,Base)函数计算x的对数值,即特定(所需)基值的数字表达式。

Syntax:

句法:


math.log(numeric_expression,base_value)

This function accepts two arguments:

此函数接受两个参数:

  • numeric expression

    数值表达式
  • Base value

    基本值

Note: If no base value is provided to the function, the math.log(x,(Base)) acts as a basic log function and calculates the log of the numeric expression to the base e.

注意 :如果没有为函数提供基值 ,则math.log(x,(Base))充当基本对数函数,并计算数字表达式对基e的对数

Example:

例:


import math 

print ("Log value for base 4 : ") 
print (math.log(20,4)) 

Output:

输出:


Log value for base 4 : 
2.1609640474436813


3. log10(x)–以10为底的对数 (3. log10(x) – log base 10)

The math.log10(x) function calculates the logarithmic value of the numeric expression to the base 10.

math.log10(x)函数将数字表达式的对数值计算为以10

Syntax:

句法:


math.log10(numeric_expression)

Example:

例:


import math 

print ("Log value for base 10: ") 
print (math.log10(15)) 

In the above snippet of code, the logarithmic value of 15 to the base 10 is calculated.

在上面的代码片段中,计算了以10的对数值15

Output:

输出:


Log value for base 10 : 
1.1760912590556813


4. log1p(x) (4. log1p(x))

The math.log1p(x) function calculates the log(1+x) of a particular input value i.e. x

math.log1p(x)函数计算特定输入值(即x log(1 + x)

Note: math.log1p(1+x) is equivalent to math.log(x)

注意: math.log1p(1 + x)等同于math.log(x)

Syntax:

句法:


math.log1p(numeric_expression)

Example:

例:


import math 

print ("Log value(1+15) for x = 15 is: ") 
print (math.log1p(15)) 

In the above snippet of code, the log value of (1+15) for the input expression 15 is calculated.

在上面的代码片段中,计算了输入表达式15的对数(1 + 15)。

Thus, math.log1p(15) is equivalent to math.log(16).

因此, math.log1p(15)等同于math.log(16)

Output:

输出:


Log value(1+15) for x = 15 is: 
2.772588722239781


了解Python NumPy中的日志 (Understanding log in Python NumPy )

Python NumPy enables us to calculate the natural logarithmic values of the input NumPy array elements simultaneously.

Python NumPy使我们能够同时计算输入NumPy数组元素的自然对数值

In order to use the numpy.log() method, we need to import the NumPy module using the below statement.

为了使用numpy.log()方法,我们需要使用以下语句导入NumPy模块


import numpy

Syntax:

句法:


numpy.log(input_array)

The numpy.log() function accepts input array as a parameter and returns the array with the logarithmic value of elements in it.

numpy.log()函数接受输入数组作为参数,并返回其中元素对数的数组。

Example:

例:


import numpy as np 

inp_arr = [10, 20, 30, 40, 50] 
print ("Array input elements:\n", inp_arr) 

res_arr = np.log(inp_arr) 
print ("Resultant array elements:\n", res_arr) 

Output:

输出:


Array input elements:
 [10, 20, 30, 40, 50]
Resultant array elements:
 [ 2.30258509  2.99573227  3.40119738  3.68887945  3.91202301]


结论 (Conclusion)

In this article, we have understood the working of Python Log functions and have unveiled the variants of the logarithmic function in Python.

在本文中,我们了解了Python Log函数的工作原理,并揭示了Python中对数函数的变体。



参考资料 (References)

翻译自: https://www.journaldev.com/36109/python-log-function-logarithm

python对数函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值