红宝石 |数学 log()函数
Ruby 中的log()函数返回X的对数值。第二个参数是用户给出的对数值返回的底数。如果未给出,则默认基数为e 。
Syntax: Math.log(X, base)
Parameter: The function takes one mandatory parameter X whose logarithm value is to be returned and a non-mandatory parameter base, to whose base is the logarithm.
Return Value: The function returns the logarithm value of X.
示例 1 :
# Ruby program for log() function
# Assigning values
val1 = 213
val2 = 256
base2 = 2
val3 = 27
base3 = 3
val4 = 100
base4 = 10
# Prints the value returned by log()
puts Math.log(val1)
puts Math.log(val2, base2)
puts Math.log(val3, base3)
puts Math.log(val4, base4)
输出:
5.3612921657094255
8.0
3.0
2.0
示例 2 :
# Ruby program for log() function
# Assigning values
val1 = 10
val2 = 256
base2 = 4
val3 = 27
base3 = 10
val4 = 105
base4 = 7
# Prints the value returned by log()
puts Math.log(val1)
puts Math.log(val2, base2)
puts Math.log(val3, base3)
puts Math.log(val4, base4)
输出:
2.302585092994046
4.0
1.4313637641589871
2.3916625094004957
参考:https://devdocs.io/ruby~2.5/math#method-c-log