📜  红宝石 |数学 log()函数

📅  最后修改于: 2022-05-13 01:54:33.746000             🧑  作者: Mango

红宝石 |数学 log()函数

Ruby 中的log()函数返回X的对数值。第二个参数是用户给出的对数值返回的底数。如果未给出,则默认基数为e

示例 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