Open In App

Ruby | Math log2() function

Improve
Improve
Like Article
Like
Save
Share
Report

The log2() function in Ruby returns the base 2 logarithm value of X.

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.

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


Output:

7.734709620225838
8.0
4.754887502163468
6.643856189774724

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


Output:

4.392317422778761
1.0
4.247927513443585
6.918863237274595

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


Last Updated : 07 Jan, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads