在 R 编程中执行对数计算 - log()、log10()、log1p() 和 log2() 函数
R 语言中的log()
函数返回参数中传递的参数的自然对数(以 e 为底的对数)。
Syntax: log(x)
Parameter:
x: Specified value.
Returns: It returns the natural logarithm of the specified value, infinity for 0 and NaN for the negative value.
示例 1:
# R program to calculate log value
print(log(1))
print(log(30))
print(log(0))
print(log(-44))
输出:
[1] 0
[1] 3.401197
[1] -Inf
[1] NaN
Warning message:
In log(-44) : NaNs produced
对数(x,基数 = y)
log(x,base=y)
是 R 中的一个内置函数,用于计算以 y 为底的指定值的对数,0 为无穷大,负值为 NaN。
Syntax: log(x, base = y)
Parameters:
x and base y.
Returns: It returns the logarithm of the specified value to base y, infinity for 0, and NaN for the negative value.
示例 2:
# R program to calculate log value
print(log(10,base=10))
print(log(16,base=2))
print(log(0,base=10))
print(log(-44,base=4))
输出:
[1] 1
[1] 4
[1] -Inf
[1] NaN
Warning message:
In print(log(-44, base = 4)) : NaNs produced
log10()函数
log10()
是 R 中的一个内置函数,用于计算以 10 为底的指定值的对数,0 为无穷大,负值为 NaN。
Syntax: log10(x)
Parameters:
x: Specified values.
Returns: It returns the logarithm of the specified value to base 10,infinity for 0 and NaN for negative value.
示例 2:
# R program to calculate log value
print(log10(1))
print(log10(10))
print(log10(0))
print(log10(-44))
输出:
[1] 0
[1] 1
[1] -Inf
[1] NaN
Warning message:
In print(log10(-44)) : NaNs produced
log1p()
log1p()
是 R 中的一个内置函数,用于计算 1+x 的准确自然对数,其中 x 是指定值,对于 0 抛出无穷大,对于负值抛出 NaN。
Syntax: log1p(x)
Parameters:
x: Specified values.
Returns: It returns the accurate natural logarithm of 1+x, where x is the specified value and throws infinity for 0 and NaN for negative value.
例子:
# R program to illustrate
# the use of log1p() method
# Getting the accurate natural
# logarithm of 1 + x, where x is
# the specified value and throws
# infinity for 0 and NaN for negative value.
print(log1p(1))
print(log1p(10))
print(log1p(0))
print(log1p(-44))
输出:
[1] 0.6931472
[1] 2.397895
[1] 0
[1] NaN
Warning message:
In log1p(-44) : NaNs produced
日志2()
log2()
是 R 中的一个内置函数,用于计算 x 以 2 为底的对数,其中 x 是指定值,或者对于 0 抛出无穷大,对于负值抛出 NaN。
Syntax: log2(x)
Parameters:
x: Specified values.
Returns: It returns the logarithm of x to base 2, where x is the specified value or throws infinity for 0 and NaN for negative value.
例子:
# R program to illustrate
# the use of log2() method
# Getting the logarithm of x
# to base 2, where x is the
# specified value or throws
# infinity for 0 and NaN for negative value.
print(log2(1))
print(log2(2))
print(log2(0))
print(log2(-44))
输出:
[1] 0
[1] 1
[1] -Inf
[1] NaN
Warning message:
In print(log2(-44)) : NaNs produced