Open In App

Ruby | Math log() function

Last Updated : 07 Jan, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The log() function in Ruby returns the logarithm value of X. The second parameter is the base given by the user to which the logarithm value is returned. In case its not given, then the default base is 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.

Example 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)


Output:

5.3612921657094255
8.0
3.0
2.0

Example 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)


Output:

2.302585092994046
4.0
1.4313637641589871
2.3916625094004957

Reference: https://devdocs.io/ruby~2.5/math#method-c-log



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads