Ruby | Math log2() function

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

7.734709620225838
8.0
4.754887502163468
6.643856189774724

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

4.392317422778761
1.0
4.247927513443585
6.918863237274595

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.