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.
The definition of the Log2 function inside the math package is as follows:
Log2 function takes a single argument of type float64.
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.
Following is a simple example that generates the binary logarithm value of 5:
package mainimport ("fmt""math")func main() {x := 5.0y := math.Log2(x)fmt.Print(x, "'s binary logarithm value is ", y)}
The following example shows how the Log2 function handles infinite values.
The
Inffunction returns an infinite value with a sign matching the sign of the argument that it is given.
package mainimport ("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
NaNfunction to generate the undefined numeric values to test.
package mainimport ("fmt""math")func main() {x := math.NaN()y := math.Log2(x)fmt.Print(x, "'s binary logarithm value is ", y)}
Free Resources