Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby
math
logarithm
communitycreator

What is Math.log2() in Ruby?

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The log2() function in Ruby returns the logarithm of a number to the base 2.

Figure 1 shows the mathematical representation of the log2() function.

Figure 1: Mathematical representation of the log2() function

Syntax

log2(number)

Parameter

This function requires a number whose logarithm base 2 must be calculated, and it must be greater than zero.

Return value

log2() returns the logarithm of a number to the base 2.

Example

#example
print "log2(10) : ", Math.log2(10), "\n"
print "log2(2) : ", Math.log2(2), "\n"
#error
#number is 0
print "log2(0) : ", Math.log2(0), "\n"

RELATED TAGS

ruby
math
logarithm
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring