Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

golang

What is the Log2 function in golang?

Faraz Karim

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.

Answers Code

The Go programming language uses the Log2 function to find the binary logarithm of a float64 number.

To use this function, you must import the math package in your file and access the Log2 function within it using the . notation (math.Log2). Here, Log2 is the actual function, while math is the Go package that stores the definition of this function.

Function definition

The definition of the Log2 function inside the math package is as follows:

Parameters

Log2 function takes a single argument of type float64.

Return value

The Log2 function returns a single value of type int, representing the input argument’s binary logarithm.

Following are some of the special return cases:

  • +Inf: Positive infinity is returned if the passed argument is also positive infinity.

  • -Inf: Negative infinity is returned if the passed argument is zero.

  • NAN: The Log2 function returns NAN if the passed argument is either a NAN value, negative infinity value, or value which is less than 0.

Examples

Following is a simple example that generates the binary logarithm value of 5:

package main
import (
"fmt"
"math"
)
func main() {
x := 5.0
y := math.Log2(x)
fmt.Print(x, "'s binary logarithm value is ", y)
}

The following example shows how the Log2 function handles infinite values.

The Inf function returns an infinite value with a sign matching the sign of the argument that it is given.

package main
import (
"fmt"
"math"
)
func main() {
x := math.Inf(-1)
y := math.Log2(x)
fmt.Print(x, "'s binary logarithm value is ", y)
fmt.Print( "\n")
a := math.Inf(1)
b := math.Log2(a)
fmt.Print(a, "'s binary logarithm value is ", b)
}

The following examples show how the Log2 function handles undefined values.

We use the NaN function to generate the undefined numeric values to test.

package main
import (
"fmt"
"math"
)
func main() {
x := math.NaN()
y := math.Log2(x)
fmt.Print(x, "'s binary logarithm value is ", y)
}

RELATED TAGS

golang

CONTRIBUTOR

Faraz Karim
Copyright ©2022 Educative, Inc. All rights reserved

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.

Answers Code
Keep Exploring