红宝石 |数学 log2()函数
Ruby 中的log2()函数返回X的以 2 为底的对数值。
Syntax: Math.log2(X)
Parameter: The function takes one mandatory parameter X whose base 2 logarithm value is to be returned.
Return Value: The function the base 2 logarithm value of X.
示例 1 :
# Ruby program for log2() function
# Assigning values
val1 = 213
val2 = 256
val3 = 27
val4 = 100
# Prints the value returned by log2()
puts Math.log2(val1)
puts Math.log2(val2)
puts Math.log2(val3)
puts Math.log2(val4)
输出:
7.734709620225838
8.0
4.754887502163468
6.643856189774724
示例 2 :
# Ruby program for log2() function
# Assigning values
val1 = 21
val2 = 2
val3 = 19
val4 = 121
# Prints the value returned by log2()
puts Math.log2(val1)
puts Math.log2(val2)
puts Math.log2(val3)
puts Math.log2(val4)
输出:
4.392317422778761
1.0
4.247927513443585
6.918863237274595
参考:https://devdocs.io/ruby~2.5/math#method-c-log2